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=
明文:中国

最新文章

  1. 【APP设计利器】Sketch 41 Mac中文破解版(含汉化插件)
  2. Keychain group access
  3. 我的一个小作品 android App ---校园资讯助手
  4. ORACLE用SYS登录报ORA-28009:connection as SYS should be as SYSDBA OR SYSOPER解决方法
  5. (转)Java + Excel 接口自动化
  6. Python3中的新特性(3)——代码迁移与2to3
  7. 通过cagradientLayer类封装uiimageview动画色度差
  8. 【POJ】【3071】Football
  9. 【LA2796】Concert Hall Scheduling(最大费用最大流)
  10. PHP删除数组中特定元素
  11. Okhttp3日志采集功能
  12. Bootstrap面包屑导航
  13. openwrt增加串口登录需要密码
  14. Java中正则表达式的几种用法
  15. markdown基础
  16. 第1章 ssh和SSH服务(包含隧道内容)
  17. Maven 学习总结 (七) 之 灵活构建
  18. Pearson 相关系数--最佳理解及相关应用
  19. mysql索引知识简单记录
  20. Linux 关机、重启 命令

热门文章

  1. kwargs.pop是什么意思
  2. 关于DOM的一些基础问题
  3. poj 1920 Towers of Hanoi
  4. pytorch lstm crf 代码理解 重点
  5. 用diiv实现多个方块居中嵌套--margin
  6. P1073 奇数还是偶数
  7. H3C 常用的IPv6地址类型及格式
  8. P1046 阶乘
  9. ioctl 命令的实现
  10. 【t056】智力问答(multiset做法)