0

我有以下代码

问题是当我尝试运行它时会引发异常并打印:

条目不是有效的 base64 字符串,因为它包含不是 base 64 的字符

这是代码:

    private static string Encrypt(string plainStr, string completeEncodedKey, int keySize)
    {
        Console.WriteLine("Entro Encrypt");
        RijndaelManaged aesEncryption = new RijndaelManaged();
        aesEncryption.KeySize = keySize;
        aesEncryption.BlockSize = 128;
        aesEncryption.Mode = CipherMode.CBC;
        aesEncryption.Padding = PaddingMode.PKCS7;
        Console.WriteLine(completeEncodedKey);
        aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[0]);
        aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[1]);
        byte[] plainText = ASCIIEncoding.UTF8.GetBytes(plainStr);
        ICryptoTransform crypto = aesEncryption.CreateEncryptor();
        Console.WriteLine("Abajo de crypto");
        // The result of the encryption and decryption            
        byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length);
        return Convert.ToBase64String(cipherText);
    }

这是堆栈跟踪:

en System.Convert.FromBase64String(String s)  
en cryptograph.Cryptography.Encrypt(String plainStr, String completeEncodedKey,
        Int32 keySize)  
en C:\programacion\cryptograph\cryptograph\cryptograph\Cryptography.cs:línea 65  
en cryptograph.Cryptography.generateKey(String key, String pass)  
en C:\programacion\cryptograph\cryptograph\cryptograph\Cryptography.cs:línea 13  
en cryptograph.cryptograph.Main(String[] args)  
en C:\programacion\cryptograph\cryptograph\cryptograph\cryptograph.cs:línea 21  

编辑:completeEncodedKey 是一个随机字符串,例如“AAAA”或其他内容。我知道它的长度必须是四的倍数

4

0 回答 0