Click here to Skip to main content
16,006,768 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionRijndaelManaged Padding is invalid and cannot be removed Pin
Member 906777730-Jul-12 3:28
Member 906777730-Jul-12 3:28 
AnswerRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Dave Kreskowiak31-Jul-12 4:35
mveDave Kreskowiak31-Jul-12 4:35 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Member 90677771-Aug-12 0:02
Member 90677771-Aug-12 0:02 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Pete O'Hanlon1-Aug-12 2:18
mvePete O'Hanlon1-Aug-12 2:18 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Member 90677771-Aug-12 3:53
Member 90677771-Aug-12 3:53 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Pete O'Hanlon1-Aug-12 4:46
mvePete O'Hanlon1-Aug-12 4:46 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Member 90677771-Aug-12 5:40
Member 90677771-Aug-12 5:40 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Dave Kreskowiak1-Aug-12 4:45
mveDave Kreskowiak1-Aug-12 4:45 
You've got a ton of problems with this code.

First, The two lines where you have:
Encoding.Unicode.GetBytes(...);

are getting the bytes all right, but then completely ignoring the returned data and dropping it. These lines do absolutely nothing.

Next, the length of the IV in bytes must be the block size divided by 8, not the key size. This line:
int length = keysize / 0x10;

should be
// Divided by 16 because we're using Unicode encoding.
// If using ASCII or UTF8, this would be divided by 8 instead.
int length = blockSize / 16;


Also, you're using the Unicode encoding to convert a string to bytes. This limits your key string to 16 caharacters (for a 256 bit block size) since Unicode will return 2 bytes for every character in your IV and key strings. You're cutting the maximum size of your source string in half by using it.

I'd also factor out the code that does the string trimming and conversion to bytes with the following. I think it's pretty obvious what it does and how you use it:
public static byte[] UnicodeConvertStringToVector(string source, int sizeInBits)
{
	int maxStringLength = sizeInBits / (UnicodeEncoding.CharSize * 8);

	// Make sure the length of the string in Unicode characters is no longer than
	// allowed by the sizeInBits parameter.
	if (source.Length > maxStringLength) {
		source = source.Substring(0, maxStringLength);
	} else if (source.Length < maxStringLength) {
		// and make sure that it's no shorter than allowed by sizeInBits...
		source = source.PadRight(maxStringLength, "#");
	}

	return Encoding.Unicode.GetBytes(source);
}


And finally, from the point where you declare bytes and rgbIV, your code is just a freaking mess. It's no wonder it doesn't work correctly. I don't know what the hell you're doing in there, but it shouldn't be returning a string. It should be returning an array of bytes.

I'm probably shooting myself in the foot by posting this, but here's the "cleaned up" code for this part:
byte[] keyData = UnicodeConvertStringToVector(myKeyString, keySize);
byte[] ivData = UnicodeConvertStringToVector(myIV, length);
byte[] encryptedData = null;

using (MemoryStream outputStream = new MemoryStream())
{
    using (CryptoStream cryptStream = new CryptoStream(outputStream, managed.CreateEncryptor(keyData, ivData), CryptoStreamMode.Write))
    {
        using (StreamWriter encryptStream = new StreamWriter(cryptStream))
        {
            encryptStream.Write(strInputString);
        }
        encryptedData = outputStream.ToArray;
    }
}

return encryptedData;

Your code might make a bit more sense if you used meaningful variable names instead of the generic "stream1", "stream2" garbage. Also, nobody puts "str" on the front of their variable names anymore.


modified 1-Aug-12 11:24am.

GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Member 90677771-Aug-12 5:36
Member 90677771-Aug-12 5:36 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Dave Kreskowiak1-Aug-12 6:46
mveDave Kreskowiak1-Aug-12 6:46 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Member 90677771-Aug-12 7:10
Member 90677771-Aug-12 7:10 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Dave Kreskowiak1-Aug-12 7:16
mveDave Kreskowiak1-Aug-12 7:16 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Dave Kreskowiak1-Aug-12 7:08
mveDave Kreskowiak1-Aug-12 7:08 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Member 90677771-Aug-12 10:23
Member 90677771-Aug-12 10:23 
GeneralRe: RijndaelManaged Padding is invalid and cannot be removed Pin
Dave Kreskowiak1-Aug-12 12:19
mveDave Kreskowiak1-Aug-12 12:19 
Questionrecommendation for high quality .NET extension library Pin
Southmountain28-Jul-12 10:23
Southmountain28-Jul-12 10:23 
GeneralRe: recommendation for high quality .NET extension library Pin
Ravi Bhavnani28-Jul-12 10:42
professionalRavi Bhavnani28-Jul-12 10:42 
GeneralRe: recommendation for high quality .NET extension library Pin
Southmountain28-Jul-12 10:48
Southmountain28-Jul-12 10:48 
GeneralRe: recommendation for high quality .NET extension library Pin
Ravi Bhavnani28-Jul-12 10:52
professionalRavi Bhavnani28-Jul-12 10:52 
AnswerRe: recommendation for high quality .NET extension library Pin
Paul Conrad28-Jul-12 11:44
professionalPaul Conrad28-Jul-12 11:44 
GeneralRe: recommendation for high quality .NET extension library Pin
Dave Kreskowiak28-Jul-12 14:13
mveDave Kreskowiak28-Jul-12 14:13 
AnswerRe: recommendation for high quality .NET extension library Pin
Pete O'Hanlon28-Jul-12 20:35
mvePete O'Hanlon28-Jul-12 20:35 
AnswerRe: recommendation for high quality .NET extension library Pin
Florian Rappl28-Jul-12 23:04
professionalFlorian Rappl28-Jul-12 23:04 
GeneralRe: recommendation for high quality .NET extension library Pin
BillWoodruff29-Jul-12 1:38
professionalBillWoodruff29-Jul-12 1:38 
GeneralRe: recommendation for high quality .NET extension library Pin
BillWoodruff29-Jul-12 2:31
professionalBillWoodruff29-Jul-12 2:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.