def decrypt(encryptPassword): # 解密函数 base64Decoded = base64.b64decode(encryptPassword) bs = AES.block_size unpad = lambda s: s[0:-s[-1]] iv = base64Decoded[:bs] key = base64Decoded[-32:] cipher = AES.new(key, AES.MODE_CBC, iv) originalPassword = unpad(cipher.decrypt(base64Decoded[:-32]))[bs:] return originalPassword
def encrypt(originalPassword): # 加密函数 bs = AES.block_size pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs) paddPassword = pad(originalPassword) iv = Random.OSRNG.new().read(bs) key = os.urandom(32) cipher = AES.new(key, AES.MODE_CBC, iv) encryptPassword = base64.b64encode(iv + cipher.encrypt(paddPassword) + key) return encryptPassword
def _aes256cbc_encrypt(key, iv, plaintext): cipher = _AES.new(key, _AES.MODE_CBC, iv) ciphertext = cipher.encrypt(plaintext) return ciphertext
def pycrypto_aes_ctr(key, index): from Crypto.Cipher import AES from Crypto.Util import Counter ctr = Counter.new(128, initial_value=index) return AES.new(key, AES.MODE_CTR, counter=ctr)
def pycrypto_aes_ecb(key): from Crypto.Cipher import AES return AES.new(key, AES.MODE_ECB)