简体   繁体   English

Base-64 char数组的长度无效

[英]Invalid length for a Base-64 char array

I'm getting a "Invalid length for a Base-64 char array." 我得到了“Base-64 char数组的长度无效”。 inside of the IF(){...} are variations i have tried to get it to work. IF(){...}内部是我试图让它工作的变种。 it fails in the first line without calling decrypt(...) proving it's not that functions problem. 它没有在第一行中失败而没有调用decrypt(...)证明它不是函数问题。 i get the same error inside with the first decrypt(...) call. 我在第一次解密(...)调用时遇到了同样的错误。 the last one using the encoding.ascii... will get me inside the function, but then it fails inside the function. 使用encoding.ascii的最后一个...将让我进入函数内部,但随后它在函数内部失败。 I'm getting the proper encrypted info from the database to string SSnum. 我从数据库中获取正确的加密信息到字符串SSnum。 it's value is: 4+mFeTp3tPF 它的值是:4 + mFeTp3tPF

try
{
    string SSnum = dr.GetString(dr.GetOrdinal("Social Security"));
    if (isEncrypted)
    {
      byte[] temp = Convert.FromBase64String(SSnum);
      //SSnum = decrypt(Convert.FromBase64String(SSnum), Key, IV);
      //SSnum = decrypt(Encoding.ASCII.GetBytes(SSnum), Key, IV);
    }
    txt_Social_Security.Text = SSnum;
}
catch { txt_Social_Security.Text = ""; }

I've been told to use the Convert.FromBase64String() and not the ASCII method...so why is it failing, how can i fix it? 我被告知使用Convert.FromBase64String()而不是ASCII方法...所以为什么它失败了,我该如何解决?

Base64 data length should be multiple of 4 and with padding char '=' You can change your data as valid base64 data. Base64数据长度应为4的倍数,并使用padding char'='您可以将数据更改为有效的base64数据。

string dummyData = imgData.Trim().Replace(" ", "+");
if (dummyData.Length % 4 > 0)
dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
byte[] byteArray = Convert.FromBase64String(dummyData); 

https://stackoverflow.com/a/9301545/2024022 https://stackoverflow.com/a/9301545/2024022

This will help you , try once. 这将帮助您,尝试一次。 Thanks suribabu. 谢谢suribabu。

it's value is: 4+mFeTp3tPF 它的值是:4 + mFeTp3tPF

You are receiving this error because that value, 4+mFeTp3tPF , is in fact not valid Base64. 您收到此错误,因为该值4+mFeTp3tPF实际上不是有效的Base64。

Is it possible you are simply missing the required padding character, as so 4+mFeTp3tPF= ? 您是否可能只是缺少必需的填充字符,因此4+mFeTp3tPF=

Are you certain that you have a Base64 string? 你确定你有一个Base64字符串吗? Base64 is a means of encoding binary data into a string while only using standard 7-bit ASCII characters. Base64是一种将二进制数据编码为字符串的方法,只使用标准的7位ASCII字符。 It's not a string encoding like ASCII and has some control bytes present. 不是像ASCII一样的字符串编码,并且存在一些控制字节。 You have a Base64 string if you're using Convert.ToBase64String to obtain the value (which, if you're trying to store binary data as a string, is your best bet) 如果你使用Convert.ToBase64String来获取值,那么你有一个Base64字符串(如果你试图将二进制数据存储为字符串,那么这是你最好的选择)

Judging by your error (and your example data), I'm assuming that you do not have a Base64 string. 根据您的错误(以及您的示例数据)判断,我假设您没有 Base64字符串。 If you need to store binary data in the database, you can either create a column using a binary type or encode the string into Base64 by using Convert.ToBase64String . 如果需要在数据库中存储二进制数据,可以使用二进制类型创建列,也可以使用Convert.ToBase64String将字符串编码为Base64。

byte[] inputData = ...;

string base64String = Convert.ToBase64String(inputData);

byte[] outputData = Convert.FromBase64String(base64String);

Here, outputData should contain the same data as inputData . 这里, outputData应包含与inputData相同的数据。

If what you have is just an ASCII-encoded string, then your original practice of using System.Text.Encoding.ASCII.GetBytes() is correct, but you should change this to use a Base64 string if you can. 如果您拥有的只是一个ASCII编码的字符串,那么您使用System.Text.Encoding.ASCII.GetBytes()原始做法是正确的,但是如果可以的话,您应该将其更改为使用Base64字符串。

Are you sure that string 4+mFeTp3tPF is well-formed Base64 string? 你确定字符串4 + mFeTp3tPF是格式良好的Base64字符串吗? I've tried some online services - no one could convert it. 我尝试了一些在线服务 - 没有人可以转换它。

replace 更换

byte[] temp = Convert.FromBase64String(SSnum); byte [] temp = Convert.FromBase64String(SSnum);

to this 对此

var temp = UTF8Encoding.UTF8.GetBytes(SSnum); var temp = UTF8Encoding.UTF8.GetBytes(SSnum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM