def get_gcm_decoded_private_key(encrypted_key_str: str, password: str, b58_address: str, salt: str, n: int, scheme: SignatureScheme) -> str: """ This interface is used to decrypt an private key which has been encrypted. :param encrypted_key_str: an gcm encrypted private key in the form of string. :param password: the secret pass phrase to generate the keys from. :param b58_address: a base58 encode address which should be correspond with the private key. :param salt: a string to use for better protection from dictionary attacks. :param n: CPU/memory cost parameter. :param scheme: the signature scheme. :return: a private key in the form of string. """ r = 8 p = 8 dk_len = 64 scrypt = Scrypt(n, r, p, dk_len) derivedkey = scrypt.generate_kd(password, salt) iv = derivedkey[0:12] derivedhalf2 = derivedkey[32:64] encrypted_key = base64.b64decode(encrypted_key_str).hex() mac_tag = a2b_hex(encrypted_key[64:96]) cipher_text = a2b_hex(encrypted_key[0:64]) private_key = AESHandler.aes_gcm_decrypt_with_iv( cipher_text, b58_address.encode(), mac_tag, derivedhalf2, iv) if len(private_key) == 0: raise SDKException(ErrorCode.decrypt_encrypted_private_key_error) private_key = b2a_hex(private_key).decode('ascii') acct = Account(private_key, scheme) if acct.get_address().b58encode() != b58_address: raise RuntimeError return private_key
def test_aes_gcm_with_iv(self): key = b'Sixteen byte key' plain_text = b'Attack at dawn' hdr = b'To your eyes only' iv = Random.new().read(AES.block_size) mac, cipher_text = AESHandler.aes_gcm_encrypt_with_iv(plain_text, hdr, key, iv) decrypt_out = AESHandler.aes_gcm_decrypt_with_iv(cipher_text, hdr, mac, key, iv) self.assertEqual(plain_text, decrypt_out)
def get_gcm_decoded_private_key(encrypted_key_str: str, password: str, address: str, salt: str, n: int, scheme: SignatureScheme) -> str: r = 8 p = 8 dk_len = 64 scrypt = Scrypt(n, r, p, dk_len) derivedkey = scrypt.generate_kd(password, salt) iv = derivedkey[0:12] derivedhalf2 = derivedkey[32:64] encrypted_key = base64.b64decode(encrypted_key_str).hex() mac_tag = a2b_hex(encrypted_key[64:96]) cipher_text = a2b_hex(encrypted_key[0:64]) pri_key = AESHandler.aes_gcm_decrypt_with_iv(cipher_text, address.encode(), mac_tag, derivedhalf2, iv) pri_key = b2a_hex(pri_key).decode('ascii') acct = Account(pri_key, scheme) if acct.get_address().b58encode() != address: raise RuntimeError return pri_key