【问题标题】:String cannot be of zero length. Parameter name: oldValue字符串的长度不能为零。参数名称:oldValue
【发布时间】:2013-05-19 23:43:37
【问题描述】:

我正在处理解密密码,但我遇到了这个错误:

字符串长度不能为零。参数名称:oldValue

请帮助解决此错误或建议我使用另一个解密程序。

这里是完整的代码:

string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("+",""));
int charcount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decode_char = new char[charcount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decode_char, 0);
decryptpwd = new String(decode_char);
return decryptpwd;

【问题讨论】:

  • Base64 不是加密。您必须散列密码。
  • 错误的哪一部分你不明白?你的代码没有意义。
  • @FreeLancer 这是我的完整代码,我使用来自网站的代码 string decryptpwd = string.Empty; UTF8Encoding encodepwd = new UTF8Encoding();解码器解码 = encodepwd.GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("+","")); int charcount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); char[] decode_char = new char[charcount];解码.GetChars(todecode_byte, 0, todecode_byte.Length, decode_char, 0); decryptpwd = new String(decode_char);返回解密密码;

标签: c# asp.net encryption


【解决方案1】:

您要求 Replace 方法用加号(第二个参数)更改空字符串(第一个参数)。这毫无意义,Replace 正在抱怨这一点。
我想你想做相反的事情

 byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("+",""));

其中一部分我不确定当您将某些内容更改为输入字符串并将 FromBase64String 应用于结果时会产生什么结果。嗯,这确实取决于字符串中最初的内容,但可以肯定的是(如果encryptpwd 真的是 Base64 字符串)没有空格可以替换。

请记住,您不能将普通字符串传递给 Convert.FromBase64String,您需要一个 base 64 字符串

What is a base 64 string

例如

string pwd = "786";   // The original string

UnicodeEncoding u = new UnicodeEncoding();
byte[] x = u.GetBytes(pwd);  // The Unicode bytes of the string above

// Convert bytes to a base64 string
string b64 = Convert.ToBase64String(x);
Console.WriteLine(b64);

// Go back to the plain text string    
byte[] b = Convert.FromBase64String(b64);
string result = u.GetString(b);
Console.WriteLine(result);

最后一句话。有人(@Slacks)已经告诉你 base64 字符串不是加密技术,你不应该将它用于加密密码(它们根本没有加密)

【讨论】:

  • 我试试这个它给出了错误 Invalid length for a Base-64 char array
  • 实际上,当我输入密码 786 时,它在我添加 ("+","") 后给出错误 Invalid length for a Base-64 char array 然后它给出错误“字符串不能为零长度。参数名称:oldValue"
  • 从哪里得到字符串 786?您不能获取任意字符串并将其传递给 FromBse64String。你需要一个 Base64String。你从 Convert.ToBase64String() 中得到的东西。我将添加一个示例
  • 我只能建议搜索主题加密和存储密码。论据非常广泛。 Look at this question 在我的答案的第二部分中,我解释了如何加密和解密纯文本
  • 谢谢史蒂夫这么好心
【解决方案2】:
encryptpwd.Replace("","+")

你到底要替换什么?您尚未指定要替换的原始值。

String.Replace 接受两个字符串参数oldValuenewValue。您指定了 newValue +,但是对于 oldValue,空字符串是不合法的。

因此,如果您想用+ 替换空格,请尝试:

encryptpwd.Replace(" ","+");

反之亦然:

encryptpwd.Replace("+"," ");

http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

【讨论】:

  • 我也尝试了你的解决方案我得到了错误 Invalid length for a Base-64 char array
【解决方案3】:

问题来了

encryptpwd.Replace("","+")

应该有一些字符或字符串要替换

encryptpwd.Replace(" ","+")

【讨论】:

  • @Rajev 我在收到错误后尝试您的解决方案 Invalid length for a Base-64 char array
【解决方案4】:

错误字符串长度不能为零。参数名称:oldValue

如果您想将单个字符与字符串分开。

试试这个:-

   int l=0;
   string str = Console.ReadLine();
   while(l <= str.Length -1)
   {
       Console.Write("{0} ", str[l]);
       l++;
   }

【讨论】:

  • 这如何回答 OP 的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多