首页 >后端开发 >php教程 >满分求php3des加密的问题 兼容JAVA

满分求php3des加密的问题 兼容JAVA

WBOY
WBOY原创
2016-06-23 14:21:201113浏览

3des 

对方提供的密钥
56781234D56F012BCD5E701A3CDD6709

需要用PHP和他实现同样的加密 结果转化成16进制 

也就是如果有8位 例如 00000000
加密后转化成16进制就是16位

他们提供一个软件 我对12345678 进行加密
先将12345678 用 bin2hex转化成16进制字符串 结果为 3132333435363738
然后进行加密 得到的结果为 B2A85CF088D9FF03
如图:


现在用要PHP实现和它相同的加密结果!我试了很多次都对应不上,求大神贴代码
附JAVA加密代码:
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');

答案贴出来 留给其他有疑惑的同学

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn