wpf Base-64 字符数组或字符串的长度无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28450852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:56:35  来源:igfitidea点击:

Invalid length for a Base-64 char array or string

c#wpf

提问by Joy

Here are my encryption and decryption methods. I have two databases and I copied the encrypted password from one database to the other. The code was in vb but I converted it into C#.

这是我的加密和解密方法。我有两个数据库,我将加密的密码从一个数据库复制到另一个数据库。代码在 vb 中,但我将其转换为 C#。

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Security.Cryptography;
 using System.IO;
 namespace AccountSystem.Class{
class ClEncrDecr
{
    private TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();

    private byte[] TruncateHash(string key, int length)
    {
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        //Hash the Key
        byte[] keyBytes = System.Text.Encoding.Unicode.GetBytes(key);
        byte[] hash = sha1.ComputeHash(keyBytes);

        // truncate or pad the hash
        Array.Resize(ref hash, length);
        return hash;
    }

    public ClEncrDecr()
    {
        string key = "ABCD";
        tripleDESCryptoServiceProvider.Key = TruncateHash(key, tripleDESCryptoServiceProvider.KeySize / 8 );
        tripleDESCryptoServiceProvider.IV = TruncateHash("", tripleDESCryptoServiceProvider.BlockSize / 8 );
    }

    public string EncryptData(string plainText)
    {
        byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plainText);
        MemoryStream ms = new MemoryStream();
        CryptoStream encStream = new CryptoStream(ms, tripleDESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
        encStream.FlushFinalBlock();
        return Convert.ToBase64String(ms.ToArray());
    }

    public string DecryptData(string encryptedtext)
    {
        byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);
        MemoryStream ms = new MemoryStream();
        CryptoStream decStream = new CryptoStream(ms, tripleDESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
        decStream.FlushFinalBlock();
        return System.Text.Encoding.Unicode.GetString(ms.ToArray());
    }
}
}

Login Code :

登录代码:

MessageBox.Show(crypto.DecryptData(obj.password))

When we call DecryptData(string encryptedtext)it throws an exception saying Invalid length for a Base-64 char array or string. What can I do?

当我们调用DecryptData(string encryptedtext)它时会抛出一个异常说Invalid length for a Base-64 char array or string. 我能做什么?

回答by Claudio P

If you have the following encrypted Password:

如果您有以下加密密码:

dfghfgdfgd667878nnvghv

dfghfgdfgd667878nnvghv

It can't be converted to a byte array from Base64, because it's not a valid Base64String. A valid Base64String would be:

它无法从 Base64 转换为字节数组,因为它不是有效的 Base64String。有效的 Base64String 将是:

dfghfgdfgd667878nnvghv==

dfghfgdfgd667878nnvghv==

回答by Olli

As Claudio mentioned in the comment, your encryptedtext variable is not a base64 encoded string, perhaps it's at least missing padding character(s) at the end.

正如 Claudio 在评论中提到的,您的 encryptedtext 变量不是 base64 编码的字符串,也许它至少在末尾缺少填充字符。

It's not visible from the example how it's created, but you might want to look for example this SO question: How do I encode and decode a base64 string?

从示例中看不到它是如何创建的,但是您可能想查看这个 SO 问题的示例:如何对 base64 字符串进行编码和解码?

About padding: http://en.wikipedia.org/wiki/Base64#Padding

关于填充:http: //en.wikipedia.org/wiki/Base64#Padding