Created
October 24, 2014 02:37
-
-
Save naixwf/6a4174becd47e6f2e63b to your computer and use it in GitHub Desktop.
这个脚本可以兼容java的AES加密解密 ``` Cipher cipher = Cipher.getInstance("AES"); SecretKeySpec keySpec = new SecretKeySpec(ROW_BYTES, "AES"); cipher.init(2, keySpec); byte[] ciphertext = cipher.doFinal(Base64.decodeBase64(content));
```
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
# -*- coding: utf-8 -*- | |
import base64 | |
from Crypto.Cipher import AES | |
BS = AES.block_size | |
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) | |
unpad = lambda s: s[0:-ord(s[-1])] | |
key = '80e36e39f34e678c' | |
cipher = AES.new(key) | |
def encryption(input): | |
encrypted = base64.b64encode(cipher.encrypt(pad(input))) | |
return encrypted | |
def decryption(input): | |
decrypted = unpad(cipher.decrypt(base64.b64decode(input))) | |
return decrypted | |
if __name__ == "__main__": | |
input = '370682198404164421' | |
output = 'vOojyD/p4cG3KVrvxOT8hgrsCJ7NxsOzItQIj6lvuz4=' | |
print encryption(input) == output | |
print decryption(output) == input | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
via http://likang.me/blog/2013/06/05/python-pycrypto-aes-ecb-pkcs-5/