def actionAesDecrypt(self, to, *args): from lib import pyelliptic if len(args) == 3: # Single decrypt encrypted_texts = [(args[0], args[1])] keys = [args[2]] else: # Batch decrypt encrypted_texts, keys = args texts = [] # Decoded texts for iv, encrypted_text in encrypted_texts: encrypted_text = base64.b64decode(encrypted_text) iv = base64.b64decode(iv) text = None for key in keys: ctx = pyelliptic.Cipher(base64.b64decode(key), iv, 0, ciphername='aes-256-cbc') try: decrypted = ctx.ciphering(encrypted_text) if decrypted and decrypted.decode( "utf8"): # Valid text decoded text = decrypted.decode("utf8") except Exception as err: pass texts.append(text) if len(args) == 3: self.response(to, texts[0]) else: self.response(to, texts)
def actionAesEncrypt(self, to, text, key=None, iv=None): from lib import pyelliptic if key: key = base64.b64decode(key) else: key = os.urandom(32) if iv: # Generate new AES key if not definied iv = base64.b64decode(iv) else: iv = pyelliptic.Cipher.gen_IV('aes-256-cbc') if text: encrypted = pyelliptic.Cipher(key, iv, 1, ciphername='aes-256-cbc').ciphering( text.encode("utf8")) else: encrypted = b"" res = [ base64.b64encode(item).decode("utf8") for item in [key, iv, encrypted] ] self.response(to, res)
def actionAesEncrypt(self, to, text, key=None, iv=None): from lib import pyelliptic if key: key = key.decode("base64") else: key = os.urandom(32) if iv: # Generate new AES key if not definied iv = iv.decode("base64") else: iv = pyelliptic.Cipher.gen_IV('aes-256-cbc') if text: encrypted = pyelliptic.Cipher(key, iv, 1, ciphername='aes-256-cbc').ciphering( text.encode("utf8")) else: encrypted = "" self.response(to, [ base64.b64encode(key), base64.b64encode(iv), base64.b64encode(encrypted) ])
def aesDecrypt(iv, encrypted_text, key): encrypted_text = encrypted_text.decode("base64") iv = iv.decode("base64") text = None ctx = pyelliptic.Cipher(key.decode("base64"), iv, 0, ciphername='aes-256-cbc') try: decrypted = ctx.ciphering(encrypted_text) if decrypted and decrypted.decode("utf8"): # Valid text decoded return decrypted except Exception, err: pass
def aesEncrypt(text, key=None, iv=None): if key: key = key.decode("base64") else: key = os.urandom(32) if iv: # Generate new AES key if not definied iv = iv.decode("base64") else: iv = pyelliptic.Cipher.gen_IV('aes-256-cbc') if text: encrypted = pyelliptic.Cipher(key, iv, 1, ciphername='aes-256-cbc').ciphering(text.encode("utf8")) else: encrypted = "" return (base64.b64encode(key), base64.b64encode(iv), base64.b64encode(encrypted))
def encrypt(data, pubkey, ephemcurve=None, ciphername='aes-256-cbc'): from lib import pyelliptic curve, pubkey_x, pubkey_y, i = pyelliptic.ECC._decode_pubkey(pubkey) if ephemcurve is None: ephemcurve = curve ephem = pyelliptic.ECC(curve=ephemcurve) key = hashlib.sha512(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest() key_e, key_m = key[:32], key[32:] pubkey = ephem.get_pubkey() iv = pyelliptic.OpenSSL.rand( pyelliptic.OpenSSL.get_cipher(ciphername).get_blocksize()) ctx = pyelliptic.Cipher(key_e, iv, 1, ciphername) ciphertext = iv + pubkey + ctx.ciphering(data) mac = pyelliptic.hmac_sha256(key_m, ciphertext) return key_e, ciphertext + mac