2017-03-08 47 views
1

您好,我想加密和解密文本。我的加密代碼工作正常,並匹配我想要的值。但是當我想解密這是給錯誤padding is invalid and cannot be removed。在下面的代碼中,我首先給出了我的加密和解密代碼。此外,我必須修復這個錯誤Stack overflow link,StackoverlFlow Link 2但沒有解決它。填充無效且無法刪除解密值

string getHashKey1 = EncryptText("10002:1486703720424", "hpIw4SgN)TxJdoQj=GKo)p83$uHePgoF"); 

結果= 1ltQFLRGNif73uCNzi0YEvBqLKiRgx6fWsk5e/GcTQc=

string reverseKey = DecryptText('1ltQFLRGNif73uCNzi0YEvBqLKiRgx6fWsk5e/GcTQc=', "hpIw4SgN)TxJdoQj=GKo)p83$uHePgoF"); 

當我在AES_DECRYPT aes.Padding = PaddingMode.Zeros添加;我得到低於結果。 結果: - ����y�7�t���Ij���,���� Z��$�

public string EncryptText(string input, string password) 
    { 
     string result = ""; 
     try 
     { 
      // Get the bytes of the string 
      byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input); 
      byte[] passwordBytes = Encoding.UTF8.GetBytes(password); 

      byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes); 

      result = Convert.ToBase64String(bytesEncrypted); 
      return result; 
     } 
     catch (Exception ex) 
     { 

     } 

     return result; 
    } 

public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) 
    { 
     byte[] encryptedBytes = null; 
     try 
     {  
      using (MemoryStream ms = new MemoryStream()) 
      { 
       using (Aes aes = Aes.Create()) 
       { 
        aes.Key = passwordBytes; 
        aes.Mode = CipherMode.ECB; 

        // "zero" IV 
        aes.IV = new byte[16]; 

        using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) 
        { 
         cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); 
         cs.Close(); 
        } 
        encryptedBytes = ms.ToArray(); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 

     }    
     return encryptedBytes; 
    } 

上面的代碼工作正常進行加密。 下面的代碼是給錯誤 padding is invalid and cannot be removed

public string DecryptText(string input, string password) 
    { 
     // Get the bytes of the string 
     byte[] bytesToBeDecrypted = Convert.FromBase64String(input); 
     byte[] passwordBytes = Encoding.UTF8.GetBytes(password); 
     passwordBytes = SHA256.Create().ComputeHash(passwordBytes); 

     byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes); 

     string result = Encoding.UTF8.GetString(bytesDecrypted); 

     return result; 
    } 


public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes) 
    { 
     byte[] decryptedBytes = null; 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (Aes aes = Aes.Create()) 
      { 
       aes.Key = passwordBytes; 
       aes.Mode = CipherMode.ECB; 
       aes.IV = new byte[16];     

       using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) 
       { 
        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length); 
        cs.Close(); // here i am getting error 
       } 
       decryptedBytes = ms.ToArray(); 
      } 
     } 

     return decryptedBytes; 
    } 
+0

當你解密passwordBytes = SHA256.Create()。ComputeHash(passwordBytes);',但不是當加密時加密 – pedrofb

+0

@pedrofb我解密時需要做什麼 –

+0

@pedrofb謝謝我刪除了該行現在它可以工作。謝謝 –

回答

1

你是散列密碼,當你解密,

passwordBytes = SHA256.Create().ComputeHash(passwordBytes); 

而不是在加密。這意味着您在使用不同的密碼

+0

感謝您的答案。我已經給了我3個小時來發現錯誤 –

3

你有兩個問題:

1)(已經指出的pedrofb):您可以使用UTF8.GetBytes在加密,但解密SHA256(UTF8.GetBytes())

您不應該執行這兩種方法中的任何一種,而應該使用適當的基於密碼的密鑰派生函數,例如PBKDF2。在.NET中,PBKDF2可通過Rfc2898DeriveBytes類獲得。

byte[] salt = 8 or more bytes that you always pass in as the same. 
// (salt could be fixed for your application, 
// but if you have users it should be unique per user and stored along with the output value) 
int iterations = 100000; 
// Or bigger. If you were making a user management system you 
// should write this number down, too, so you can increase it over time; 
// it should be whatever number makes it take 100ms or more on the fastest relevant computer) 
Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations); 
passwordBytes = pbkdf2.GetBytes(16); // 16 = AES128, 24 = AES192, 32 = AES256. 

2)在加密中使用Base64編碼,但在解密中使用UTF8.GetBytes。

獎金問題:

3)您使用的電子密碼本(ECB)鏈接。建議使用密碼塊鏈接(CBC)而不是ECB。若要正確使用CBC,請在加密時創建一個隨機初始化向量(IV)(創建新的Aes對象時自動完成,或者如果重新使用該對象,則可以調用GenerateIV()進行加密)。然後,您可以將IV(對AES始終爲16字節)預先添加到密文中。在解密過程中,您可以a)截斷前16個字節並將其分配爲IV(然後解密其餘數據)或b)解密整個blob並忽略解密輸出的前16個字節。

相關問題