2013-04-26 64 views
0

我使用rijndeal來加密和解密一些數據!但它給了我這個錯誤:rijndeal算法解密給填充錯誤

 
Padding is invalid and cannot be removed. 

我搜索了很多,但沒有任何幫助我解決這個錯誤! 那朵是我的加密/解密代碼:

public string Encrypt(string text) 
    { 

     mainRM = new System.Security.Cryptography.RijndaelManaged(); 
     mainRM.BlockSize = 256; 
     mainRM.KeySize = 256; 
     memorystream = new System.IO.MemoryStream(); 
     ICryptoTransform icrypt = mainRM.CreateEncryptor(key, iv); 
     CryptoStream cryptstream = new CryptoStream(memorystream, icrypt, CryptoStreamMode.Write); 
     cryptstream.FlushFinalBlock(); 
     System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptstream); 
     sw.Write(text); 
     return Convert.ToBase64String(memorystream.ToArray()); 
    } 

    public string Decrypt(string CryptedText) 
    { 
     string custinfo; 
     System.IO.StreamReader streamreader; 
     mainRM = new RijndaelManaged(); 
     mainRM.BlockSize = 256; 
     mainRM.KeySize = 256; 
     memorystream = new System.IO.MemoryStream(Convert.FromBase64String(CryptedText)); 
     ICryptoTransform icrypt = mainRM.CreateDecryptor(key, iv); 
     memorystream.Position = 0; 
     CryptoStream cryptstream = new CryptoStream(memorystream, icrypt, CryptoStreamMode.Read); 
     cryptstream.FlushFinalBlock(); 
     streamreader = new System.IO.StreamReader(cryptstream); 
     custinfo = streamreader.ReadToEnd(); 
     return custinfo; 
    } 

誰能幫助我?

+0

SO用戶在將黑客破解爲試圖解決問題的碎片後,往往會發布他們的代碼。發佈無效的第一個版本,而不是您添加無意義代碼的版本。 – 2013-04-26 12:25:34

+0

無意義的代碼? – 2013-04-26 13:53:03

+0

將文本寫入流之後,是否需要調用FlushFinalBlock()? – 2013-04-26 14:00:54

回答

0

將解密端設置爲不填充。解密並查看你添加到明文末尾的內容。這將告訴你什麼填充加密端添加。更改加密端以顯式設置該填充並將解密端更改爲明確期望相同的填充。

或者同時設置爲PKCS7(aka PKCS5)填充。你需要在兩邊都有相同的填充。