Skip to content Skip to sidebar Skip to footer

Python AES Encryption Without Extra Module

Is it possible to encrypt/decrypt data with AES without installing extra modules? I need to send/receive data from C#, which is encrypted with the System.Security.Cryptography refe

Solution 1:

I'm using Cryptography library.

Cryptography is an actively developed library that provides cryptographic recipes and primitives. It supports Python 2.6-2.7, Python 3.3+ and PyPy.

Here is an example of how to use that library:

>>> import os
>>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
>>> from cryptography.hazmat.backends import default_backend
>>> backend = default_backend()
>>> key = os.urandom(32)
>>> iv = os.urandom(16)
>>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
>>> encryptor = cipher.encryptor()
>>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
>>> decryptor = cipher.decryptor()
>>> decryptor.update(ct) + decryptor.finalize()
'a secret message'

Solution 2:

PYAES should work with any version of Python3.x. No need to modify the library.

Here is a complete working example for pyaes CTR mode for Python3.x (https://github.com/ricmoo/pyaes)

import pyaes

# A 256 bit (32 byte) key
key = "This_key_for_demo_purposes_only!"
plaintext = "Text may be any length you wish, no padding is required"

# key must be bytes, so we convert it
key = key.encode('utf-8')

aes = pyaes.AESModeOfOperationCTR(key)    
ciphertext = aes.encrypt(plaintext)

# show the encrypted data
print (ciphertext)

# DECRYPTION
# CRT mode decryption requires a new instance be created
aes = pyaes.AESModeOfOperationCTR(key)

# decrypted data is always binary, need to decode to plaintext
decrypted = aes.decrypt(ciphertext).decode('utf-8')

# True
print (decrypted == plaintext)

Let me know if you get any errors


Solution 3:

The available Cryptographic Services available in the Standard Library are those. As you can see AES is not listed, but is suggest to use pycrypto which is an extra module.

You just have to install it using pip, or easy_install and then as showed in pycrypto's page:

from Crypto.Cipher import AES
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
message = "The answer is no"
print obj.encrypt(message)

The only other way without using an extra module would be to code the function yourself, but what's the difference of downloading an extra module and use that instead?

If you want a pure Python implementation of AES that you can download and import check pyaes.


Solution 4:

Here is a self-contained implementation of AES compatible with Python 3.

Example usage:

aesmodal = AESModeOfOperation() 
key = [143,194,34,208,145,203,230,143,177,246,97,206,145,92,255,84]
iv = [103,35,148,239,76,213,47,118,255,222,123,176,106,134,98,92]

size = aesmodal.aes.keySize["SIZE_128"]

mode,orig_len,ciphertext = aesmodal.encrypt("Hello, world!", aesmodal.modeOfOperation["OFB"], key, size, iv)
print(ciphertext)
plaintext = aesmodal.decrypt(ciphertext, orig_len, mode, key, size, iv)
print(plaintext)

Solution 5:

To add to @enrico.bacis' answer: AES is not implemented in the standard library. It is implemented in the PyCrypto library, which is stable and well tested. If you need AES, add PyCrypto as a dependency of your code.

While the AES primitives are, in theory, simple enough that you could write an implementation of them in pure Python, it is strongly recommended that you not do so. This is the first rule of crypto: don't implement it yourself. In particular, if you're just rolling your own crypto library, you'll almost certainly leave yourself open to some kind of side-channel attack.


Post a Comment for "Python AES Encryption Without Extra Module"