Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Decrypting a text using Rijndael

Decrypting a text using Rijndael

Scheduled Pinned Locked Moved C#
algorithmsworkspace
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 3 Offline
    3 Offline
    3bood ghzawi
    wrote on last edited by
    #1

    Hi all, i have been using a Rijndael algorithm to encrypt and decrypt a sequence of bytes, but when i run the code i get the following exeption "Padding is invalid and cannot be removed.", this exeption belongs to decrypt function at line int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length); i writ my code here:

    public static byte[] Encrypt(byte[] clearData)
    {
    System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
    //Get your key from config file to open the lock!
    string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            byte\[\] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            hashmd5.Clear();
    
            Rijndael rijKey = Rijndael.Create();
            rijKey.Mode = CipherMode.;
            rijKey.Padding = PaddingMode.PKCS7;
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms,rijKey.CreateEncryptor(), CryptoStreamMode.Write); 
            cs.Write(clearData, 0, clearData.Length);
            cs.FlushFinalBlock();
            byte\[\] cipherTextBytes = ms.ToArray();
            ms.Close();
            cs.Close();
    
            return cipherTextBytes;
        }
    
        public static byte\[\] Decrypt(byte\[\] encryptedData)
        {
            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            //Get your key from config file to open the lock!
            string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            byte\[\] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            hashmd5.Clear();
    
            Rijndael rijKey = Rijndael.Create();
            rijKey.Mode = CipherMode.ECB;
            rijKey.Padding = PaddingMode.PKCS7;
            MemoryStream memoryStream = new MemoryStream(encryptedData);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, rijKey.CreateDecryptor(),
            CryptoStreamMode.Read);
            byte\[\] pTextBytes = new byte\[encryptedData.Length\];
            int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return pTextBytes;
        }
    
    N K L 3 Replies Last reply
    0
    • 3 3bood ghzawi

      Hi all, i have been using a Rijndael algorithm to encrypt and decrypt a sequence of bytes, but when i run the code i get the following exeption "Padding is invalid and cannot be removed.", this exeption belongs to decrypt function at line int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length); i writ my code here:

      public static byte[] Encrypt(byte[] clearData)
      {
      System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
      //Get your key from config file to open the lock!
      string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

              MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
              byte\[\] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
              hashmd5.Clear();
      
              Rijndael rijKey = Rijndael.Create();
              rijKey.Mode = CipherMode.;
              rijKey.Padding = PaddingMode.PKCS7;
              MemoryStream ms = new MemoryStream();
              CryptoStream cs = new CryptoStream(ms,rijKey.CreateEncryptor(), CryptoStreamMode.Write); 
              cs.Write(clearData, 0, clearData.Length);
              cs.FlushFinalBlock();
              byte\[\] cipherTextBytes = ms.ToArray();
              ms.Close();
              cs.Close();
      
              return cipherTextBytes;
          }
      
          public static byte\[\] Decrypt(byte\[\] encryptedData)
          {
              System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
              //Get your key from config file to open the lock!
              string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
              MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
              byte\[\] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
              hashmd5.Clear();
      
              Rijndael rijKey = Rijndael.Create();
              rijKey.Mode = CipherMode.ECB;
              rijKey.Padding = PaddingMode.PKCS7;
              MemoryStream memoryStream = new MemoryStream(encryptedData);
              CryptoStream cryptoStream = new CryptoStream(memoryStream, rijKey.CreateDecryptor(),
              CryptoStreamMode.Read);
              byte\[\] pTextBytes = new byte\[encryptedData.Length\];
              int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length);
              memoryStream.Close();
              cryptoStream.Close();
              return pTextBytes;
          }
      
      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Format the code. Remove the duplicate post before some replies to it.


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • 3 3bood ghzawi

        Hi all, i have been using a Rijndael algorithm to encrypt and decrypt a sequence of bytes, but when i run the code i get the following exeption "Padding is invalid and cannot be removed.", this exeption belongs to decrypt function at line int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length); i writ my code here:

        public static byte[] Encrypt(byte[] clearData)
        {
        System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
        //Get your key from config file to open the lock!
        string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                byte\[\] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
        
                Rijndael rijKey = Rijndael.Create();
                rijKey.Mode = CipherMode.;
                rijKey.Padding = PaddingMode.PKCS7;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms,rijKey.CreateEncryptor(), CryptoStreamMode.Write); 
                cs.Write(clearData, 0, clearData.Length);
                cs.FlushFinalBlock();
                byte\[\] cipherTextBytes = ms.ToArray();
                ms.Close();
                cs.Close();
        
                return cipherTextBytes;
            }
        
            public static byte\[\] Decrypt(byte\[\] encryptedData)
            {
                System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
                //Get your key from config file to open the lock!
                string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                byte\[\] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
        
                Rijndael rijKey = Rijndael.Create();
                rijKey.Mode = CipherMode.ECB;
                rijKey.Padding = PaddingMode.PKCS7;
                MemoryStream memoryStream = new MemoryStream(encryptedData);
                CryptoStream cryptoStream = new CryptoStream(memoryStream, rijKey.CreateDecryptor(),
                CryptoStreamMode.Read);
                byte\[\] pTextBytes = new byte\[encryptedData.Length\];
                int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length);
                memoryStream.Close();
                cryptoStream.Close();
                return pTextBytes;
            }
        
        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        Your code is unformatted, you need to ensure that the > & <g; appear, rather than > etc. The problem you are getting is due to extra characters being read from the stream. The length of encrypyted string will always be a multiple of a set length (which itself is a power of two). Our encryption rountine always produces blocks of length 16, So the string "1" as does "12345678901234" would produce an encrypted string 16 hex characters long, "123456789012345" produces 32 character long string. I thnk the actual block size relates to the key and/or the IV value. In this case, for a string to be decrypted, the count of hex characters must be evenly divisible by 16. You are probably either reading past the end of the string in the memory stream, or the stream is including padding characters. In either case, you must stop reading before then.

        Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

        1 Reply Last reply
        0
        • 3 3bood ghzawi

          Hi all, i have been using a Rijndael algorithm to encrypt and decrypt a sequence of bytes, but when i run the code i get the following exeption "Padding is invalid and cannot be removed.", this exeption belongs to decrypt function at line int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length); i writ my code here:

          public static byte[] Encrypt(byte[] clearData)
          {
          System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
          //Get your key from config file to open the lock!
          string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

                  MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                  byte\[\] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                  hashmd5.Clear();
          
                  Rijndael rijKey = Rijndael.Create();
                  rijKey.Mode = CipherMode.;
                  rijKey.Padding = PaddingMode.PKCS7;
                  MemoryStream ms = new MemoryStream();
                  CryptoStream cs = new CryptoStream(ms,rijKey.CreateEncryptor(), CryptoStreamMode.Write); 
                  cs.Write(clearData, 0, clearData.Length);
                  cs.FlushFinalBlock();
                  byte\[\] cipherTextBytes = ms.ToArray();
                  ms.Close();
                  cs.Close();
          
                  return cipherTextBytes;
              }
          
              public static byte\[\] Decrypt(byte\[\] encryptedData)
              {
                  System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
                  //Get your key from config file to open the lock!
                  string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
                  MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                  byte\[\] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                  hashmd5.Clear();
          
                  Rijndael rijKey = Rijndael.Create();
                  rijKey.Mode = CipherMode.ECB;
                  rijKey.Padding = PaddingMode.PKCS7;
                  MemoryStream memoryStream = new MemoryStream(encryptedData);
                  CryptoStream cryptoStream = new CryptoStream(memoryStream, rijKey.CreateDecryptor(),
                  CryptoStreamMode.Read);
                  byte\[\] pTextBytes = new byte\[encryptedData.Length\];
                  int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length);
                  memoryStream.Close();
                  cryptoStream.Close();
                  return pTextBytes;
              }
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, you have been around for several months, so you should know code needs to be shown with PRE tags so it keeps its formatting; and posting the same stuff twice isn't acceptable either. Please delete the one without replies, and edit the one heading this thread. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


          K 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, you have been around for several months, so you should know code needs to be shown with PRE tags so it keeps its formatting; and posting the same stuff twice isn't acceptable either. Please delete the one without replies, and edit the one heading this thread. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            Actually, the Pre tags are there, its just that he's used < instead of < I'm assuming he had Encode "<" (and other HTML) characters when pasting ticked.

            Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

            L 1 Reply Last reply
            0
            • K Keith Barrow

              Actually, the Pre tags are there, its just that he's used < instead of < I'm assuming he had Encode "<" (and other HTML) characters when pasting ticked.

              Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              You're right, I did not notice as I don't look at unformatted code at all... If the poster isn't willing to do the posting right, then he is beyond help as far as I'm concerned. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups