2012-05-11 38 views
0

我要加密數據庫中的地址,它也不例外撲救,但是當我負載解密地址提示錯誤:解密個人地址給出錯誤asp.net

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="[email protected]#$%^&*()~`*-/+|\}{[]<>?" ValidChars=",.?-_ " /> 

我用:

public string Decode(string sData) 
{ 
    try 
    { 
    System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
    System.Text.Decoder utf8Decode = encoder.GetDecoder(); 

    byte[] todecode_byte = Convert.FromBase64String(sData); 
    int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); 
    char[] decoded_char = new char[charCount]; 
    utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); 
    string result = new String(decoded_char); 
    return result; 
    } 
    catch (Exception e) 
    { 
    throw new Exception("Error in Decode" + e.Message); 
    } 
} 
+1

你可以顯示你的代碼在哪裏做編碼?或者給我們一個給出錯誤的sData的例子? –

回答

1

在編碼側:然後

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); 

SDATA將「M TIzIEFueSBTdHJlZXQsIEFueSBDaXR5LCBBbnkgU3RhdGUgOTk5OTksIFVTQQ ==」。我假設你將它存儲在數據庫中。

在譯碼方:

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,USA」

SDATA必須是有效的Base64編碼字符串即正確終止,否則Convert.FromBase64String將產生一個異常。

請記住,如果你沒有你的加密代碼,上面提供了的安全性。

+0

我如何驗證字符串是base64即時通訊仍然收到錯誤 – skhurams

+0

如果我限制36個字符然後沒有錯誤,允許在base64中允許多少個字符 – skhurams

+0

要驗證,請使用try和catch! –

相關問題