none
Problem while using AES Encryption Algorithm RRS feed

  • Question

  • Hello everyone

    I'm using the following class in .Net Framework 4.0 to encrypt my files and decrypt them:

    https://ourcodeworld.com/articles/read/471/how-to-encrypt-and-decrypt-files-using-the-aes-encryption-algorithm-in-c-sharp

    encrypting file was successfully without any problem but while I'd tried to decrypt the encrypted file, it show me this error: 'Length of the data to decrypt is invalid.' at line:

    while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)

    Please help me guys. I'd tried many way to solve this but I couldn't :(




    Sunday, February 18, 2018 5:22 PM

All replies

  • The  'Length of the data to decrypt is invalid' error indicates that your decryptor CryptoStream is not receiving the same data that resulted from encryption. Somewhere along the process you made some mistake, and you are not passing to the decryptor the exact same binaries that resulted from encryption. This can happen for various reasons, which are not visible in your question. For example, if you tried to convert the binary encrypted data into a string and then back from the string for decoding, this could easily corrupt the data if it was not done properly.
    Sunday, February 18, 2018 5:46 PM
    Moderator
  • yeah but when i use ReadByte() instead of Read(), everything is OK. but its not good to reading big files and takes many times to decrypt file!

    Unfortunately I don't know what else I should do. You are my last hope :(



    Sunday, February 18, 2018 6:32 PM
  • Hi Shahryar1991,

    I suggest you to download the source file from the code project about AES encrypt and decrypt for reference.

    https://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt

    https://www.codeproject.com/search.aspx?q=AES+C%23&x=0&y=0&sbo=kw

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, February 25, 2018 6:48 PM
    Moderator