Full score php3des encryption compatibility with JAVA 3des
Key provided by the other party
56781234D56F012BCD5E701A3CDD6709
You need to use PHP to convert the same encryption result to a hexadecimal format.
That is, if there are 8 bits, such as 00000000
The encrypted data is converted to a hexadecimal value, which is 16 bits.
They provide a software I encrypt 12345678
First, convert 12345678 to a hexadecimal string using bin2hex and the result is 3132333435363738.
The encrypted result is B2A85CF088D9FF03.
Now we need PHP to implement the same encryption result as it! I tried it many times but I couldn't match it. please paste the code
With the JAVA encryption code:
package com.huateng.crypto.des;import com.huateng.crypto.CryptoException;import java.io.PrintStream;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.spec.InvalidKeySpecException;import java.util.Arrays;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESedeKeySpec;import javax.crypto.spec.IvParameterSpec;public class TripleDesTool{ public static byte[] decrypt(byte[] input, byte[] key) throws CryptoException { byte[] decryptedData; try { SecretKeyFactory keyFactory = null; try { keyFactory = SecretKeyFactory.getInstance("DESede"); } catch (NoSuchAlgorithmException ex) { throw new CryptoException(ex.getMessage(), ex); } SecretKey secretKey = null; try { secretKey = keyFactory.generateSecret(new DESedeKeySpec(key)); } catch (InvalidKeySpecException ex) { throw new CryptoException(ex.getMessage(), ex); } Cipher cipher = null; try { cipher = Cipher.getInstance("DESede/CBC/NoPadding"); } catch (NoSuchPaddingException ex) { throw new CryptoException(ex.getMessage(), ex); } catch (NoSuchAlgorithmException ex) { throw new CryptoException(ex.getMessage(), ex); } IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }); try { cipher.init(2, secretKey, iv); } catch (InvalidAlgorithmParameterException ex) { throw new CryptoException(ex.getMessage(), ex); } try { decryptedData = cipher.doFinal(input); } catch (BadPaddingException ex) { throw new CryptoException(ex.getMessage(), ex); } catch (IllegalBlockSizeException ex) { throw new CryptoException(ex.getMessage(), ex); } catch (IllegalStateException ex) { throw new CryptoException(ex.getMessage(), ex); } } catch (InvalidKeyException ex) { throw new CryptoException(ex.getMessage(), ex); } return decryptedData; } public static byte[] encrypt(byte[] input, byte[] key) throws CryptoException { int len = (input.length / 8 + (input.length % 8 == 0 ? 0 : 1)) * 8; byte[] plainData = new byte[len]; byte[] encryptedData = new byte[8]; Arrays.fill(plainData, 32); System.arraycopy(input, 0, plainData, 0, input.length); try { DESedeKeySpec dks = null; dks = new DESedeKeySpec(key); SecretKeyFactory keyFactory = null; try { keyFactory = SecretKeyFactory.getInstance("DESede"); } catch (NoSuchAlgorithmException ex) { throw new CryptoException(ex.getMessage(), ex); } SecretKey secretKey = null; try { secretKey = keyFactory.generateSecret(dks); } catch (InvalidKeySpecException ex) { throw new CryptoException(ex.getMessage(), ex); } Cipher cipher = null; try { cipher = Cipher.getInstance("DESede/CBC/NoPadding"); } catch (NoSuchPaddingException ex) { throw new CryptoException(ex.getMessage(), ex); } catch (NoSuchAlgorithmException ex) { throw new CryptoException(ex.getMessage(), ex); } IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }); try { cipher.init(1, secretKey, iv); } catch (InvalidAlgorithmParameterException ex) { throw new CryptoException(ex.getMessage(), ex); } try { encryptedData = cipher.doFinal(plainData); } catch (BadPaddingException ex) { throw new CryptoException(ex.getMessage(), ex); } catch (IllegalBlockSizeException ex) { throw new CryptoException(ex.getMessage(), ex); } catch (IllegalStateException ex) { throw new CryptoException(ex.getMessage(), ex); } } catch (InvalidKeyException ex) { throw new CryptoException(ex.getMessage(), ex); } return encryptedData; } public static void main(String[] args) throws Exception { byte[] input = { 1, -100, 24, 68, -33, -12, -45, -4 }; for (int i = 0; i < input.length; i++) { System.out.print(input[i] + ","); } System.out.println(""); System.out.println(""); byte[] output1 = decrypt(input, new byte[] { -118, -59, -33, 70, 60, 29, -123, 70, 35, 53, -85, -88, 90, 20, -3, 111, -118, -59, -33, 70, 60, 29, -123, 70 }); for (int i = 0; i < output1.length; i++) { System.out.print(output1[i] + ","); } System.out.println(""); }}
Thank you!
Reply to discussion (solution)
Occupied by yourself on the first floor, online and so on! If you have any ideas, you are welcome.
I have done it myself. who is the fuck?
// Echo bin2hex ('20140901'); exit;
$ Key = '56781234d56f012bcd5e701a3cdd670956781234d56f012b ';
$ Key = pack ('h48', $ key );
// Echo $ key; exit;
$ Iv = pack ('H * ', '0 ');
Function fmt3DESEx ($ s ){
$ Key = '56781234d56f012bcd5e701a3cdd670956781234d56f012b ';
$ Key = pack ('h48', $ key );
$ Td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_CBC ,'');
// $ Iv = pack ('h10', "0102030405060708"); // like c # new byte}
$ Iv = pack ('h10', '123 ');
Mcrypt_generic_init ($ td, $ key, $ iv );
$ Encrypted_data = mcrypt_generic ($ td, $ s );
Mcrypt_generic_deinit ($ td );
Mcrypt_module_close ($ td );
Return bin2hex ($ encrypted_data );
}
Echo fmt3DESEx ('20140901 ');
Post the answers to other questions