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

Net和Java中三重DES加密的结果不同,java,.net,vb.net,encryption,Java,.net,Vb.net,Encryption,我正在尝试用Java和.Net加密字符串。但问题是两种算法产生的结果不同。我使用三重DES加密算法。 它应该会产生同样的结果 我的.Net方法: 公共函数EncryptTripleDES(ByVal sIn作为字符串,ByVal sKey作为字符串)作为字符串 Dim DES作为新的System.Security.Cryptography.TripleDESCryptoServiceProvider Dim hashMD5作为新的System.Security.Cryptography.MD5C

我正在尝试用Java和.Net加密字符串。但问题是两种算法产生的结果不同。我使用三重DES加密算法。 它应该会产生同样的结果

我的.Net方法:

公共函数EncryptTripleDES(ByVal sIn作为字符串,ByVal sKey作为字符串)作为字符串
Dim DES作为新的System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim hashMD5作为新的System.Security.Cryptography.MD5CryptoServiceProvider
"抢钥匙"
'计算MD5哈希。
DES.Key=hashMD5.ComputeHash(System.Text.ascienceoding.ASCII.GetBytes(sKey))
'设置密码模式。
DES.Mode=System.Security.Cryptography.CipherMode.ECB
'创建加密程序。
Dim DESEncrypt As System.Security.Cryptography.ICryptoTransform=DES.CreateEncryptor()
'获取字符串的字节数组。
作为字节()的Dim缓冲区=System.Text.AscienceODing.ASCII.GetBytes(sIn)
'转换并返回字符串。
返回Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer,0,Buffer.Length))
端函数
我的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;
    }
}

谢谢大家的支持。我找到了答案,这是我的答案。这个VB.NET方法工作得很好。我唯一的错误是我的VB.NET方法使用AnsienceODing,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

您发布了一个java类和一个C#方法,因此我们无法判断您是否向C#方法传递了正确的参数。您正在使用C#中的ASCII和来自Java的UTF-8来获取看起来错误的密钥字节。我会先检查这些GetBytes调用的结果,然后再互相比较。现在不要使用三重DES。它最多只能提供112位的安全性,即使您使用192位的最大密钥大小。如果使用较短的密钥大小,则仅提供56或57位的安全性。AES将更快(处理器有一个特殊的AES-NI指令集),并且使用128位的最小密钥大小将更加安全。3DES的最大密文大小也有实际限制。看,永远不要使用。它是确定性的,因此在语义上不安全。您至少应该使用随机模式,如或。最好是对密文进行身份验证,这样就不可能进行类似的攻击。这可以通过诸如GCM或EAX之类的身份验证模式或方案来完成。一般建议:始终使用完全限定的密码字符串
Cipher.getInstance(“DESede”)可能会产生不同的密码。它很可能导致
“DESede/ECB/pkcs5p添加”
,但不一定非得如此。如果它发生变化,您将失去不同JVM之间的兼容性。如果您只对加密感兴趣,而对安全性不感兴趣,那么确定。如果需要安全性,请不要因为3DES不安全而使用3DES,请使用AES,不要因为不安全而使用ECB模式,请使用随机iv的CBC。请参阅Artjom B的评论。不要使用ECB模式,因为它不安全,请参阅,向下滚动到企鹅。非常感谢@zaph的宝贵评论。将来我会照顾它的。我的问题是,加密逻辑是由我的客户机(Java)决定的,我必须编写其等效的解密逻辑。这是另一个由于不好的原因导致安全性差的情况,显然我们不是专业人士。