【问题标题】:The input is not a valid Base-64 string as it contains a non-base 64 character in C#?输入不是有效的 Base64 字符串,因为它在 C# 中包含非 base 64 字符?
【发布时间】:2014-01-09 20:18:56
【问题描述】:

我在 WCF Web 服务中使用以下方法对请求和响应进行加密和解密:

public static string Decrypt(string textToDecrypt, string key)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 0x80;
            rijndaelCipher.BlockSize = 0x80;

            string decodedUrl = HttpUtility.UrlDecode(textToDecrypt);
            byte[] encryptedData = Convert.FromBase64String(decodedUrl);
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
            byte[] keyBytes = new byte[0x10];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
            {
                len = keyBytes.Length;
            }
            Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            rijndaelCipher.IV = keyBytes;
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            return encoding.GetString(plainText);
        }


       public static string Encrypt(string textToEncrypt, string key)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 0x80;
            rijndaelCipher.BlockSize = 0x80;
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
            byte[] keyBytes = new byte[0x10];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
            {
                len = keyBytes.Length;
            }
            Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            rijndaelCipher.IV = keyBytes;
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
            byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt);
            return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
        }

通过使用方法成功加密和解密数据。之后我成功加密 JSON 对象但面临解密问题 我正在使用以下数据:

用于加密

Encrypt("{\"password\":\"Password123\",\"username\":\"Jhon.Trrot\"}", "demo")

用于解密

Decrypt(encString, "demo");

当我删除 :, 时,它运行良好,但 :, 出现此错误:

The server encountered an error processing the request. The exception message is 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. '.

【问题讨论】:

  • OT,但Encoding.UTF8.GetBytes(key); 是从字符串中获取密钥的一种非常糟糕的方法。
  • 你能告诉我们编码的输入字符串(当然是由测试键生成的)吗?请编辑成问题(在解码方法开始时打印出textToDecrypt)。

标签: c# json wcf encryption


【解决方案1】:

您忘记在加密方法中对您的 base 64 输出进行 URL 编码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2012-09-22
    • 1970-01-01
    • 2020-11-22
    • 2021-07-16
    相关资源
    最近更新 更多