10 examples of 'python aes encryption' in Python

Every line of 'python aes encryption' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
134def encrypt_AES(key, plain_text, IV456):
135 """
136 AES encryption routine
137 """
138 obj = AES.new(key, AES.MODE_CBC, IV456)
139 return obj.encrypt(plain_text)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

157def aes_encrypt(key: bytes, plain_text: bytes) -> bytes:
158 """
159 AES-GCM encryption
160
161 Parameters
162 ----------
163 key: bytes
164 AES session key, which derived from two secp256k1 keys
165 plain_text: bytes
166 Plain text to encrypt
167
168 Returns
169 -------
170 bytes
171 nonce(16 bytes) + tag(16 bytes) + encrypted data
172 """
173 aes_cipher = AES.new(key, AES_CIPHER_MODE)
174
175 encrypted, tag = aes_cipher.encrypt_and_digest(plain_text) # type: ignore
176 cipher_text = bytearray()
177 cipher_text.extend(aes_cipher.nonce) # type: ignore
178 cipher_text.extend(tag)
179 cipher_text.extend(encrypted)
180 return bytes(cipher_text)
19def encrypt(data: bytes, key: bytes) -> bytes:
20 # pad data
21 data = Padding.pad(data, 16)
22
23 # new encryptor
24 encryptor = AES.new(key, AES.MODE_CBC)
25
26 # return IV + cipher
27 return encryptor.iv + encryptor.encrypt(data)
77def encrypt(message, key):
78 '''Encrypt a message.'''
79
80 # Generate a random nonce.
81 nonce = _get_random_bytes(_GCM_NONCE_SIZE)
82
83 # Load the AES key.
84 aead = AES.new(key, AES.MODE_GCM, nonce)
85
86 # Encrypt the message.
87 ciphertext, tag = aead.encrypt_and_digest(message)
88
89 return nonce + ciphertext + tag
68def aes(data=''):
69 return self.aes_enc.update(data)
39def aes_encrypt_str(aes_key, plain_str):
40 if isinstance(aes_key, long):
41 aes_key = long_to_bytes(aes_key, 16)
42 cipher = AES.new(aes_key, AES.MODE_CFB, '\x00' * 16)
43 return cipher.encrypt(plain_str)
76def set_aes():
77 global aes
78 if aes is None:
79 # shoulds never fail
80 print "[i] Setting AES key.......",
81 try:
82 aes = AES.new(key, AES.MODE_ECB)
83 print "OK"
84 except Exception, e:
85 print "ERROR: %s" % e.msg
86 sys.exit(-4)
47def Crypt(key, data, iv_orig):
48 assert len(iv_orig) == 16
49 iv = bytes(iv_orig)
50 cipher = AES.new(bytes(key))
51 out = [0] * len(data)
52 for i in range(len(data)):
53 if i % 16 == 0:
54 iv = cipher.encrypt(iv)
55 out[i] = data[i] ^ iv[i % 16]
56 return out
70@staticmethod
71def encrypt(key, raw):
72 assert type(raw) == bytes, "input data is bytes"
73 if isinstance(key, str):
74 key = b64decode(key.encode())
75 raw = pad(raw, AES.block_size)
76 iv = Random.new().read(AES.block_size)
77 cipher = AES.new(key, AES.MODE_CBC, iv)
78 return iv + cipher.encrypt(raw)
37def encryptECB(key, data):
38 inBlock = tools.addPadding(data)
39 cipher = AES.new(key, AES.MODE_ECB)
40 return cipher.encrypt(inBlock)

Related snippets