Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8314
  • Last Modified:

Padding is invalid and cannot be removed

I am getting an error related to my encryption class. The error is "Padding is invalid and cannot be removed". See the code below

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using Moxie.Logging;
using Moxie.Cryptography;


namespace UPS.Security.Cryptography
{
    public class UPSCryptographyProvider
    {
        private const string CRYPTKEY = "RZFKGEHN$GCNV@LXJLKMEKDYYURTKJLE";
        private const string CRYPTIV = "IL#MCFACBX&YKPWGPWPGJNUPL?ZJHBXM";
        public RijndaelManaged _Rijndael = new RijndaelManaged();
       

        public UPSCryptographyProvider()
        {
            //  cryptSetting
         
            _Rijndael.Mode = CipherMode.CBC;
            _Rijndael.Padding = PaddingMode.ISO10126;
            _Rijndael.BlockSize = 256;
            _Rijndael.KeySize = 256;


           
            //_Rijndael.Clear();

        }


        /// <summary>
        /// Encrypts a string
        /// </summary>
        /// <param name="decryptedString"></param>
        /// <returns></returns>
        public string EncryptString(string plainText)
        {
            _Rijndael.Key = Encoding.ASCII.GetBytes(CRYPTKEY);
            _Rijndael.IV = Encoding.ASCII.GetBytes(CRYPTIV);
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            ICryptoTransform transform = _Rijndael.CreateEncryptor();
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms,transform,CryptoStreamMode.Write);
            cs.Write(plainTextBytes,0,plainTextBytes.Length);

            cs.FlushFinalBlock();

            byte[] cipherText = ms.ToArray();
           
            return Convert.ToBase64String(cipherText);


           
        }

        /// <summary>
        /// Decrypt a string
        /// </summary>
        /// <param name="encryptedString"></param>
        /// <returns></returns>
        public string DecryptString(string cipherText)
        {
            _Rijndael.Key = Encoding.ASCII.GetBytes(CRYPTKEY);
            _Rijndael.IV = Encoding.ASCII.GetBytes(CRYPTIV);
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
            ICryptoTransform transform = _Rijndael.CreateDecryptor();
            MemoryStream ms = new MemoryStream(cipherTextBytes);
            CryptoStream cs = new CryptoStream(ms,transform,CryptoStreamMode.Read);

            byte[] plainTextBuffer = new byte[cipherTextBytes.Length];
            int plainTextLength = cs.Read(plainTextBuffer,0,cipherTextBytes.Length);

            byte[] plainText = new byte[plainTextLength];
            Array.Copy(plainTextBuffer,0,plainText,0,plainTextLength);
             
            return Encoding.UTF8.GetString(plainText);
           

        }
             
    }

   
}
0
jtwoods4
Asked:
jtwoods4
1 Solution
 
Ajay SharmaCommented:
try using this
_Rijndael.Padding = PaddingMode.PKCS7
 or
_Rijndael.Padding = PaddingMode.ANSIX923

instead of
_Rijndael.Padding = PaddingMode.ISO10126;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now