Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am using Rijndael algorithm for encyption and decription in ASP.Net Core but getting error <big>specified padding mode is not valid for this algorithm</big>

C#
private static readonly string _Pwd = "3BF3EE08-B757-41ba-87F5-59CC6B9A47B4";
        private static readonly byte[] _Salt = new byte[] { 0x45, 0xf1, 0x61, 0x6e, 0x20, 0x0, 0x65, 0x64, 0x76, 0x65, 0x64, 0x3, 0x76 };
        //private static RijndaelManaged _cryptoProvider;
        //128 bit encyption: DO NOT CHANGE    
        private static readonly byte[] Key = { 12, 14, 6, 22, 32, 28, 8, 24, 13, 3, 11, 9, 17, 15, 4, 29 };
        private static readonly byte[] IV = { 12, 2, 16, 7, 5, 9, 17, 8, 4, 47, 16, 18, 1, 32, 25, 10 };
        public static string Decrypt(string cipherText)
        {
            if (string.IsNullOrEmpty(cipherText))
                return "";
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
            byte[] decryptedData = DoDecrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
            return System.Text.Encoding.Unicode.GetString(decryptedData);
        }

        private static byte[] DoDecrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = null;
            try
            {
                Rijndael alg = Rijndael.Create();
                alg.Key = Key;
                alg.IV = IV;
                cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(cipherData, 0, cipherData.Length);
                cs.FlushFinalBlock();
                return ms.ToArray();
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                cs.Close();
            }
        }


What I have tried:

C#
public static string Decrypt(string cipherText)
        {
            if (string.IsNullOrEmpty(cipherText))
                return "";
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
            byte[] decryptedData = DoDecrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
            return System.Text.Encoding.Unicode.GetString(decryptedData);
        }

        private static byte[] DoDecrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = null;
            try
            {
                //Rijndael alg =new Rijndael();
                //alg.Key = Key;
                //alg.IV = IV;
                cs = new CryptoStream(ms, Rijndael.CreateDecryptor(_Pwd, IV, KeySize.Aes128), CryptoStreamMode.Write);
                cs.Write(cipherData, 0, cipherData.Length);
                cs.FlushFinalBlock();
                return ms.ToArray();
            }
            catch(Exception ex)
            {
                return null;
            }
            finally
            {
                //cs.Close();
            }
        }
Posted
Comments
F-ES Sitecore 3-Jul-17 6:20am    
Have you googled the error and tried any of the suggestions?
itsathere 3-Jul-17 6:51am    
Yes and still trying to find.
Mehdi Gholam 3-Jul-17 10:44am    
.net core v1.1 has limitations, try it on the v2 preview which has more functionality.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900