解密个人地址时出现错误asp.net

解密个人地址时出现错误asp.net,asp.net,encryption,Asp.net,Encryption,我想加密数据库中的地址,它会毫无例外地保存,但当我在加载时解密地址时,它会给出错误: Unable to load information. Possible reason: Error in DecodeInvalid length for a Base-64 char array. <asp:TextBox ID="txtAddress" runat="server" Width="200px" /> <ajaxToolkit:FilteredTextBoxExtend

我想加密数据库中的地址,它会毫无例外地保存,但当我在加载时解密地址时,它会给出错误:

Unable to load information. Possible reason: Error in DecodeInvalid length for a Base-64 char array. 

<asp:TextBox ID="txtAddress" runat="server" Width="200px" />
<ajaxToolkit:FilteredTextBoxExtender ID="ftxtAddress" runat="server" TargetControlID="txtAddress"
FilterType="Custom,Numbers, LowercaseLetters" InvalidChars="!@#$%^&*()~`*-/+|\}{[]<>?" ValidChars=",.?-_ " />
在编码方面:

string rawText = "123 Any Street, Any City, Any State 99999, USA";
byte[] bytesE =  UTF8Encoding.UTF8.GetBytes(rawText);
//If you have your encryption code and it outputs a byte array, pass that to bytesE
string sData = Convert.ToBase64String(bytesE);
byte[] bytesD = Convert.FromBase64String(sData);
//If you have your decryption program, you now pass bytesD to it.
string address = UTF8Encoding.UTF8.GetString(bytesD);
然后,sData将是“MTIziefuesbtdhjlzqsiefuesbdaxr5lcbbbnkgu3rhdgugotk5otksifvtqq==”。我假设您随后将其存储在数据库中

在解码侧:

string rawText = "123 Any Street, Any City, Any State 99999, USA";
byte[] bytesE =  UTF8Encoding.UTF8.GetBytes(rawText);
//If you have your encryption code and it outputs a byte array, pass that to bytesE
string sData = Convert.ToBase64String(bytesE);
byte[] bytesD = Convert.FromBase64String(sData);
//If you have your decryption program, you now pass bytesD to it.
string address = UTF8Encoding.UTF8.GetString(bytesD);
地址现在将是“123美国任何街道、任何城市、任何州99999”

sData必须是正确终止的有效Base64编码字符串,否则Convert.FromBase64String将产生异常


请记住,如果您没有加密代码,上面提供的安全性。

您可以在编码的位置显示您的代码吗?或者给我们一个给出错误的sData示例?如何验证字符串是否为base64 im仍在接收错误base64中允许多少个字符如果我限制为36个字符,则无错误要验证,请使用try and catch!你想自己编码到base64吗?如果是这样,您需要参考规范或RFC 4648。如果您的数据已经是一个字节数组,只需使用UTF8ENcoding.UTF8.GetBytes,如上图所示。