コード例 #1
0
 def test_aes_gcm(self):
     key = b'Sixteen byte key'
     plain_text = b'Attack at dawn'
     hdr = b'To your eyes only'
     nonce, mac, cipher_text = AESHandler.aes_gcm_encrypt(plain_text, hdr, key)
     decrypt_out = AESHandler.aes_gcm_decrypt(cipher_text, hdr, nonce, mac, key)
     self.assertEqual(plain_text, decrypt_out)
コード例 #2
0
 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)
コード例 #3
0
    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
コード例 #4
0
    def export_gcm_encrypted_private_key(self, password: str, salt: str,
                                         n: int) -> str:
        """
        This interface is used to export an AES algorithm encrypted private key with the mode of GCM.

        :param password: the secret pass phrase to generate the keys from.
        :param salt: A string to use for better protection from dictionary attacks.
                      This value does not need to be kept secret, but it should be randomly chosen for each derivation.
                      It is recommended to be at least 8 bytes long.
        :param n: CPU/memory cost parameter. It must be a power of 2 and less than 2**32
        :return: an gcm encrypted 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]
        mac_tag, cipher_text = AESHandler.aes_gcm_encrypt_with_iv(
            self.__private_key,
            self.__address.b58encode().encode(), derivedhalf2, iv)
        encrypted_key = b2a_hex(cipher_text) + b2a_hex(mac_tag)
        encrypted_key_str = base64.b64encode(a2b_hex(encrypted_key))
        return encrypted_key_str.decode()
コード例 #5
0
ファイル: ecies.py プロジェクト: OnyxPayDev/SmartContractDemo
 def encrypt_with_cbc_mode(plain_text: bytes,
                           public_key: bytes,
                           iv: bytes = b'') -> (bytes, bytes, bytes):
     if not isinstance(public_key, bytes):
         raise SDKException(
             ErrorCode.other_error(
                 'the type of public key should be bytes.'))
     if len(public_key) != 33:
         raise SDKException(
             ErrorCode.other_error(
                 'the length of public key should be 33 bytes.'))
     if not (public_key.startswith(b'\x02')
             or public_key.startswith(b'\x03')):
         raise SDKException(ErrorCode.other_error('Invalid public key.'))
     public_key = ECIES.__uncompress_public_key(public_key)
     r = randint(1, NIST256p.order)
     g_tilde = r * NIST256p.generator
     h_tilde = r * VerifyingKey.from_string(string=public_key,
                                            curve=NIST256p).pubkey.point
     str_g_tilde_x = number_to_string(g_tilde.x(), NIST256p.order)
     str_g_tilde_y = number_to_string(g_tilde.y(), NIST256p.order)
     encode_g_tilde = b''.join([b'\x04', str_g_tilde_x, str_g_tilde_y])
     str_h_tilde_x = number_to_string(h_tilde.x(), NIST256p.order)
     seed = b''.join([encode_g_tilde, str_h_tilde_x])
     aes_key = pbkdf2(seed, 32)
     aes_iv, cipher_text = AESHandler.aes_cbc_encrypt(
         plain_text, aes_key, iv)
     return aes_iv, encode_g_tilde, cipher_text
コード例 #6
0
ファイル: ecies.py プロジェクト: OnyxPayDev/SmartContractDemo
 def decrypt_with_gcm_mode(nonce: bytes, mac_tag: bytes, cipher_text: bytes,
                           private_key: bytes, hdr: bytes,
                           encode_g_tilde: bytes):
     if not isinstance(private_key, bytes):
         raise SDKException(
             ErrorCode.other_error(
                 'the length of private key should be 32 bytes.'))
     if len(private_key) != 32:
         raise SDKException(
             ErrorCode.other_error(
                 'the length of private key should be 32 bytes.'))
     str_g_tilde_x = encode_g_tilde[1:33]
     str_g_tilde_y = encode_g_tilde[33:65]
     g_tilde_x = string_to_number(str_g_tilde_x)
     g_tilde_y = string_to_number(str_g_tilde_y)
     g_tilde = Point(NIST256p.curve, g_tilde_x, g_tilde_y, NIST256p.order)
     h_tilde = g_tilde * SigningKey.from_string(
         string=private_key, curve=NIST256p).privkey.secret_multiplier
     seed = b''.join(
         [encode_g_tilde,
          number_to_string(h_tilde.x(), NIST256p.order)])
     aes_key = pbkdf2(seed, 32)
     plain_text = AESHandler.aes_gcm_decrypt(cipher_text, hdr, nonce,
                                             mac_tag, aes_key)
     return plain_text
コード例 #7
0
 def decrypt_with_cbc_mode(cipher_text: bytes, private_key: bytes, iv: bytes, encode_g_tilde: bytes) -> bytes:
     aes_key = ECIES.generate_decrypt_aes_key(private_key, encode_g_tilde)
     try:
         plain_text = AESHandler.aes_cbc_decrypt(cipher_text, iv, aes_key)
     except ValueError as e:
         raise SDKException(ErrorCode.other_error(e.args[0]))
     return plain_text
コード例 #8
0
ファイル: ecies.py プロジェクト: yuexkdx/ontology-python-sdk
 def decrypt_with_gcm_mode(nonce: bytes, mac_tag: bytes, cipher_text: bytes,
                           private_key: bytes, hdr: bytes,
                           encode_g_tilde: bytes):
     aes_key = ECIES.generate_decrypt_aes_key(private_key, encode_g_tilde)
     plain_text = AESHandler.aes_gcm_decrypt(cipher_text, hdr, nonce,
                                             mac_tag, aes_key)
     return plain_text
コード例 #9
0
ファイル: ecies.py プロジェクト: yuexkdx/ontology-python-sdk
 def encrypt_with_cbc_mode(plain_text: bytes,
                           public_key: bytes,
                           iv: bytes = b'') -> (bytes, bytes, bytes):
     aes_key, encode_g_tilde = ECIES.generate_encrypt_aes_key(public_key)
     aes_iv, cipher_text = AESHandler.aes_cbc_encrypt(
         plain_text, aes_key, iv)
     return aes_iv, encode_g_tilde, cipher_text
コード例 #10
0
 def export_gcm_encrypted_private_key(self, password: str, salt: str, n: int):
     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]
     mac_tag, cipher_text = AESHandler.aes_gcm_encrypt_with_iv(self.__private_key,
                                                               self.__address.b58encode().encode(),
                                                               derivedhalf2,
                                                               iv)
     encrypted_key = b2a_hex(cipher_text) + b2a_hex(mac_tag)
     encrypted_key_str = base64.b64encode(a2b_hex(encrypted_key))
     return encrypted_key_str.decode()
コード例 #11
0
 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
コード例 #12
0
 def test_aes_ctr(self):
     key = b'Sixteen byte key'
     plain_text = b'Attack at dawn'
     nonce, cipher_text = AESHandler.aes_ctr_encrypt(plain_text, key)
     decrypt_out = AESHandler.aes_ctr_decrypt(cipher_text, nonce, key)
     self.assertEqual(plain_text, decrypt_out)
コード例 #13
0
 def encrypt_with_gcm_mode(plain_text: bytes, hdr: bytes, public_key: bytes):
     aes_key, encode_g_tilde = ECIES.generate_encrypt_aes_key(public_key)
     nonce, mac_tag, cipher_text = AESHandler.aes_gcm_encrypt(plain_text, hdr, aes_key)
     return nonce, mac_tag, encode_g_tilde, cipher_text