微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

String cannot be of zero length. Parameter name: 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,todecode_byte.Length);
char[] decode_char = new char[charcount];
Decode.GetChars(todecode_byte,todecode_byte.Length,decode_char,0);
decryptpwd = new String(decode_char);
return decryptpwd;

解决方法

您要求Replace方法更改带有加号字符(第二个参数)的空字符串(第一个参数).这没有任何意义,Replace正在抱怨这一点.
我想你想反过来
byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("+",""));

从此我不知道当你将某些内容更改为输入字符串并将FromBase64String应用于结果时,结果将是什么.好吧,它实际上取决于字符串中最初的内容,但可以肯定的是(如果encryptpwd确实是Base64字符串),则无法替换空格.

请记住,您不能将普通字符串传递给Convert.FromBase64String,您需要一个基本为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字符串不是加密技术,你不应该用它来加密密码(它们根本不加密)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐