Padding is invalid and cannot be removed

Posted on 2007-08-01
Last Modified: 2013-12-17
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 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;



        /// <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);


            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];
            return Encoding.UTF8.GetString(plainText);


Question by:jtwoods4
    1 Comment
    LVL 18

    Accepted Solution

    try using this
    _Rijndael.Padding = PaddingMode.PKCS7
    _Rijndael.Padding = PaddingMode.ANSIX923

    instead of
    _Rijndael.Padding = PaddingMode.ISO10126;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now