Skip to content

Instantly share code, notes, and snippets.

@alsotang
Created September 26, 2012 18:44
Show Gist options
  • Save alsotang/3789764 to your computer and use it in GitHub Desktop.
Save alsotang/3789764 to your computer and use it in GitHub Desktop.
一个简单的AES加密库
from Crypto.Cipher import AES
import os
BLOCK_SIZE = AES.block_size
key = os.urandom(BLOCK_SIZE)
cipher = AES.new(key)
def pad(data):
len_data = len(data)
return data + ((BLOCK_SIZE - len_data % BLOCK_SIZE)-1) * ' ' + chr(len_data)
def unpad(data):
len_data = ord(data[-1])
return data[-(len_data+1):]
def encrypt(data):
return cipher.encrypt(pad(data))
def decrypt(data):
return cipher.decrypt(unpad(data))
if __name__ == '__main__':
data = 'hello world'
print decrypt(encrypt(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment