Specified padding mode is not valid for this algorithm - .net Core

后端 未结 1 2048
旧巷少年郎
旧巷少年郎 2021-01-20 03:49

I am getting the following error message when I convert from .net 4.5 to .net core 2. The code is exactly the same. I have seen a few posts but none have solve this error.

1条回答
  •  囚心锁ツ
    2021-01-20 04:35

    I've fixed the part that did not produce a consistent result.

    But, since the Encryption method is commented out in your code, I'm not sure whether the Encrypted input value is exactly what is presented here or it comes from a different source and/or a different method of Encryption.

    I've integrated the MemoryStream assigned byte array, used by both the Encryption and the Decryption methods, with the Unicode Encoding/Decoding of the original value.
    Of course another Encoding can be used. (Tested also with UTF8, which should be the default).
    The string is Base64Encoded/Decoded as usual.

    I'm posting here just the section of code included in the #region "String Encryption" block.
    The rest of the code is untouched.

    Tested on: Visual Studio pro 15.8.0
    .Net FrameWork: Core 2.1
    C# 6.0 and C# 7.3

    The Ecryption/Decryption methods are called this way:

    string encryptedstring = EncryptionExtension.CryptString(
            EncryptionExtension.CryptMethod.Encrypt, EncryptionExtension.EncryptionAlgorithms.Rijndael, 
                "Some text to encrypt, more text to encrypt", "SomeKey");
    string decryptedstring = EncryptionExtension.CryptString(
            EncryptionExtension.CryptMethod.Decrypt, EncryptionExtension.EncryptionAlgorithms.Rijndael, 
                encryptedstring, "SomeKey");
    

    #region "String Encryption"
    
    public static string EncryptString(EncryptionAlgorithms Method, string Value, string Key)
    {
        return CryptString(CryptMethod.Encrypt, Method, Value, Key);
    }
    
    public static string DecryptString(EncryptionAlgorithms Method, string Value, string Key)
    {
        return CryptString(CryptMethod.Decrypt, Method, Value, Key);
    }
    

    public static string CryptString(CryptMethod Method, EncryptionAlgorithms Algorithm, string Value, string Key)
    {
        if (Value == null || Value.Length <= 0)
        {
            throw new ArgumentNullException("Data can not be empty");
        }
        if (Key == null || Key.Length <= 0)
        {
            throw new ArgumentNullException("Key can not be empty");
        }
    
        SymmetricAlgorithm provider = null;
    
        try
        {
            switch (Algorithm)
            {
                case EncryptionAlgorithms.Rijndael:
                    provider = new RijndaelManaged();
                    break;
            }
            provider.KeySize = provider.LegalKeySizes[0].MaxSize;
            provider.BlockSize = provider.LegalBlockSizes[0].MaxSize;
            provider.Key = DerivePassword(Key, provider.LegalKeySizes[0].MaxSize / 8);
    
            switch (provider.BlockSize / 8)
            {
                case 8:
                    provider.IV = IV_8;
                    break;
                case 16:
                    provider.IV = IV_16;
                    break;
                case 24:
                    provider.IV = IV_24;
                    break;
                case 32:
                    provider.IV = IV_32;
                    break;
            }
    
            if (Method == CryptMethod.Encrypt)
            {
                byte[] encodedText = Encoding.Unicode.GetBytes(Value);
    
                // Create the streams used for encryption/decryption    
                using (ICryptoTransform encryptor = provider.CreateEncryptor(provider.Key, provider.IV))
                using (var msCrypt = new MemoryStream())
                using (var csEncrypt = new CryptoStream(msCrypt, encryptor, CryptoStreamMode.Write))
                {
                    csEncrypt.Write(encodedText, 0, encodedText.Length);
                    csEncrypt.FlushFinalBlock();
                    return Convert.ToBase64String(msCrypt.ToArray());
                }
            }
            else
            {
                byte[] cipherBytes = Convert.FromBase64String(Value);
    
                // Create the streams used for decryption.    
                using (ICryptoTransform decryptor = provider.CreateDecryptor(provider.Key, provider.IV))
                using (var msCrypt = new MemoryStream(cipherBytes))
                using (var csDecrypt = new CryptoStream(msCrypt, decryptor, CryptoStreamMode.Read))
                {
                    byte[] decodedText = new byte[cipherBytes.Length];
                    int decryptedCount = csDecrypt.Read(decodedText, 0, decodedText.Length);
                    return Encoding.Unicode.GetString(decodedText, 0, decryptedCount);
                }
            }
        }
        catch (Exception ex)
        {
            //throw new AceExplorerException(ex.Message + "  " + ex.StackTrace + " " + ex.TargetSite.ToString() + " " + ex.Source, ex.InnerException);
            throw new Exception(ex.Message + "  " + ex.StackTrace + " " + ex.TargetSite.ToString() + " " + ex.Source, ex.InnerException);
        }
        finally
        {
            // Clear the Provider object.    
            provider?.Clear();
        }
    }
    #endregion
    
    private static byte[] DerivePassword(string password, int length)
    {
        Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(password, CRYPT_SALT, 1000);
        return derivedBytes.GetBytes(length);
    }
    

    0 讨论(0)
提交回复
热议问题