字符串的长度不能为零.参数名称:旧值

本文关键字:参数 旧值 不能 字符串 | 更新日期: 2023-09-27 18:15:15

我正在研究解密密码,但我坚持这个错误:

字符串的长度不能为零。参数名称:旧值

请帮助解决此错误或建议我使用其他解密程序。

以下是完整的代码:

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;

字符串的长度不能为零.参数名称:旧值

您要求 Replace 方法将空字符串(第一个参数(更改为加号字符(第二个参数(。这毫无意义,替换正在抱怨这一点。
我想你想反其道而行之

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

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

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

什么是基数 64 字符串

例如

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字符串不是加密技术,您不应该使用它来加密密码(它们根本没有加密(

encryptpwd.Replace("","+")

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

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

因此,如果要将空格替换为+请尝试:

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

反之亦然:

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

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

问题就在这里

encryptpwd.Replace("","+")

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

encryptpwd.Replace(" ","+")

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

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

试试这个:-

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