I saw that exists another questions about this topic, but I checked every question and I can't solve my problem..
This is my method to decrypt and another method to call decrypt method with required parameters:
public string Decrypt(AesOperationType operationType, byte[] criptotext, byte[] Key, byte[] initVector)
{
string plaintext = null;
using (Aes aesAlg = Aes.Create())
{
aesAlg.KeySize = 128;
aesAlg.Key = Key;
aesAlg.IV = initVector;
if (operationType == AesOperationType.Cbc)
{
aesAlg.Mode = CipherMode.CBC;
}
else if (operationType == AesOperationType.Cfb)
{
aesAlg.Mode = CipherMode.ECB;
}
//apelam functia de decriptare
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(criptotext))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
Console.WriteLine("Start decrypt for criptotext : " + BitConverter.ToString(criptotext) + "\n");
Console.WriteLine("Plaintext after decrypt : " + plaintext + "\n");
return plaintext;
}
public byte[] Encrypt_Call()
{
var key = "1212121212121212";
var key_byte = Encoding.ASCII.GetBytes(key);
using (Aes aess = Aes.Create())
{
var iv = aess.IV;
cryptdecrypt object = new cryptdecrypt();
var result = object.Encrypt(AesOperationType.Cbc, "plaintext", key_byte, iv);
return result;
}
}
public void Decrypt_Call()
{
var key = "1212121212121212";
var key_byte = Encoding.ASCII.GetBytes(key);
using (Aes aess = Aes.Create())
{
var iv = aess.IV;
cryptdecrypt object = new cryptdecrypt();
var cryptotext = Encrypt_Call();
var result = object.Decrypt(AesOperationType.Cbc, cryptotext , key_byte, iv);
}
}
Encrypt method works fine, but at decryption method call, I face this error:
Padding is invalid and cannot be removed.
I also tried to put csDecrypt.FlushFinalBlock() this line before this line:
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
The error disappears and as result I get an empty string.
Any ideas to solve this?
Typically an invalid padding error means the decryption failed. In this case with CBC mode the IV is not specified so it will be junk (or random).
Either:
Specify an IV of block length (AES 16-bytes).
create a random IV on encryption and prepend it to the encrypted data. On decryption split the IV off and use for decryption. <–– Best option
Related
I'm getting "Specified initialization vector (IV) does not match the block size for this algorithm" when trying to decrypt on C# using System.Security.Cryptography.AES.
I encrypt with Crypto-JS:
var key = CryptoJS.MD5("secret");
var iv = CryptoJS.MD5("deviceID");
encrypted = CryptoJS.AES.encrypt(pass, key, {iv:iv, mode:CryptoJS.mode.CBC, padding:CryptoJS.pad.Pkcs7 }).toString();
decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv:iv, mode:CryptoJS.mode.CBC, padding:CryptoJS.pad.Pkcs7 }).toString(CryptoJS.enc.Utf8);
Decrypt with C#:
byte[] iv = Encoding.UTF8.GetBytes(hexIv);
byte[] key = Encoding.UTF8.GetBytes(hexKey);
byte[] cipher = Encoding.UTF8.GetBytes(sCipher);
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv; // HERE I GOT ERROR (IV) does not match the block size
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipher))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
try
{
decrypted = srDecrypt.ReadToEnd();
}
catch (Exception err)
{
throw;
}
}
}
}
}
C# iv is a 32 bytes size
Both IV's are the same MD5 hash
AES Object Screenshot
I have below code written in .net framework to decrypt encrypted text and it is working fine.
private static string MD5(string testString)
{
byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(testString);
byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
return hashedString;
}
public static string DecryptString(string cypherText, string encryptionKey)
{
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
byte[] key = ASCIIEncoding.ASCII.GetBytes(MD5(encryptionKey));
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
string plaintext;
using (var rijAlg = new RijndaelManaged())
{
rijAlg.BlockSize = 256;
rijAlg.Key = key;
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.Zeros;
rijAlg.IV = ASCIIEncoding.ASCII.GetBytes(MD5(MD5(_encryptionKey)));
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
using (var msDecrypt = new MemoryStream(cipherTextBytes))
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (var srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
}
return plaintext;
}
Above code never worked and was returning some junk characters when the code was migrated to .net 5. With some research and help from fellow community member I got the below updated code that works.
public static string DecryptUsingBouncyCastle(string cypherText, string keyString)
{
byte[] key = ASCIIEncoding.ASCII.GetBytes(MD5(keyString));
byte[] cipherTextBytes = Convert.FromBase64String(cypherText);
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
var ivStringBytes = ASCIIEncoding.ASCII.GetBytes(MD5(MD5(keyString)));
var engine = new RijndaelEngine(256);
var blockCipher = new CbcBlockCipher(engine);
var cipher = new PaddedBufferedBlockCipher(blockCipher, new ZeroBytePadding());
var keyParam = new KeyParameter(key);
var keyParamWithIV = new ParametersWithIV(keyParam, ivStringBytes, 0, ivStringBytes.Length);
cipher.Init(false, keyParamWithIV);
var outputBytes = new byte[cipher.GetOutputSize(cipherTextBytes.Length)];
var length = cipher.ProcessBytes(cipherTextBytes, outputBytes, 0);
length += cipher.DoFinal(outputBytes, length);
var resultText = Encoding.UTF8.GetString(outputBytes, 0, length);
return resultText;
}
I was cautioned that AES should be preferred over Rijndael with 256 bits block size. With that in mind I tried to write the decrypt method using AES but I could not succeed. It is not throwing any error but returning some garbage text.
public static string DecryptStringFromAES(string cypherText, string keyString)
{
byte[] key = ASCIIEncoding.ASCII.GetBytes(MD5(keyString));
byte[] cipherText = Convert.FromBase64String(cypherText);
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
string plaintext;
using (var aes = Aes.Create())
{
aes.BlockSize = 128;
aes.Key = key;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros;
aes.IV = new byte[16];
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (var msDecrypt = new MemoryStream(cipherText))
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (var srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
}
return plaintext;
}
I am not an expert in Cryptography and I don't really know what is wrong in the above code. My job demanded this task to decrypt some encrypted information from Active Directory. Encryption is done by some other process and I can't share the encryption key and don't have test code as we don't have a test environment.
Appreciate if someone could look at the code and see if I messed up anything with that code and suggest any correction that could possibly fix the issue. Thanks in advance.
We have implemented AES encryption in our project. And it works fine.
But, if the user tampers the byte[] then the Decrypt function returns a wrong plain text with different symbols like � in it.
We want to handle this case by determining that the data is tampered.
Please find below the code of Decrypt function:
public static string Decrypt(string encryptedText)
{
try
{
// First convert the base64 string to byte[].
var cipherText = Convert.FromBase64String(encryptedText);
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
string plaintext = null;
// Create an Aes object
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Encoding.ASCII.GetBytes("abc");
aesAlg.IV = Encoding.ASCII.GetBytes("xyz");
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
catch (Exception ex)
{
return null;
}
}
Please help me to solve my issue.
Thanks!
I make a chat application and I use AES algorithm. But I see this error " the input data is not complete block".
This is my code:
public static string DecryptStringFromBytes_Aes(byte[] cipherText, string key, string mode)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
if (mode == "ECB") aesAlg.Mode = CipherMode.ECB;
else if (mode == "CBC") aesAlg.Mode = CipherMode.CBC;
else if (mode == "CFB") aesAlg.Mode = CipherMode.CFB;
aesAlg.Key = Convert.FromBase64String(key);
aesAlg.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 16));
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
The error is in line: plaintext = srDecrypt.ReadToEnd();
I don't know why.
Help me, please. Thank you very much.
Hello Friendly OverFlowers:
I have a line of code in the bigger example that is not working:
plaintext = srDecrypt.ReadToEnd();
It reports an exception:
The input data is not a complete block.
I have:
1) Looked at Encodings
2) Verified Decrypt (args) were correct.
Oh the intention from the simple main was to get back the encrypted value from the decrypted value. The plaintext = line is in the Decryption portion.
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Encryptor
{
class Program
{
static void Main(string[] args)
{
CryptDecrypt cd = new CryptDecrypt(new Guid());
string s = cd.Encrypt("Password");
Console.WriteLine(s);
string t = cd.Decrypt(s);
Console.WriteLine(t);
Console.ReadKey();
}
}
public class CryptDecrypt
{
private byte[] Key;
private byte[] IV;
public CryptDecrypt(Guid keyBase)
{
string Hash = keyBase.ToString();
Key = Encoding.UTF8.GetBytes(Hash.Take(32).ToArray());
IV = Encoding.UTF8.GetBytes(Hash.Reverse().Take(16).ToArray());
}
public string Encrypt(string plainText)
{
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.IV = IV;
aesAlg.Key = IV;
aesAlg.Padding = PaddingMode.Zeros;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return Convert.ToBase64String(encrypted);
}
public string Decrypt(string inputStr)
{
// Check arguments.
if (inputStr == null || inputStr.Length <= 0)
throw new ArgumentNullException("cipherText");
byte[] cipherText = Encoding.UTF8.GetBytes(inputStr);
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.Padding = PaddingMode.Zeros;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}
You have two errors. First is that you use IV as the Key in the Encrypt method, and second you forgot to convert back from Base64 before decrypting.
See the code amended to correct these problems.
void Main()
{
CryptDecrypt cd = new CryptDecrypt(new Guid());
string s = cd.Encrypt("Password");
Console.WriteLine(s);
string t = cd.Decrypt(s);
Console.WriteLine(t);
}
public class CryptDecrypt
{
private byte[] Key;
private byte[] IV;
public CryptDecrypt(Guid keyBase)
{
string Hash = keyBase.ToString();
Key = Encoding.UTF8.GetBytes(Hash.Take(32).ToArray());
IV = Encoding.UTF8.GetBytes(Hash.Reverse().Take(16).ToArray());
}
public string Encrypt(string plainText)
{
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.IV = IV;
aesAlg.Key = Key; <- HERE
aesAlg.Padding = PaddingMode.Zeros;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
swEncrypt.Flush();
}
encrypted = msEncrypt.ToArray();
}
}
}
return Convert.ToBase64String(encrypted);
}
public string Decrypt(string inputStr)
{
// Check arguments.
if (inputStr == null || inputStr.Length <= 0)
throw new ArgumentNullException("cipherText");
byte[] cipherText = Convert.FromBase64String(inputStr); <- HERE
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.Padding = PaddingMode.Zeros;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}