Scripts 学盟
标题:
关于 php 中的 3DES
[打印本页]
作者:
混混@普宁.中国
时间:
2011-7-29 00:16:38
标题:
关于 php 中的 3DES
因为与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可用分
作者:
那个谁
时间:
2011-7-29 12:32:29
作者:
混混@普宁.中国
时间:
2011-7-29 15:04:11
欢迎光临 Scripts 学盟 (http://www.iscripts.org/)
Powered by Discuz! X2