Re: Problem with Decryption(PKCS7 invalid padding)

From: Ivan Medvedev [MS] (ivanmed_at_online.microsoft.com)
Date: 03/14/04

  • Next message: Jonathan Ruckert: "RE: opening a project"
    Date: Sat, 13 Mar 2004 15:19:28 -0800
    
    

    Keith -
    the problem most likely is that you are assuming that the number of bytes in
    the cipthertext is the same as the number of bytes in the original
    plaintext, which is not the case with padding is used (ciphertext is usually
    longer). You may want to check out this link for a sample on doing symmetruc
    encryption: http://www.dotnetthis.com/Articles/Crypto.htm.
    --Ivan
    http://blogs.dotnetthis.com/ivan

    "Keith La Force" <klaforc@clemson.edu> wrote in message
    news:2E23F07D-AB79-4B71-B0CF-319AC1E83571@microsoft.com...
    > Hi, I am recieving the following error when trying to decrypt a message
    after writing to a text file
    >
    > PKCS7 padding is invalid and cannot be removed
    >
    > Here is the code:
    >
    > static void Main(string[] args)
    > {
    > string fileName = "encryptedtext.txt";
    > string passPhrase = "Wouldn't you like to know!";
    >
    > FileStream fStream = new FileStream(fileName, FileMode.OpenOrCreate,
    FileAccess.Write);
    >
    > byte[] writeBuffer = new byte[1024];
    > byte[] key = new Byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
    13, 14, 15, 16 };
    > byte[] initVect = new Byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
    13, 14, 15, 16 };
    >
    > RijndaelManaged rijn = new RijndaelManaged();
    >
    > CryptoStream encStream = new CryptoStream(fStream,
    rijn.CreateEncryptor(key, initVect), CryptoStreamMode.Write);
    >
    > Console.WriteLine("\nEncrypting Phrase: {0} - to file: {1}\n", passPhrase,
    fileName);
    >
    > ASCIIEncoding byteConverter = new ASCIIEncoding();
    > writeBuffer = byteConverter.GetBytes(passPhrase);
    >
    > encStream.Write(writeBuffer, 0, passPhrase.Length);
    >
    > fStream.Close();
    >
    > fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    >
    > byte[] encBuffer = new byte[100];
    >
    > fStream.Read(encBuffer, 0, passPhrase.Length);
    > string readResult = byteConverter.GetString(encBuffer);
    >
    > Console.WriteLine("Encrypted Text: {0}\n", readResult);
    >
    > fStream.Seek(0, SeekOrigin.Begin);
    >
    > CryptoStream decStream = new CryptoStream(fStream,
    rijn.CreateDecryptor(key, initVect), CryptoStreamMode.Read);
    >
    > byte[] readBuffer = new byte[passPhrase.Length];
    >
    > try
    > {
    > decStream.Read(readBuffer, 0, passPhrase.Length);
    > }
    > catch(System.Exception e)
    > {
    > Console.WriteLine("The message is {0} ", e.Message);
    > }
    > readResult = byteConverter.GetString(readBuffer);
    >
    > Console.WriteLine("Decrypted Text: {0}\n", readResult);
    >
    > Console.ReadLine();
    > fStream.Close();
    > }
    >
    > Thanks for your help in advance.


  • Next message: Jonathan Ruckert: "RE: opening a project"