Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello fellow coders,
I have recently been doing some work with cryptography in C#. I compiled my program to test the algorithm and I got an error message from JIT saying:
"Padding is invalid and cannot be removed"
I have looked at many other questions on the internet about this, and most of them say to use the FlushFinalBlock method on the CryptoStrea, but this isn't working for me. Here is my code:
C#
public static string encodeAES(string input, string key)
{
	MemoryStream mStream = new MemoryStream();
	AesManaged crypto = new AesManaged();
	CryptoStream cStream = new CryptoStream(mStream, crypto.CreateEncryptor(getEncryptionKey(key, keyLength_AES), crypto.IV), CryptoStreamMode.Write);// getEncryptionKey is defined elsewhere and returns a key formatted for AES based of the user's key
	byte[] toEncrypt = new ASCIIEncoding().GetBytes(input); // TODO
	cStream.Write(toEncrypt, 0, toEncrypt.Length);
	cStream.FlushFinalBlock();
	byte[] ret = mStream.ToArray();
	cStream.Close();
	mStream.Close();
	return new ASCIIEncoding().GetString(ret);
}
public static string decodeAES(string input, string key)
{
	MemoryStream msDecrypt = new MemoryStream(GetBytes(input));
	AesManaged crypto = new AesManaged();
	CryptoStream csDecrypt = new CryptoStream(msDecrypt, crypto.CreateDecryptor(getEncryptionKey(key, keyLength_AES), crypto.IV), CryptoStreamMode.Read);// getEncryptionKey is defined elsewhere and returns a key formatted for AES based of the user's key
	byte[] fromEncrypt = new byte[input.Length];
	csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
	return new ASCIIEncoding().GetString(fromEncrypt);
}

It seems as though I have done everything right, but I am still getting an error from the decodeAES method. Any ideas/solutions? Thank you very much for your time!

Cheers!
Posted

1 solution

The problem is almost certainly the final part of your method:
C#
return new ASCIIEncoding().GetString(ret);
Converting a byte array to a string using any form of encoding is likely to give you a string that can't be converted back to the original array of bytes.

Try it: instead of returning a string, use your code to generate the string, then use your complementary code to immediately generate a "unstringed" byte array from that.
Then compare each byte in the original with it's equivalent in the "regenerated" version. One single bit different will be enough to scupper the whole operation.
 
Share this answer
 
Comments
FelisPhasma 19-Jul-15 11:29am    
Would base64 encoding work?
OriginalGriff 19-Jul-15 11:46am    
Probably, yes - but the resulting data would be quite large - four times the size of the original encrypted array!

Why do you want strings? Encrypted data is normally passed around as binary (byte) data rather than as characters.
FelisPhasma 19-Jul-15 11:54am    
The main reason is that it will be passed between users as a string.
OriginalGriff 19-Jul-15 12:10pm    
Eh? They are going to type it in? :laugh:
Send 'em a binary file - that doesn't encourage people to open it in a word processor and completely muck it up...
FelisPhasma 19-Jul-15 12:13pm    
Lol, not quite like that. Perhaps I should have just said that it will be displayed to the user ascii form. I suppose I will find a way to convert the AES output to ASCII, and the other algorithms over to byte arrays rather than strings. Thanks for your help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900