我是python/django的新手。我想使用AES加密。搜索后,我找到了一些这样的库:https://gist.github.com/jeetsukumaran/1291836但是由于我有IV,我无法使用它们,但这没有静脉代码。我在C#中有示例代码。有人可以帮助我将此代码转换为Python代码?
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace AESUtility {
public class AES {
string AES_Key = string.Empty;
string AES_IV = string.Empty;
public AES(string AES_Key, string AES_IV) {
this.AES_Key = AES_Key;
this.AES_IV = AES_IV;
}
public bool Encrypt(String Input, out string encryptedString) {
try {
var aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Convert.FromBase64String(this.AES_Key);
aes.IV = Convert.FromBase64String(this.AES_IV);
var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
byte[] xBuff = null;
using(var ms = new MemoryStream()) {
using(var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write)) {
byte[] xXml = Encoding.UTF8.GetBytes(Input);
cs.Write(xXml, 0, xXml.Length);
}
xBuff = ms.ToArray();
}
encryptedString = Convert.ToBase64String(xBuff);
return true;
} catch (Exception ex) {
encryptedString = string.Empty;
return false;
}
}
public bool Decrypt(String Input, out string decodedString) {
try {
RijndaelManaged aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Convert.FromBase64String(this.AES_Key);
aes.IV = Convert.FromBase64String(this.AES_IV);
var decrypt = aes.CreateDecryptor();
byte[] xBuff = null;
using(var ms = new MemoryStream()) {
using(var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write)) {
byte[] xXml = Convert.FromBase64String(Input);
cs.Write(xXml, 0, xXml.Length);
}
xBuff = ms.ToArray();
}
decodedString = Encoding.UTF8.GetString(xBuff);
return true;
} catch (Exception ex) {
decodedString = string.Empty;
return false;
}
}
}
}
编辑2022
pycrypto现在可能是不受欢迎的。最新版本是2013年10月(请参阅https://pypi.org/project/pycrypto/#history(
在现代项目中,使用pycryptodome。官方文档给出了AES加密的示例:https://www.pycryptodome.org/en/latest/src/src/examples.html#encrypt-data-with-aes-aes
原始答案:
这是使用众所周知的pycrypto库的AES加密的示例:
>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'xd6x83x8dd!VTx92xaa`Ax05xe0x9bx8bxf1'
>>> obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'
AES 256在OFB模式下:
from Crypto.Cipher import AES
from Crypto.Random import new as Random
from hashlib import sha256
from base64 import b64encode,b64decode
class AESCipher:
def __init__(self,data,key):
self.block_size = 16
self.data = data
self.key = sha256(key.encode()).digest()[:32]
self.pad = lambda s: s + (self.block_size - len(s) % self.block_size) * chr (self.block_size - len(s) % self.block_size)
self.unpad = lambda s: s[:-ord(s[len(s) - 1:])]
def encrypt(self):
plain_text = self.pad(self.data)
iv = Random().read(AES.block_size)
cipher = AES.new(self.key,AES.MODE_OFB,iv)
return b64encode(iv + cipher.encrypt(plain_text.encode())).decode()
def decrypt(self):
cipher_text = b64decode(self.data.encode())
iv = cipher_text[:self.block_size]
cipher = AES.new(self.key,AES.MODE_OFB,iv)
return self.unpad(cipher.decrypt(cipher_text[self.block_size:])).decode()
用法:
AESCipher("data to encrypt","key to use").encrypt()
AESCipher("encrypted data","key to use").decrypt()