2016-08-04 63 views
0

我想加密Java和.Net中的字符串。但問題是這兩種算法產生不同的結果。我正在使用三重DES加密算法。 它應該產生相同的結果。.Net和Java中三重DES加密的結果不同

我的.NET方法:

Public Function EncryptTripleDES(ByVal sIn As String, ByVal sKey As String) As String 
    Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider 
    Dim hashMD5 As New System.Security.Cryptography.MD5CryptoServiceProvider 
    ' scramble the key 
      ' Compute the MD5 hash. 
    DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey)) 
    ' Set the cipher mode. 
    DES.Mode = System.Security.Cryptography.CipherMode.ECB 
    ' Create the encryptor. 
    Dim DESEncrypt As System.Security.Cryptography.ICryptoTransform = DES.CreateEncryptor() 
    ' Get a byte array of the string. 
    Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn) 
    ' Transform and return the string. 
    Return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)) 
End Function 

我的Java類:

public class TrippleDESEncryption { 
    private static final String UNICODE_FORMAT = "UTF8"; 
    public static final String DESEDE_ENCRYPTION_SCHEME = "DESede"; 
    private KeySpec keySpec; 
    private SecretKeyFactory secretKeyFactory; 
    private Cipher cipher; 
    byte[] keyAsBytes; 
    private String encryptionKey; 
    private String encryptionScheme; 
    SecretKey key; 

    public TrippleDESEncryption() throws Exception { 
      encryptionKey = "234342343423434234342343"; 
      encryptionScheme = DESEDE_ENCRYPTION_SCHEME; 
      keyAsBytes = encryptionKey.getBytes(UNICODE_FORMAT); 
      keySpec = new DESedeKeySpec(keyAsBytes); 
      secretKeyFactory = SecretKeyFactory.getInstance(encryptionScheme); 
      cipher = Cipher.getInstance(encryptionScheme); 
      key = secretKeyFactory.generateSecret(keySpec); 

    } 

    /** 
    * Method To Encrypt The String 
    */ 
    public String encrypt(String unencryptedString) { 
      String encryptedString = null; 
      try { 
       cipher.init(Cipher.ENCRYPT_MODE, key); 
       byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT); 
       byte[] encryptedText = cipher.doFinal(plainText); 
       BASE64Encoder base64encoder = new BASE64Encoder(); 
       encryptedString = base64encoder.encode(encryptedText); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return encryptedString; 
    } 
} 
+0

你已經發布了一個java類與C#方法,所以我們不能告訴你正確的參數傳遞給C#方法。您使用C#中的ASCII和Java中的UTF-8來獲取看起來錯誤的關鍵字節。我將首先檢查這些GetBytes調用的結果。 –

+1

**現在不要使用三重DES。**即使使用最大的192位密鑰,它也只能提供最高112位的安全性。如果使用較短的密鑰大小,則它只提供56或57位的安全性。 AES的速度會更快(處理器有一個特殊的AES-NI指令集),而且最低128位的密鑰更加安全。 3DES的最大密文大小也有實際的限制。請參閱[3DES和AES的安全性比較](http://security.stackexchange.com/q/26179/45523)。 –

+1

**切勿使用[ECB模式](http://crypto.stackexchange.com/q/14487/13022)**。它是確定性的,因此不具有語義安全性。您至少應該使用[CBC](http://crypto.stackexchange.com/q/22260/13022)或[CTR](http://crypto.stackexchange.com/a/2378/)這樣的隨機模式。 13022)。最好是對密文進行身份驗證,以便像[padding oracle attack](http://crypto.stackexchange.com/q/18185/13022)這樣的攻擊是不可能的。這可以通過驗證模式(如GCM或EAX)或[加密 - 然後MAC](http://crypto.stackexchange.com/q/202/13022)方案完成。 –

回答

-1

謝謝大家對你的支持。我找到解決方案,這裏是我的解決方案。這VB.NET方法工作fine.My唯一的錯誤是,我的VB.NET方法使用ANSIEncoding和Java類使用UTF8。

Public Function DecryptTripleDES(ByVal sOut As String, ByVal sKey As String) As String 
    Try 
     Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider 
     DES.Key = UTF8Encoding.UTF8.GetBytes(sKey) 
     ' Set the cipher mode. 
     DES.Mode = System.Security.Cryptography.CipherMode.ECB 
     ' Create the decryptor. 
     Dim DESDecrypt As System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor() 
     Dim Buffer As Byte() = Convert.FromBase64String(sOut) 
     ' Transform and return the string. 
     Return System.Text.UTF8Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)) 
    Catch ex As Exception 
     Throw New Exception() 
    End Try 
End Function 
+0

好吧如果你只對加密感興趣而不是安全感。如果因爲不安全而需要使用3DES而不使用3DES,請使用AES,不要使用ECB模式,因爲它不安全,請隨意使用CBC。請參閱Artjom B的評論。不要使用ECB模式,它不安全,請參閱[ECB模式](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29),向下滾動到企鵝。 – zaph

+0

非常感謝您的寶貴意見。將來我會照顧它。我的問題是加密邏輯是由我的客戶端(Java)決定的,我必須編寫相應的解密邏輯。 – Ajeet

+0

由於不好的原因,只是另一個安全性差的案例,顯然我們不是專業人士。 – zaph