Maison > Article > développement back-end > 满分求php3des加密的问题 兼容JAVA
3des
对方提供的密钥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(""); }}
1楼自己占,在线等!有思路的也欢迎
自己搞定了 我操 这一百分给谁呀
//echo bin2hex('88888888');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[]{1,2,3,4,5,6,7,8}
$iv = pack('H10','0000000000000000');
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('88888888');
答案贴出来 留给其他有疑惑的同学