Note that there are some explanatory texts on larger screens.

plurals
  1. PODecrypting Java 3DES ECB NoPadding encrypted value in PHP
    text
    copied!<p>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:</p> <pre><code>&lt;?php $key = "428982f4658cfeb679bae2bf85ebcde9"; $input = "9CB39217A434F6339EF27C503B561342"; $encrypted_data = bin2hex( mcrypt_ecb(MCRYPT_3DES, pack("H*", $key), pack("H*", $input), MCRYPT_DECRYPT) ); ?&gt; </code></pre> <p>and part of the java code starting with main:</p> <pre><code>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; } </code></pre> <p>Java results in: 31303031464646464646464646464646 <br/> While PHP results in: 411C689D687F2F118EA05BF234F4E582</p> <p>I don't know why PHP is getting a different value. Any help would be appreciated.<br/> Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload