Panadol Chong
Panadol Chong

Reputation: 1903

Different result of after converting 3DES program from C# to Java

I have a c# program as follow:

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);
        }

My clear text is "4140700104596085", and my key is "222222222222222222222222222222222222222222222222", and this program will return me "0C90320B7B9EC798" as result.

Now I am trying to do it in java code, and here is my code:

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);
  }

As u can see, I comment the cipher.init(Cipher.ENCRYPT_MODE, key, iv);, because I will hit java.security.InvalidAlgorithmParameterException: ECB mode cannot use IV error.

Even I comment it, the result I get is still different, it is f8f8739fb41259d6. I believe something wrong in my code in Java, any ideas?

I tried google on it, but its not that straight forward to translate.

Upvotes: 1

Views: 76

Answers (1)

Panadol Chong
Panadol Chong

Reputation: 1903

I found the solution.

I should follow the code in C#.

Thus, I remove

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++];
      }

And replace with

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

And the fromHexToDecimal method will be as follow:

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;
    }

Upvotes: 1

Related Questions