.Net和Java中的三重DES加密结果不同

时间:2016-08-04 07:26:38

标签: java .net vb.net encryption

我正在尝试加密Java和.Net中的字符串。但问题是两种算法都会产生不同的结果。我正在使用Triple 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;
    }
}

1 个答案:

答案 0 :(得分:-1)

谢谢大家的支持。我找到解决方案,这是我的解决方案。这个VB.NET方法运行正常。我唯一的错误就是我的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