Py3 compatibility of CryptMessage plugin, Rename ecies crypto function names to make it more clear
This commit is contained in:
parent
883c2851ff
commit
40569eee2e
2 changed files with 30 additions and 27 deletions
|
@ -1,12 +1,15 @@
|
||||||
from lib.pybitcointools import bitcoin as btctools
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import base64
|
||||||
|
|
||||||
|
import lib.pybitcointools as btctools
|
||||||
|
|
||||||
ecc_cache = {}
|
ecc_cache = {}
|
||||||
|
|
||||||
|
|
||||||
def encrypt(data, pubkey, ephemcurve=None, ciphername='aes-256-cbc'):
|
def eciesEncrypt(data, pubkey, ephemcurve=None, ciphername='aes-256-cbc'):
|
||||||
from lib import pyelliptic
|
import pyelliptic
|
||||||
curve, pubkey_x, pubkey_y, i = pyelliptic.ECC._decode_pubkey(pubkey)
|
pubkey_openssl = toOpensslPublickey(base64.b64decode(pubkey))
|
||||||
|
curve, pubkey_x, pubkey_y, i = pyelliptic.ECC._decode_pubkey(pubkey_openssl)
|
||||||
if ephemcurve is None:
|
if ephemcurve is None:
|
||||||
ephemcurve = curve
|
ephemcurve = curve
|
||||||
ephem = pyelliptic.ECC(curve=ephemcurve)
|
ephem = pyelliptic.ECC(curve=ephemcurve)
|
||||||
|
@ -19,17 +22,20 @@ def encrypt(data, pubkey, ephemcurve=None, ciphername='aes-256-cbc'):
|
||||||
mac = pyelliptic.hmac_sha256(key_m, ciphertext)
|
mac = pyelliptic.hmac_sha256(key_m, ciphertext)
|
||||||
return key_e, ciphertext + mac
|
return key_e, ciphertext + mac
|
||||||
|
|
||||||
|
def eciesDecrypt(encrypted_data, privatekey):
|
||||||
|
ecc_key = getEcc(privatekey)
|
||||||
|
return ecc_key.decrypt(base64.b64decode(encrypted_data))
|
||||||
|
|
||||||
def split(encrypted):
|
def split(encrypted):
|
||||||
iv = encrypted[0:16]
|
iv = encrypted[0:16]
|
||||||
ciphertext = encrypted[16+70:-32]
|
ciphertext = encrypted[16 + 70:-32]
|
||||||
|
|
||||||
return iv, ciphertext
|
return iv, ciphertext
|
||||||
|
|
||||||
|
|
||||||
def getEcc(privatekey=None):
|
def getEcc(privatekey=None):
|
||||||
from lib import pyelliptic
|
import pyelliptic
|
||||||
global eccs
|
global ecc_cache
|
||||||
if privatekey not in ecc_cache:
|
if privatekey not in ecc_cache:
|
||||||
if privatekey:
|
if privatekey:
|
||||||
publickey_bin = btctools.encode_pubkey(btctools.privtopub(privatekey), "bin")
|
publickey_bin = btctools.encode_pubkey(btctools.privtopub(privatekey), "bin")
|
||||||
|
|
|
@ -10,11 +10,7 @@ from . import CryptMessage
|
||||||
|
|
||||||
@PluginManager.registerTo("UiWebsocket")
|
@PluginManager.registerTo("UiWebsocket")
|
||||||
class UiWebsocketPlugin(object):
|
class UiWebsocketPlugin(object):
|
||||||
def encrypt(self, text, publickey):
|
def eciesDecrypt(self, encrypted, privatekey):
|
||||||
encrypted = CryptMessage.encrypt(text, CryptMessage.toOpensslPublickey(publickey))
|
|
||||||
return encrypted
|
|
||||||
|
|
||||||
def decrypt(self, encrypted, privatekey):
|
|
||||||
back = CryptMessage.getEcc(privatekey).decrypt(encrypted)
|
back = CryptMessage.getEcc(privatekey).decrypt(encrypted)
|
||||||
return back.decode("utf8")
|
return back.decode("utf8")
|
||||||
|
|
||||||
|
@ -31,11 +27,11 @@ class UiWebsocketPlugin(object):
|
||||||
def actionEciesEncrypt(self, to, text, publickey=0, return_aes_key=False):
|
def actionEciesEncrypt(self, to, text, publickey=0, return_aes_key=False):
|
||||||
if type(publickey) is int: # Encrypt using user's publickey
|
if type(publickey) is int: # Encrypt using user's publickey
|
||||||
publickey = self.user.getEncryptPublickey(self.site.address, publickey)
|
publickey = self.user.getEncryptPublickey(self.site.address, publickey)
|
||||||
aes_key, encrypted = self.encrypt(text.encode("utf8"), publickey.decode("base64"))
|
aes_key, encrypted = CryptMessage.eciesEncrypt(text.encode("utf8"), publickey)
|
||||||
if return_aes_key:
|
if return_aes_key:
|
||||||
self.response(to, [base64.b64encode(encrypted), base64.b64encode(aes_key)])
|
self.response(to, [base64.b64encode(encrypted).decode("utf8"), base64.b64encode(aes_key).decode("utf8")])
|
||||||
else:
|
else:
|
||||||
self.response(to, base64.b64encode(encrypted))
|
self.response(to, base64.b64encode(encrypted).decode("utf8"))
|
||||||
|
|
||||||
# Decrypt a text using privatekey or the user's site unique private key
|
# Decrypt a text using privatekey or the user's site unique private key
|
||||||
# Return: Decrypted text or list of decrypted texts
|
# Return: Decrypted text or list of decrypted texts
|
||||||
|
@ -51,7 +47,7 @@ class UiWebsocketPlugin(object):
|
||||||
texts = [] # Decoded texts
|
texts = [] # Decoded texts
|
||||||
for encrypted_text in encrypted_texts:
|
for encrypted_text in encrypted_texts:
|
||||||
try:
|
try:
|
||||||
text = self.decrypt(encrypted_text.decode("base64"), privatekey)
|
text = CryptMessage.eciesDecrypt(encrypted_text, privatekey).decode("utf8")
|
||||||
texts.append(text)
|
texts.append(text)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
texts.append(None)
|
texts.append(None)
|
||||||
|
@ -64,15 +60,15 @@ class UiWebsocketPlugin(object):
|
||||||
# Encrypt a text using AES
|
# Encrypt a text using AES
|
||||||
# Return: Iv, AES key, Encrypted text
|
# Return: Iv, AES key, Encrypted text
|
||||||
def actionAesEncrypt(self, to, text, key=None, iv=None):
|
def actionAesEncrypt(self, to, text, key=None, iv=None):
|
||||||
from lib import pyelliptic
|
import pyelliptic
|
||||||
|
|
||||||
if key:
|
if key:
|
||||||
key = key.decode("base64")
|
key = base64.b64decode(key)
|
||||||
else:
|
else:
|
||||||
key = os.urandom(32)
|
key = os.urandom(32)
|
||||||
|
|
||||||
if iv: # Generate new AES key if not definied
|
if iv: # Generate new AES key if not definied
|
||||||
iv = iv.decode("base64")
|
iv = base64.b64decode(iv)
|
||||||
else:
|
else:
|
||||||
iv = pyelliptic.Cipher.gen_IV('aes-256-cbc')
|
iv = pyelliptic.Cipher.gen_IV('aes-256-cbc')
|
||||||
|
|
||||||
|
@ -81,12 +77,13 @@ class UiWebsocketPlugin(object):
|
||||||
else:
|
else:
|
||||||
encrypted = ""
|
encrypted = ""
|
||||||
|
|
||||||
self.response(to, [base64.b64encode(key), base64.b64encode(iv), base64.b64encode(encrypted)])
|
res = [base64.b64encode(item).decode("utf8") for item in [key, iv, encrypted]]
|
||||||
|
self.response(to, res)
|
||||||
|
|
||||||
# Decrypt a text using AES
|
# Decrypt a text using AES
|
||||||
# Return: Decrypted text
|
# Return: Decrypted text
|
||||||
def actionAesDecrypt(self, to, *args):
|
def actionAesDecrypt(self, to, *args):
|
||||||
from lib import pyelliptic
|
import pyelliptic
|
||||||
|
|
||||||
if len(args) == 3: # Single decrypt
|
if len(args) == 3: # Single decrypt
|
||||||
encrypted_texts = [(args[0], args[1])]
|
encrypted_texts = [(args[0], args[1])]
|
||||||
|
@ -96,16 +93,16 @@ class UiWebsocketPlugin(object):
|
||||||
|
|
||||||
texts = [] # Decoded texts
|
texts = [] # Decoded texts
|
||||||
for iv, encrypted_text in encrypted_texts:
|
for iv, encrypted_text in encrypted_texts:
|
||||||
encrypted_text = encrypted_text.decode("base64")
|
encrypted_text = base64.b64decode(encrypted_text)
|
||||||
iv = iv.decode("base64")
|
iv = base64.b64decode(iv)
|
||||||
text = None
|
text = None
|
||||||
for key in keys:
|
for key in keys:
|
||||||
ctx = pyelliptic.Cipher(key.decode("base64"), iv, 0, ciphername='aes-256-cbc')
|
ctx = pyelliptic.Cipher(base64.b64decode(key), iv, 0, ciphername='aes-256-cbc')
|
||||||
try:
|
try:
|
||||||
decrypted = ctx.ciphering(encrypted_text)
|
decrypted = ctx.ciphering(encrypted_text)
|
||||||
if decrypted and decrypted.decode("utf8"): # Valid text decoded
|
if decrypted and decrypted.decode("utf8"): # Valid text decoded
|
||||||
text = decrypted
|
text = decrypted.decode("utf8")
|
||||||
except Exception, err:
|
except Exception as err:
|
||||||
pass
|
pass
|
||||||
texts.append(text)
|
texts.append(text)
|
||||||
|
|
||||||
|
@ -145,5 +142,5 @@ class UserPlugin(object):
|
||||||
if "encrypt_publickey_%s" % index not in site_data:
|
if "encrypt_publickey_%s" % index not in site_data:
|
||||||
privatekey = self.getEncryptPrivatekey(address, param_index)
|
privatekey = self.getEncryptPrivatekey(address, param_index)
|
||||||
publickey = btctools.encode_pubkey(btctools.privtopub(privatekey), "bin_compressed")
|
publickey = btctools.encode_pubkey(btctools.privtopub(privatekey), "bin_compressed")
|
||||||
site_data["encrypt_publickey_%s" % index] = base64.b64encode(publickey)
|
site_data["encrypt_publickey_%s" % index] = base64.b64encode(publickey).decode("utf8")
|
||||||
return site_data["encrypt_publickey_%s" % index]
|
return site_data["encrypt_publickey_%s" % index]
|
||||||
|
|
Loading…
Reference in a new issue