I'm currently trying to translate a simple AES code from C# to Python. I know both of these languages pretty well, but I have no idea about the encryption field (especially AES). I wrote this AES code before in C#, but now I have no idea how to make it work in Python (I'm using PyCrypto since Python2.7 does not have built-in AES). Below is my C# code:
using System.Collections;
using System.Text;
using System.Security.Cryptography;
namespace DefaultClasses
{
public class SimpleAES
{
private const string KEY = "someKey";
private const string IV = "someIV";
private AesCryptoServiceProvider _aes;
private ICryptoTransform _crypto;
public SimpleAES()
{
_aes = new AesCryptoServiceProvider();
_aes.BlockSize = 128;
_aes.KeySize = 256;
_aes.Key = ASCIIEncoding.ASCII.GetBytes(KEY);
_aes.IV = ASCIIEncoding.ASCII.GetBytes(IV);
_aes.Padding = PaddingMode.PKCS7;
_aes.Mode = CipherMode.CBC;
}
public string encrypt(string message)
{
_crypto = _aes.CreateEncryptor(_aes.Key, _aes.IV);
byte[] encrypted = _crypto.TransformFinalBlock(
ASCIIEncoding.ASCII.GetBytes(message), 0, ASCIIEncoding.ASCII.GetBytes(message).Length);
_crypto.Dispose();
return System.Convert.ToBase64String(encrypted);
}
public string decrypt(string message)
{
_crypto = _aes.CreateDecryptor(_aes.Key, _aes.IV);
byte[] decrypted = _crypto.TransformFinalBlock(
System.Convert.FromBase64String(message), 0, System.Convert.FromBase64String(message).Length);
_crypto.Dispose();
return ASCIIEncoding.ASCII.GetString(decrypted);
}
}
}
Please note that I want also to have BlockSize = 128, KeySize = 256, Padding = PKCS7, and Cipher CBC for Python. Thanks in advances!