- 注册时间
 - 2011-3-21
 - 最后登录
 - 2025-4-11
 - 在线时间
 - 1191 小时
 - 阅读权限
 - 200
 - 积分
 - 9459
 - 帖子
 - 1256
 - 精华
 - 0
 - UID
 - 1
  
 
 
 
    
 | 
因为与php 的系统交互,需要用到 3des 加密 
 
 
下面是 php 加密码代码:- function trpleDES_encrypt($input, $key)
 
 - {
 
 -         $td = mcrypt_module_open('tripledes', '', 'ecb', '');
 
 -         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 
 -         mcrypt_generic_init($td, $key, $iv);
 
 -         $encrypted_data = mcrypt_generic($td, $input);
 
 -         mcrypt_generic_deinit($td);
 
 -         mcrypt_module_close($td);
 
 -         return $encrypted_data;
 
 - }
 
  复制代码 搞了一个晚上。。。加密结果就是不一样。。。扑街了。。。 
 
耗了一个晚上,才发现,是 php  加密时,原文 padding 方式不一样。。。 
 
不费话了,java 代码:-         public static byte[] zerosPadding(byte[] bytes) {
 
 -                 byte[] result = new byte[((bytes.length+7)/ 8) * 8];
 
 -                 System.arraycopy(bytes, 0, result, 0, bytes.length);
 
 -                 return result;
 
 -         }
 
 -         
 
 -         public static byte[] encrypt(byte[] input,  byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ShortBufferException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
 
 -                 Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
 
 -                 cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "DESede"));
 
 -                 return cipher.doFinal(zerosPadding(input));
 
 -         }
 
  复制代码 就这么点东东搞了那么久,还浪费了俺的200分CSDN可用分 
 
   |   
 
  
 
 |