Skip to content

Instantly share code, notes, and snippets.

@tomleo
Created February 6, 2014 19:32
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 tomleo/8851022 to your computer and use it in GitHub Desktop.
Save tomleo/8851022 to your computer and use it in GitHub Desktop.
Python symmetric encryption with CRC
# This code is via http://www.turnkeylinux.org/blog/python-symmetric-encryption
# Author: Alon Swartz
import zlib
import struct
from Crypto.Cipher import AES
class CheckSumError(Exception):
pass
def _lazysecret(secret, blocksize=32, padding='}'):
"""pads secret if not legal AES block size (16, 24, 32)"""
if not len(secret) in (16, 24, 32):
return secret + (blocksize - len(secret)) * padding
return secret
def encrypt(plaintext, secret, lazy=True, checksum=True):
"""encrypt plaintext with secret
plaintext - content to encrypt
secret - secret to encrypt plaintext
lazy - pad secret if less than legal blocksize (default: True)
checksum - attach crc32 byte encoded (default: True)
returns ciphertext
"""
secret = _lazysecret(secret) if lazy else secret
encobj = AES.new(secret, AES.MODE_CFB)
if checksum:
plaintext += struct.pack("i", zlib.crc32(plaintext))
return encobj.encrypt(plaintext)
def decrypt(ciphertext, secret, lazy=True, checksum=True):
"""decrypt ciphertext with secret
ciphertext - encrypted content to decrypt
secret - secret to decrypt ciphertext
lazy - pad secret if less than legal blocksize (default: True)
checksum - verify crc32 byte encoded checksum (default: True)
returns plaintext
"""
secret = _lazysecret(secret) if lazy else secret
encobj = AES.new(secret, AES.MODE_CFB)
plaintext = encobj.decrypt(ciphertext)
if checksum:
crc, plaintext = (plaintext[-4:], plaintext[:-4])
if not crc == struct.pack("i", zlib.crc32(plaintext)):
raise CheckSumError("checksum mismatch")
return plaintext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment