Encryption


SUBMITTED BY: Guest

DATE: May 19, 2013, 3:59 a.m.

FORMAT: Python

SIZE: 7.0 kB

HITS: 2107

  1. #!/usr/bin/env python
  2. # Bitcoin wallet keys AES encryption / decryption (see http://github.com/joric/pywallet)
  3. # Uses pycrypto or libssl or libeay32.dll or aes.py from http://code.google.com/p/slowaes
  4. crypter = None
  5. try:
  6. from Crypto.Cipher import AES
  7. crypter = 'pycrypto'
  8. except:
  9. pass
  10. class Crypter_pycrypto( object ):
  11. def SetKeyFromPassphrase(self, vKeyData, vSalt, nDerivIterations, nDerivationMethod):
  12. data = vKeyData + vSalt
  13. for i in range(nDerivIterations):
  14. data = hashlib.sha512(data).digest()
  15. self.SetKey(data[0:32])
  16. self.SetIV(data[32:32+16])
  17. return len(data)
  18. def SetKey(self, key):
  19. self.chKey = key
  20. def SetIV(self, iv):
  21. self.chIV = iv[0:16]
  22. def Encrypt(self, data):
  23. return AES.new(self.chKey,AES.MODE_CBC,self.chIV).encrypt(data)[0:32]
  24. def Decrypt(self, data):
  25. return AES.new(self.chKey,AES.MODE_CBC,self.chIV).decrypt(data)[0:32]
  26. try:
  27. if not crypter:
  28. import ctypes
  29. import ctypes.util
  30. ssl = ctypes.cdll.LoadLibrary (ctypes.util.find_library ('ssl') or 'libeay32')
  31. crypter = 'ssl'
  32. except:
  33. pass
  34. class Crypter_ssl(object):
  35. def __init__(self):
  36. self.chKey = ctypes.create_string_buffer (32)
  37. self.chIV = ctypes.create_string_buffer (16)
  38. def SetKeyFromPassphrase(self, vKeyData, vSalt, nDerivIterations, nDerivationMethod):
  39. strKeyData = ctypes.create_string_buffer (vKeyData)
  40. chSalt = ctypes.create_string_buffer (vSalt)
  41. return ssl.EVP_BytesToKey(ssl.EVP_aes_256_cbc(), ssl.EVP_sha512(), chSalt, strKeyData,
  42. len(vKeyData), nDerivIterations, ctypes.byref(self.chKey), ctypes.byref(self.chIV))
  43. def SetKey(self, key):
  44. self.chKey = ctypes.create_string_buffer(key)
  45. def SetIV(self, iv):
  46. self.chIV = ctypes.create_string_buffer(iv)
  47. def Encrypt(self, data):
  48. buf = ctypes.create_string_buffer(len(data) + 16)
  49. written = ctypes.c_int(0)
  50. final = ctypes.c_int(0)
  51. ctx = ssl.EVP_CIPHER_CTX_new()
  52. ssl.EVP_CIPHER_CTX_init(ctx)
  53. ssl.EVP_EncryptInit_ex(ctx, ssl.EVP_aes_256_cbc(), None, self.chKey, self.chIV)
  54. ssl.EVP_EncryptUpdate(ctx, buf, ctypes.byref(written), data, len(data))
  55. output = buf.raw[:written.value]
  56. ssl.EVP_EncryptFinal_ex(ctx, buf, ctypes.byref(final))
  57. output += buf.raw[:final.value]
  58. return output
  59. def Decrypt(self, data):
  60. buf = ctypes.create_string_buffer(len(data) + 16)
  61. written = ctypes.c_int(0)
  62. final = ctypes.c_int(0)
  63. ctx = ssl.EVP_CIPHER_CTX_new()
  64. ssl.EVP_CIPHER_CTX_init(ctx)
  65. ssl.EVP_DecryptInit_ex(ctx, ssl.EVP_aes_256_cbc(), None, self.chKey, self.chIV)
  66. ssl.EVP_DecryptUpdate(ctx, buf, ctypes.byref(written), data, len(data))
  67. output = buf.raw[:written.value]
  68. ssl.EVP_DecryptFinal_ex(ctx, buf, ctypes.byref(final))
  69. output += buf.raw[:final.value]
  70. return output
  71. try:
  72. if not crypter:
  73. from aes import *
  74. crypter = 'pure'
  75. except:
  76. pass
  77. class Crypter_pure(object):
  78. def __init__(self):
  79. self.m = AESModeOfOperation()
  80. self.cbc = self.m.modeOfOperation["CBC"]
  81. self.sz = self.m.aes.keySize["SIZE_256"]
  82. def SetKeyFromPassphrase(self, vKeyData, vSalt, nDerivIterations, nDerivationMethod):
  83. data = vKeyData + vSalt
  84. for i in range(nDerivIterations):
  85. data = hashlib.sha512(data).digest()
  86. self.SetKey(data[0:32])
  87. self.SetIV(data[32:32+16])
  88. return len(data)
  89. def SetKey(self, key):
  90. self.chKey = [ord(i) for i in key]
  91. def SetIV(self, iv):
  92. self.chIV = [ord(i) for i in iv]
  93. def Encrypt(self, data):
  94. mode, size, cypher = self.m.encrypt(data, self.cbc, self.chKey, self.sz, self.chIV)
  95. return ''.join(map(chr, cypher))
  96. def Decrypt(self, data):
  97. chData = [ord(i) for i in data]
  98. return self.m.decrypt(chData, self.sz, self.cbc, self.chKey, self.sz, self.chIV)
  99. import hashlib
  100. def Hash(data):
  101. return hashlib.sha256(hashlib.sha256(data).digest()).digest()
  102. def main():
  103. #address
  104. addr = '1AJ3vE2NNYW2Jzv3fLwyjKF1LYbZ65Ez64'
  105. sec = '5JMhGPWc3pkdgPd9jqVZkRtEp3QB3Ze8ihv62TmmvzABmkNzBHw'
  106. secret = '47510706d76bc74a5d57bdcffc68c9bbbc2d496bef87c91de7f616129ac62b5f'.decode('hex')
  107. pubkey = '046211d9b7836892c8eef49c4d0cad7797815eff95108e1d30745c03577596c9c00d2cb1ab27c7f95c28771278f89b7ff40da49fe9b4ee834a3f6a88324db837d8'.decode('hex')
  108. ckey = '0f8c75e4c6ab3c642dd06786af80ca3a93e391637d029f1da919dad77d3c8e477efd479814ddf4c459aeba042420868f'.decode('hex')
  109. #master key
  110. crypted_key = '1e1d7ab34d8007f214eb528a1007c6721b9cd1d2c257adb25378ea8e47e3bdd22cfe93a8b6f18dcbe4206fe8c8178ff1'.decode('hex')
  111. salt = '3f94e3c670b695dd'.decode('hex')
  112. rounds = 47135
  113. method = 0
  114. password = '12345'
  115. global crypter
  116. if crypter == 'pycrypto':
  117. crypter = Crypter_pycrypto()
  118. elif crypter == 'ssl':
  119. crypter = Crypter_ssl()
  120. print "using ssl"
  121. elif crypter == 'pure':
  122. crypter = Crypter_pure()
  123. print "using slowaes"
  124. else:
  125. print("Need pycrypto of libssl or libeay32.dll or http://code.google.com/p/slowaes")
  126. exit(1)
  127. crypter.SetKeyFromPassphrase(password, salt, rounds, method)
  128. masterkey = crypter.Decrypt(crypted_key)
  129. crypter.SetKey(masterkey)
  130. crypter.SetIV(Hash(pubkey))
  131. d = crypter.Decrypt(ckey)
  132. e = crypter.Encrypt(d)
  133. print "masterkey:", masterkey.encode('hex')
  134. print 'c:', ckey.encode('hex')
  135. print 'd:', d.encode('hex')
  136. print 'e:', e.encode('hex')
  137. if __name__ == '__main__':
  138. main()

comments powered by Disqus