Skip to content

Instantly share code, notes, and snippets.

@lbragstad
Last active April 13, 2017 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbragstad/0c5c831d11684f8c7def7a6c553e1c40 to your computer and use it in GitHub Desktop.
Save lbragstad/0c5c831d11684f8c7def7a6c553e1c40 to your computer and use it in GitHub Desktop.
pycrypto->cryptography
import os
import six
import uuid
from Crypto.Cipher import AES
from cryptography.hazmat import backends
from cryptography.hazmat.primitives import ciphers
from cryptography.hazmat.primitives.ciphers import algorithms
from cryptography.hazmat.primitives.ciphers import modes
from cryptography.hazmat.primitives import padding
def _pycrypto_encrypt(key, data, iv):
cipher = AES.new(key, AES.MODE_CBC, iv)
padding = 16 - len(data) % 16
padded_data = data + six.int2byte(padding) * padding
return iv + cipher.encrypt(padded_data)
def _pycrypto_decrypt(key, data):
cipher = AES.new(key, AES.MODE_CBC, data[:16])
result = cipher.decrypt(data[16:])
return result[:-1 * six.byte2int([result[-1]])]
def _cryptography_encrypt(key, data, iv):
cipher = ciphers.Cipher(
algorithms.AES(key),
modes.CBC(iv),
backend=backends.default_backend())
padder = padding.PKCS7(256).padder()
padded_data = padder.update(data) + padder.finalize()
encryptor = cipher.encryptor()
return iv + encryptor.update(padded_data) + encryptor.finalize()
def _cryptography_decrypt(key, data):
cipher = ciphers.Cipher(
algorithms.AES(key),
modes.CBC(data[:16]),
backend=backends.default_backend())
decryptor = cipher.decryptor()
result = decryptor.update(data[16:]) + decryptor.finalize()
unpadder = padding.PKCS7(256).unpadder()
unpadded_data = unpadder.update(result) + unpadder.finalize()
return unpadded_data
def main():
message = b'[Eat my shorts!]'
key = uuid.uuid4().hex
iv = os.urandom(16)
enc_pycrypto = _pycrypto_encrypt(key, message, iv)
print 'pycrypto ciphertext: %r' % enc_pycrypto
enc_cryptography = _cryptography_encrypt(key, message, iv)
print 'cryptography ciphertext: %r' % enc_cryptography
dec_pycrpyto_with_pycrypto = _pycrypto_decrypt(key, enc_pycrypto)
print 'decrypt pycrypto with pycrypto %r' % dec_pycrpyto_with_pycrypto
dec_pycrpyto_with_cryptography = _cryptography_decrypt(key, enc_pycrypto)
print 'decrypt pycrypto with cryptography %r' % dec_pycrpyto_with_cryptography
assert dec_pycrpyto_with_pycrypto == dec_pycrpyto_with_cryptography
dec_crypto_with_pycrypto = _pycrypto_decrypt(key, enc_cryptography)
print 'decrypt cryptography with pycrypto %r' % dec_crypto_with_pycrypto
dec_crypto_with_cryptography = _cryptography_decrypt(key, enc_cryptography)
print 'decrypt cryptography with cryptography %r' % dec_crypto_with_cryptography
assert dec_crypto_with_pycrypto == dec_crypto_with_cryptography
if __name__ == '__main__':
main()
pycrypto ciphertext: '\x8e\x14\xdd\x85\x90\xb1\x95WB\xce#d.\x1c\xca\x94/KzY\xeb\x01\xafF\x80lI\xf0\xe1\xb7t\x00\xb8\xf9\xaeW!\xcd\xfa\xdfG_\x8e\x96\xec\x03m\xf7'
cryptography ciphertext: '\x8e\x14\xdd\x85\x90\xb1\x95WB\xce#d.\x1c\xca\x94/KzY\xeb\x01\xafF\x80lI\xf0\xe1\xb7t\x00\xb8\xf9\xaeW!\xcd\xfa\xdfG_\x8e\x96\xec\x03m\xf7'
decrypt pycrypto with pycrypto '[Eat my shorts!]'
decrypt pycrypto with cryptography '[Eat my shorts!]'
decrypt cryptography with pycrypto '[Eat my shorts!]'
decrypt cryptography with cryptography '[Eat my shorts!]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment