2013-10-09 50 views
3

我有一個需要被翻譯成C#下面的Java代碼:什麼是Java BouncyCastle AES解密的C#等價物?

public static byte[] encryptAES(byte[] toEncrypt, byte[] key, 
           boolean encrypte) throws Exception { 

    Security.addProvider(new BouncyCastleProvider()); 

    byte[] iv = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; 

    IvParameterSpec salt = new IvParameterSpec(iv); 
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); 

    if (encrypte == false) 
     cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), salt); 
    else 
     cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), salt); 

    byte[] result = cipher.doFinal(toEncrypt); 
    return result; 
} 

你怎麼做等價的:

​​

,什麼是等價的:

IvParameterSpec salt = new IvParameterSpec(iv); 
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); 
+0

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx – SLaks

+0

[你嘗試過什麼?](http://mattgemmell.com/2008/ 12/08 /你試過什麼/) – ntoskrnl

+0

謝謝SLaks。我嘗試過,但沒有好的結果。另一方給了我一個使用BoucyCastle的工作Java示例,所以我想我會使用它。 AESManaged類中沒有PKCS5Padding。 (雖然我不認爲這有什麼區別) – Karel

回答

4

試試看。根據http://social.msdn.microsoft.com/Forums/en-US/13a20d89-7d84-4f7d-8f5c-5ae108a7f5cf/des-encryption-padding-mode-pkcs5?forum=csharplanguage pkcs#5與pkcs#7相同。

public static byte[] EncryptDataAES(byte[] toEncrypt, byte[] key) 
    { 
     byte[] encryptedData; 
     byte[] iv = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 
                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 
                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 
                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; 


     using (SymmetricAlgorithm aes = SymmetricAlgorithm.Create()) 
     { 
      aes.Mode = CipherMode.CBC; 
      aes.Key = key; 
      aes.IV = iv; 
      aes.Padding = PaddingMode.PKCS7; 
      ICryptoTransform encryptor = aes.CreateEncryptor(); 
      using (MemoryStream mStream = new MemoryStream()) 
      { 
       using (CryptoStream cStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write)) 
       { 
        cStream.Write(toEncrypt, 0, toEncrypt.Length); 
        cStream.FlushFinalBlock(); 
        encryptedData = mStream.ToArray(); 
       } 
      } 
     } 
     return encryptedData; 
    } 

解密:

public static string DecryptDataAES(byte[] cipherText, byte[] key, byte[] iv) 
     { 
      string plaintext = null; 

      using (Rijndael rijAlg = Rijndael.Create()) 
      { 
       rijAlg.Key = key; 
       rijAlg.IV = iv; 

       ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); 

       // Create the streams used for decryption. 
       using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
       { 
        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
        { 
         using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 
         { 
          plaintext = srDecrypt.ReadToEnd(); 
         } 
        } 
       } 

      } 

      return plaintext; 
     } 

使用此解碼的KEY是字節十六進制表示。

public static byte[] StringToByteArray(String hex) 
    { 
     int NumberChars = hex.Length/2; 
     byte[] bytes = new byte[NumberChars]; 
     using (var sr = new StringReader(hex)) 
     { 
      for (int i = 0; i < NumberChars; i++) 
       bytes[i] = 
        Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16); 
     } 
     return bytes; 
    } 
+0

我的問題是解密。我收到一個加密(和b64編碼)的字符串,並且必須對它進行dercpt。 我嘗試了類似於您所寫的內容,但是使用AESManaged類進行解密並獲得「填充無效並且無法刪除」。錯誤使用PKCS7。 (加密器使用PKCS5,但是您聲明這是一個子集)。使用無或零填充模式,我得到沒有錯誤,但結果錯誤。 – Karel

+0

您是否使用Convert.FromBase64String(YOURSTRING)轉換字符串;獲取字節數組? –

+0

是的,這就是我使用的: byte [] cryptoBytes = Convert.FromBase64String(b64EncodedCryptoText); – Karel

相關問題