I am writing a console app using .Net 6.0.421 to test around AES-128 CFB mode with BlockSize
of 128 and the same bit count for FeedbackSize
. I have the following code for the decryption. The encryption is very similar bar for aes.CreateEncryptor()
, IV
, and Key
generation.
private static byte[] decrypt(byte[] cipherText, byte[] iv, byte[] key)
{
var aes = Aes.Create()!;
aes.Mode = CipherMode.CFB;
aes.Padding = PaddingMode.Zeros;
aes.BlockSize = 128;
aes.FeedbackSize = 128;
aes.Key = key;
aes.IV = iv;
using var stream = new MemoryStream();
using var cryptos = new CryptoStream(stream, aes.CreateDecryptor(), CryptoStreamMode.Write, true);
cryptos.Write(cipherText, 0, cipherText.Length);
cryptos.Close();
stream.Seek(0, SeekOrigin.Begin);
return stream.ToArray();
}
Both of the encrypt
and decrypt
are used in this way:
byte[] key;
byte[] iv;
string input = "1171713971804";
byte[] plain = Encoding.UTF8.GetBytes(input);
Console.WriteLine("Datattt: "+ input);
byte[] encrypted = encrypt(plain, out iv, out key);
byte[] decrypted = decrypt(encrypted, iv, key);
Console.WriteLine("Data Dec Length : " + decrypted.Length);
Console.WriteLine("Data Dectt: "+ Encoding.UTF8.GetString(decrypted));
You can find the full source on .Net Fiddle but keep in mind that it seems invalid characters are weeded out from the output. The decryption result with PaddingMode.ANSIX923
looks perfect while on PaddingMode.Zeros
have three question marks (possibly invalid characters) despite both decryption outputs 16 bytes of data.
Output with PaddingMode.Zeros
:
Data : 1171713971804
Data Dec Length : 16
Data Dec : 1171713971804???
Output with PaddingMode.ANSIX923
:
Data : 1171713971804
Data Dec Length : 16
Data Dec : 1171713971804
I do curious, what might be the cause for the difference of decryption output for PaddingMode.ANSIX923
and PaddingMode.Zeros
?