2012-04-28 260 views
3

長話短說,我使用的DES和我使用RSA加密密碼交換,密碼不超過16個字符 問題是當我加密密鑰,加密大小變得對我來說太大解密 這裏是我的RSA加密和解密代碼:無法解密RSA加密密鑰

加密: - 我一直在努力的localpwd爲「ASD」

byte[] plaintext = utf8.GetBytes(localpwd); 
    byte[] ciphertext = rsaservice.Encrypt(plaintext, false); 
    string cipherresult = Convert.ToBase64String(ciphertext); 

然後我打印加密的密鑰對文本框並嘗試解密

byte[] ciphertext = utf8.GetBytes(filetest.Text); 
    byte[] plain = rsaservice.Decrypt(ciphertext, true); 
    string plaintext = utf8.GetString(plain); 

我得到「要解密的數據超過這個256字節模數的最大值」。 我試圖增加密鑰大小能夠加密和解密較大的密鑰大小,但增加密鑰只是增加了加密數據的大小導致相同的錯誤 請幫助!

+5

我沒有看到ToBase64String的'反向()' – 2012-04-28 09:52:39

+0

我使用utf8編碼來獲得轉換爲字節NAD回串再次 – 2012-04-28 10:20:20

回答

5
//byte[] ciphertext = utf8.GetBytes(filetest.Text); 
    byte[] ciphertext = Convert.FromBase64String(filetest.Text); 
+0

WOW從來沒有想到會那麼簡單,你先生有剛剛救了我的命:D謝謝 – 2012-04-28 10:46:37

+0

我可以使用unicode嗎?我試圖通過網絡流發送,而且我無法爲Base64字符串執行 – 2012-04-28 12:10:14

+0

所有字符串都可以編碼爲Unicode ...在這裏應該沒關係。 – 2012-04-28 12:32:53