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
1 Posts 1 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.ECB;
            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;
        }
    
    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