0

Edit.

I guess I had a problem with creating a correct input into the encrypt/decrypt methods: These lines of code do the trick:

        string encrypted = en.Encrypt(stringBuilder.ToString(), "username", "password");
        string decrypted = en.Decrypt(encrypted, "username", "password");

        mainWindow.ChangeTextBox = encrypted + Environment.NewLine + decrypted;

I am just playing around and trying to figure out how encryption/decryption by using AES works. I am referring to this article (pretty much copy and paste :( Trying to learn).

There they give me complete encryption/decryption methods which I have tried to modify. I am trying to pass a list of entries created by ArrayList then binded with string builder. It seems like I am able to encrypt data but decryption causes error:

Length of the data to decrypt is invalid.

at line with code:

ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);

How can I implement this code to be useful with my program and work correctly?

Regards.

4
  • Can you edit your code down to the minimum needed to demonstrate the issue? That is a lot of code.
    – user47589
    Nov 3, 2011 at 18:56
  • Would it be possible to say what parameters (cleartext, ciphertext, pass, usrName) causes that exception?
    – ordag
    Nov 3, 2011 at 19:38
  • @ordag: 'PlainTextBytes.Length' seems to be the problem.
    – HelpNeeder
    Nov 4, 2011 at 0:17
  • @Inuyasha: I tried, but there's still much code left.
    – HelpNeeder
    Nov 4, 2011 at 0:17

2 Answers 2

1

I haven't looked through the code in detail, but it seems that the order of the parameters on the calls to Encrypt and Decrypt doesn't match the definition of the methods. In the calls to these methods you appear to have username, password, plain/cyphertext; whereas the methods have the signature plain/cyphertext, password, username.

2
  • Yes, I am aware of this and I have changed it after I have posted the question. Having my methods parameters formatted correctly I am still getting the same error.
    – HelpNeeder
    Nov 4, 2011 at 0:16
  • You are correct! I have misinterpreted your question! I have edited my OP with lines that were not correct.
    – HelpNeeder
    Nov 4, 2011 at 0:42
1

Am I right it's caused for an empty or null string as plaintext? ;-)
"lol1" can not be decrypted as it's not a valid ciphertext length (padding!) not to say it wouldn't make any sense.

2
  • Well, it still causes a problem if I remove the text and leave as empty string.
    – HelpNeeder
    Nov 3, 2011 at 23:14
  • @HelpNeeder: If you remove what?
    – ordag
    Nov 3, 2011 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.