将3DES程序从C#转换为Java后的不同结果

时间:2018-10-01 05:15:24

标签: java c# encryption translate 3des

我有一个c#程序,如下所示:

public static string Encrypt(string sClear, string sKey)
        {
            sClear = "4140700104596085";
            sKey = "222222222222222222222222222222222222222222222222";

            TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();
            cryptoProvider.Mode = CipherMode.ECB;

            byte[] bData = new byte[16];
            byte[] bKey = new byte[24];

            // Convert from hex to decimal
            FromHexToDecimal(sClear).CopyTo(bData, 0);
            FromHexToDecimal(sKey).CopyTo(bKey, 0);

            ICryptoTransform cTransform = DESCryptoExtensions.CreateWeakEncryptor(cryptoProvider, bKey, cryptoProvider.IV);
            byte[] result = cTransform.TransformFinalBlock(bData, 0, bData.Length);

            return BitConverter.ToString(result).Replace("-", "").Substring(0, 16);
        }

我的明文是“ 4140700104596085”,密钥是“ 222222222222222222222222222222222222222222222”,该程序将返回“ 0C90320B7B9EC798”。

现在,我正在尝试用Java代码完成操作,这是我的代码:

public static String encryptToString(String message) throws Exception {
      message = "4140700104596085";
      final MessageDigest md = MessageDigest.getInstance("md5");
      final byte[] digestOfPassword = md.digest("222222222222222222222222222222222222222222222222"
              .getBytes("utf-8"));
      final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
      for (int j = 0, k = 16; j < 8;) {
          keyBytes[k++] = keyBytes[j++];
      }

      final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
      final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
      final Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
      cipher.init(Cipher.ENCRYPT_MODE, key);
      // cipher.init(Cipher.ENCRYPT_MODE, key, iv);

      final byte[] plainTextBytes = message.getBytes("utf-8");
      final byte[] cipherText = cipher.doFinal(plainTextBytes);

      return Hex.encodeHexString(cipherText).substring(0, 16);
  }

如您所见,我评论了cipher.init(Cipher.ENCRYPT_MODE, key, iv);,因为我会遇到java.security.InvalidAlgorithmParameterException: ECB mode cannot use IV错误。

即使我发表评论,我得到的结果仍然不同,是f8f8739fb41259d6。我相信我用Java编写的代码有问题,有什么想法吗?

我尝试过使用google,但翻译起来并不那么直接。

1 个答案:

答案 0 :(得分:1)

我找到了解决方法。

我应该遵循C#中的代码。

因此,我删除了

final MessageDigest md = MessageDigest.getInstance("md5");
      final byte[] digestOfPassword = md.digest("222222222222222222222222222222222222222222222222"
              .getBytes("utf-8"));
      final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
      for (int j = 0, k = 16; j < 8;) {
          keyBytes[k++] = keyBytes[j++];
      }

并替换为

final byte[] keyBytes = fromHexToDecimal("222222222222222222222222222222222222222222222222");

fromHexToDecimal方法如下:

public static byte[] fromHexToDecimal(String hex) {

      int len = hex.length() / 2;
      byte[] result = new byte[len];

      for (int i = 0, j = 0; i < len; i++, j = j + 2) {
        StringBuilder c = new StringBuilder();
        c = c.append(hex.charAt(j)).append(hex.charAt(j+1));
        short s = (short) Integer.parseInt(c.toString(), 16);
        result[i] = (byte)(s & 0xff);
      }


      return result;
    }