Created
September 26, 2012 18:44
-
-
Save alsotang/3789764 to your computer and use it in GitHub Desktop.
一个简单的AES加密库
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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