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

Rah*_*eel 9 c# asp.net encryption

我正在使用Decrypt密码,我坚持这个错误:

字符串长度不能为零.参数名称: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;
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 9

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

 byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("+",""));
Run Code Online (Sandbox Code Playgroud)

这部分我不知道当你将某些东西改成输入字符串并将FromBase64String应用于结果时结果是什么.嗯,这实际上取决于字符串中最初的内容,但可以肯定(如果encryptpwd真的是Base64字符串),则无法替换空格.

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

什么是base 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);
Run Code Online (Sandbox Code Playgroud)

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


Dar*_*ren 7

encryptpwd.Replace("","+")
Run Code Online (Sandbox Code Playgroud)

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

String.Replace需要两个字符串参数oldValuenewValue.您指定了newValue,+但是空字符串不合法oldValue.

因此,如果要使用+try 替换空格:

encryptpwd.Replace(" ","+");
Run Code Online (Sandbox Code Playgroud)

或相反亦然:

encryptpwd.Replace("+"," ");
Run Code Online (Sandbox Code Playgroud)

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