php 3DES加密如何兼容Java
程序员文章站
2024-01-23 17:35:28
...
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
上一篇: curl post的时分 修改请求头 遇到点东西 理解不了
下一篇: 动态代理继承的图文实例
推荐阅读
-
php 3DES加密如何兼容Java
-
php如何生成加密公钥以及加密私钥的代码案例分享
-
加密解密 - Java Aes 类,可否用 php 实现,求助于懂 Java 代码的 php 程序猿
-
zend加密的php如何运行
-
PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决办法
-
麻烦大哥帮看看这个PHP文件是用什么加密的?如何解密?
-
如何用PHP代码实现这个Java代码所实现的。
-
如何批量清理系统临时文件语言:C#、 C/C++、 php 、python 、java
-
用java实现3des加密
-
请问PHP如何通过openssl实现AES-128加密算法?