2012-06-28 31 views
0

已嘗試將xml文件加密爲字符串,以便通過服務將其傳輸。傳輸是使用編譯到代碼中的對稱密鑰從服務器到服務器。解析加密時出現Encoding.Unicode.GetString錯誤Byte()

我一直使用從MSDN的AES樣本,然後將字節ARRY轉換爲與字符串像這樣:

' Encrypt the string to an array of bytes. 
    Dim encrypted As Byte() = crypto.EncryptString(original, _key, _iv) 

    Dim encrypStr As String = Encoding.Unicode.GetString(encrypted) 
    '''' >>> Transmit... 
    Dim postTrans As Byte() = Encoding.Unicode.GetBytes(encrypStr) 

    ' Decrypt the bytes to a string. 
    Dim roundtrip As String = crypto.DecryptString(postTrans, _key, _iv) 

沒有中間兩行加密/解密工作正常,與中間的兩個包括的行我要麼收到一個形式很糟糕的XML文檔,無法解析或「填充無效,無法刪除」錯誤。

這不是一個好的字符串加密方法嗎?它完美地工作,無需將字節()轉換爲字符串廣告。

+2

不要使用Encoding.GetString(),將不會始終工作。使用ToBase64() –

+0

@HenkHolterman完美!這讓我感到頭痛,今天晚上,感謝解決方案。 – baileyswalk

+0

@HenkHolterman你可能想發佈它作爲答案... –

回答

1

不要使用Encoding.GetString(),將不總是工作。使用ToBase64()

感謝Henk Holterman的回答。

相關問題