Java和PHP加解密

时间:2022-02-14 20:26:27

PHP代码

 <?php
//DES加解密工具
class DesEncrypt {
var $key;
var $iv;
function DesEncrypt($key, $iv=0) {
$this->key = $key;
if($iv == 0){
$this->iv = $key;
}else{
$this->iv = $iv;
}
}
function encrypt($input) {
$size = mcrypt_get_block_size('des', 'ecb');
$input = $this->pkcs5_pad($input, $size);
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
@mcrypt_generic_init($td, $this->key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data; }
function decrypt($encrypted) {
$encrypted = base64_decode($encrypted);
$td = mcrypt_module_open('des','','ecb','');
//使用MCRYPT_DES算法,cbc模式
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
$key = substr($this->key, 0, $ks);
$rs = @mcrypt_generic_init($td, $key, $iv);
//初始处理
$decrypted = mdecrypt_generic($td, $encrypted);
//解密
mcrypt_generic_deinit($td);
//结束
mcrypt_module_close($td);
$y=$this->pkcs5_unpad($decrypted);
return $y;
}
function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
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);
}
} $key = "123456789012345678901234567890123456"; //36位
$crypt = new DesEncrypt($key);
$str = "中国";
$mstr = $crypt->encrypt($str);
echo "密文:" . $mstr . "<br/>"; $str = $crypt->decrypt($mstr);
echo "明文:" . $str;

结果:

密文:Rm+8trB4CBQ=
明文:中国

Java代码

 package com.util;
import java.security.Key;
import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec; import org.apache.commons.codec.binary.Base64; import com.util.DesEncrypt; /**
* 加密解密
* @author bian
* 2015 上午11:13:36
*/
public class DesEncrypt {
Key key;
public DesEncrypt(String str) {
try{
String key = str;
setKey(key);// 生成密匙
}catch(Exception e){ }
} public DesEncrypt() {
setKey("heimazhifuqw233344");
} /**
* 根据参数生成KEY
*/
public void setKey(String strKey) {
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
this.key = keyFactory.generateSecret(new DESKeySpec(strKey.getBytes("UTF8")));
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
}
} /**
* 加密String明文输入,String密文输出
*/
public String encrypt(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
//BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = new String(Base64.encodeBase64(byteMi), "UTF8");
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
//base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
} /**
* 解密 以String密文输入,String明文输出
*
* @param strMi
* @return
*/
public String decrypt(String strMi) {
///BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = Base64.decodeBase64(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
//base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
} /**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key,SecureRandom.getInstance("SHA1PRNG"));
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
} /**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key,SecureRandom.getInstance("SHA1PRNG"));
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
} public static void main(String args[]) {
String key = "123456789012345678901234567890123456";//36位
String str = "中国";
DesEncrypt des = new DesEncrypt(key); // DES加密
String mStr = des.encrypt(str);
// DES解密
String deStr = des.decrypt(mStr); System.out.println("密文:" + mStr);
System.out.println("明文:" + deStr);
}
}

结果:

密文:Rm+8trB4CBQ=
明文:中国