2

I have a 3des encrypted hex string which I get from a server. When I decrypt it with Java ECB/NoPadding, I get the expected value, but when I try to decrypt it with PHP I am getting a different value. This is the PHP code:

<?php

$key = "428982f4658cfeb679bae2bf85ebcde9";
$input = "9CB39217A434F6339EF27C503B561342";
$encrypted_data = bin2hex(
    mcrypt_ecb(MCRYPT_3DES, pack("H*", $key), pack("H*", $input), MCRYPT_DECRYPT)
);

?>

and part of the java code starting with main:

String input = "F414E86894D996F8658F9327C8EC786404192012174729";
String key = "05aa58f38a0883d1439750a2495d36ad";

byte2hex(doCryto("F414E86894D996F8658F9327C8EC7864",  "05aa58f38a0883d1439750a2495d36ad", Cipher.DECRYPT_MODE));

public static byte[] doCryto(String data, String key, int CipherMode) throws Exception {
    byte result[];
    try {
        byte data1[] = hex2byte(data);
        Key key1 = formDESKey(key);
        transformation = key1.getAlgorithm() + "/ECB/NoPadding";

        Cipher c1 = Cipher.getInstance(transformation);
        c1.init(CipherMode, key1);
        result = c1.doFinal(data1);
    } catch (Exception e) {
        throw new Exception(e);
    }
    return result;
}

public static Key formDESKey(String key) throws Exception {
    short len = (short) ((key.length() / 2) * 8);
    return formDESKey(len, hex2byte(key));
}

  public static Key formDESKey(short keyLength, byte clearKeyBytes[]) throws Exception {
    Key key = null;
    switch (keyLength) {
        case 64: // '@'
            key = new SecretKeySpec(clearKeyBytes, "DES");
            break;

        case 128:
            clearKeyBytes = concat(clearKeyBytes, 0, 128/8, clearKeyBytes, 0, getBytesLength((short) 64));
        // fall through

        case 192:
            key = new SecretKeySpec(clearKeyBytes, "DESede");
            break;
    }
    if (key == null) {
        throw new Exception("Unsupported DES key length: " + keyLength + " bits");
    } else {
        return key;
    }
}

private static byte[] concat(byte array1[], int beginIndex1, int length1, byte array2[], int beginIndex2, int length2) {
    byte concatArray[] = new byte[length1 + length2];
    System.arraycopy(array1, beginIndex1, concatArray, 0, length1);
    System.arraycopy(array2, beginIndex2, concatArray, length1, length2);
    return concatArray;
}

Java results in: 31303031464646464646464646464646
While PHP results in: 411C689D687F2F118EA05BF234F4E582

I don't know why PHP is getting a different value. Any help would be appreciated.
Thanks.

Olantobi
  • 869
  • 1
  • 8
  • 16
  • You're putting many functions into each other in your PHP code. As PHP normally does not throw exceptions, that's really fargile and makes your code hard to debug so hard for you to say where the error is. Assign all function return values to variables and check them before continue to use the values. Also the `@` operator even suppressed any warnings you might have gotten - I removed it for now and please don't post code here in questions containing the `@` operator. Thank you. – hakre Apr 20 '12 at 08:10
  • @SUNNYben: Please post working code. Your code calls `formDESKey` with just a single parameter. But the method takes two parameters. – Codo Apr 20 '12 at 08:58
  • @Codo Sorry I missed a method, i've added it now. – Olantobi Apr 20 '12 at 12:03
  • 1
    You mean, outside the fact that the key is different? – Maarten Bodewes Apr 21 '12 at 10:30

0 Answers0