Hi i am learning Encryption / Decryption part. I have created two methods for Encryption / Decryption using AES 128 bit with ECB cipher mode and PKCS7 padding.
Below is the code.
public class EncClass
{
public string Encrypt(string text)
{
byte[] src = Encoding.UTF8.GetBytes(text);
byte[] key = Encoding.ASCII.GetBytes("contactcentre");
RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
using (ICryptoTransform encrypt = aes.CreateEncryptor(key, null))
{
byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
encrypt.Dispose();
return Convert.ToBase64String(dest);
}
}
public string Decrypt(string text)
{
byte[] src = Convert.FromBase64String(text);
RijndaelManaged aes = new RijndaelManaged();
byte[] key = Encoding.ASCII.GetBytes("contactcentrT");
aes.KeySize = 128;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.ECB;
using (ICryptoTransform decrypt = aes.CreateDecryptor(key, null))
{
byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
decrypt.Dispose();
return Encoding.UTF8.GetString(dest);
}
}
}
Notice that in Encryption i have passed contactcentre key and in decryption I have passed contactcentrT. It is doing proper encryption and decryption in that case.
var encString = encClass.Encrypt(@"manoj");
var decString = encClass.Decrypt(encString);
Though my both keys are not matching, still it is working properly. Just wanted to know how this could happen?