Пример #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 encrypt_with_gcm_mode(plain_text: bytes, hdr: bytes,
                           public_key: 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)
     nonce, mac_tag, cipher_text = AESHandler.aes_gcm_encrypt(
         plain_text, hdr, aes_key)
     return nonce, mac_tag, encode_g_tilde, cipher_text
Пример #3
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