I need to decrypt a cipher that was encrypted with AES CBC 128 with PKCS1 padding. But, C# AesCryptoServiceProvider and System.Security.Cryptography.PaddingMode does not have PKCS1.

What should I do? Do I have to use some other AES package? or, implement my own...

Please help...

2

There are 2 answers

0
Tom On

Fixed. Changed from

AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = Encoding.ASCII.GetBytes(key);

to

AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = Convert.FromBase64String(key);
0
Alireza Farokhi On

actually had the same problem but your suggestion will cause compile error, this worked for me.

AesCryptoServiceProvider aesCipher = new AesCryptoServiceProvider()
string keyBase64 = Convert.ToBase64String(secretKey.Key); // secretKey is previusly generated AES Key using  Aes aes = Aes.Create(); and aes.GenerateKey();
byte[] textBytes = Encoding.ASCII.GetBytes(keyBase64);
aesCipher.Key = textBytes;
aesCipher.Mode = CipherMode.ECB;
aesCipher.Padding = PaddingMode.PKCS7;

and finally:

// Encrypt
byte[] payloadBytes = Encoding.UTF8.GetBytes(text); // payload for encryption
var payloadEncryptedBytes = aesCipher.CreateEncryptor()
            .TransformFinalBlock(payloadBytes, 0, payloadBytes.Length);

// Encode
return Convert.ToBase64String(payloadEncryptedBytes);