3des加密 java php_php 3DES加密如何兼容Java

Java源码:

import java.security.Security;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class ThreeDES {

private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish

//keybyte为加密密钥,长度为24字节

//src为被加密的数据缓冲区(源)

public static byte[] encryptMode(byte[] keybyte, byte[] src) {

try {

//生成密钥

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

//加密

Cipher c1 = Cipher.getInstance(Algorithm);

c1.init(Cipher.ENCRYPT_MODE, deskey);

return c1.doFinal(src);

} catch (java.security.NoSuchAlgorithmException e1) {

e1.printStackTrace();

} catch (javax.crypto.NoSuchPaddingException e2) {

e2.printStackTrace();

} catch (java.lang.Exception e3) {

e3.printStackTrace();

}

return null;

}

//keybyte为加密密钥,长度为24字节

//src为加密后的缓冲区

public static byte[] decryptMode(byte[] keybyte, byte[] src) {

try {

//生成密钥

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

//解密

Cipher c1 = Cipher.getInstance(Algorithm);

c1.init(Cipher.DECRYPT_MODE, deskey);

return c1.doFinal(src);

} catch (java.security.NoSuchAlgorithmException e1) {

e1.printStackTrace();

} catch (javax.crypto.NoSuchPaddingException e2) {

e2.printStackTrace();

} catch (java.lang.Exception e3) {

e3.printStackTrace();

}

return null;

}

//转换成十六进制字符串

public static String byte2hex(byte[] b) {

String hs="";

String stmp="";

for (int n=0;n

来源:http://www.cnblogs.com/mailin...

回复内容:

Java源码:

import java.security.Security;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class ThreeDES {

private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish

//keybyte为加密密钥,长度为24字节

//src为被加密的数据缓冲区(源)

public static byte[] encryptMode(byte[] keybyte, byte[] src) {

try {

//生成密钥

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

//加密

Cipher c1 = Cipher.getInstance(Algorithm);

c1.init(Cipher.ENCRYPT_MODE, deskey);

return c1.doFinal(src);

} catch (java.security.NoSuchAlgorithmException e1) {

e1.printStackTrace();

} catch (javax.crypto.NoSuchPaddingException e2) {

e2.printStackTrace();

} catch (java.lang.Exception e3) {

e3.printStackTrace();

}

return null;

}

//keybyte为加密密钥,长度为24字节

//src为加密后的缓冲区

public static byte[] decryptMode(byte[] keybyte, byte[] src) {

try {

//生成密钥

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

//解密

Cipher c1 = Cipher.getInstance(Algorithm);

c1.init(Cipher.DECRYPT_MODE, deskey);

return c1.doFinal(src);

} catch (java.security.NoSuchAlgorithmException e1) {

e1.printStackTrace();

} catch (javax.crypto.NoSuchPaddingException e2) {

e2.printStackTrace();

} catch (java.lang.Exception e3) {

e3.printStackTrace();

}

return null;

}

//转换成十六进制字符串

public static String byte2hex(byte[] b) {

String hs="";

String stmp="";

for (int n=0;n

来源:http://www.cnblogs.com/mailin...

已解决 自问自答

class EasyCrypt3Des {

private $_key = "helloworld";

public function __construct($key=null) {

if($key !== null)

$this->_key = $key;

}

public function encrypt($str) {

$td = $this->gettd();

$ret = mcrypt_generic($td, $this->pkcs5_pad($str, 8));

mcrypt_generic_deinit($td);

mcrypt_module_close($td);

return $this->strToHex($ret);

}

public function decrypt($str) {

$td = $this->gettd();

$ret = $this->pkcs5_unpad(mdecrypt_generic($td, $this->hexToStr($str)));

mcrypt_generic_deinit($td);

mcrypt_module_close($td);

return $ret;

}

private function pkcs5_pad($text, $blocksize) {

$pad = $blocksize - (strlen($text) % $blocksize);

return $text . str_repeat(chr($pad), $pad);

}

private function pkcs5_unpad($text) {

$pad = ord($text{strlen($text) - 1});

if ($pad > strlen($text)) {

return false;

}

if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {

return false;

}

return substr($text, 0, -1 * $pad);

}

private function getiv() {

return pack('H16', '0000000000000000');

}

private function gettd() {

$iv = $this->getiv();

$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');

mcrypt_generic_init($td, $this->_key, $iv);

return $td;

}

private function strToHex($string){

$hex = '';

for ($i=0; $i

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值