Scripts 学盟

标题: 关于 php 中的 3DES [打印本页]

作者: 混混@普宁.中国    时间: 2011-7-29 00:16:38     标题: 关于 php 中的 3DES

因为与php 的系统交互,需要用到 3des 加密


下面是 php 加密码代码:
  1. function trpleDES_encrypt($input, $key)
  2. {
  3.         $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  4.         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  5.         mcrypt_generic_init($td, $key, $iv);
  6.         $encrypted_data = mcrypt_generic($td, $input);
  7.         mcrypt_generic_deinit($td);
  8.         mcrypt_module_close($td);
  9.         return $encrypted_data;
  10. }
复制代码
搞了一个晚上。。。加密结果就是不一样。。。扑街了。。。

耗了一个晚上,才发现,是 php  加密时,原文 padding 方式不一样。。。

不费话了,java 代码:
  1.         public static byte[] zerosPadding(byte[] bytes) {
  2.                 byte[] result = new byte[((bytes.length+7)/ 8) * 8];
  3.                 System.arraycopy(bytes, 0, result, 0, bytes.length);
  4.                 return result;
  5.         }
  6.        
  7.         public static byte[] encrypt(byte[] input,  byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ShortBufferException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
  8.                 Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
  9.                 cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "DESede"));
  10.                 return cipher.doFinal(zerosPadding(input));
  11.         }
复制代码
就这么点东东搞了那么久,还浪费了俺的200分CSDN可用分


作者: 那个谁    时间: 2011-7-29 12:32:29


作者: 混混@普宁.中国    时间: 2011-7-29 15:04:11






欢迎光临 Scripts 学盟 (http://www.iscripts.org/) Powered by Discuz! X2