Skip to content

Instantly share code, notes, and snippets.

@naixwf
Created October 24, 2014 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save naixwf/6a4174becd47e6f2e63b to your computer and use it in GitHub Desktop.
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)); ```
# -*- 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
@naixwf
Copy link
Author

naixwf commented Oct 24, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment