package com.common;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/*字符串 DESede(3DES) 加密*/
public class DES {
//private byte[] desKey; 8位
//解密数据
public static String decrypt(String message,String key) throws Exception {
byte[] bytesrc =convertHexString(message);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
String s = java.net.URLDecoder.decode(new String(retByte), "utf-8") ;
return s;
}
//加密数据
public static String encrypt(String message, String key)
throws Exception {
String s=java.net.URLEncoder.encode(message, "utf-8").toLowerCase();
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return toHexString(cipher.doFinal(s.getBytes("UTF-8"))).toUpperCase();
}
public static byte[] convertHexString(String ss)
{
byte digest[] = new byte[ss.length() / 2];
for(int i = 0; i < digest.length; i++)
{
String byteString = ss.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte)byteValue;
}
return digest;
}
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
return hexString.toString();
}
public static void main(String[] args) throws Exception {
String key = "11111111";
String value="tesdt";
System.out.println("加密数据为:"+value);
String a=encrypt(value, key);
System.out.println("加密后的数据为:"+a);
String b=decrypt(a,key);
System.out.println("解密后的数据:"+b);
}
}
des加密解密及md5加密
需积分: 9 80 浏览量
2009-01-20
15:54:47
上传
评论
收藏 2KB ZIP 举报
lsc23
- 粉丝: 0
- 资源: 9
最新资源
- VoNR应答掉话率分析处理案例.docx
- 基于matlab实现数字图像相关,该程序试图通过比较变形前和变形后的两张图像来计算应力和应变GUI.rar
- 基于matlab实现数字基带传输系统的matlab仿真源码及附属文件.rar
- 基于matlab实现生成高斯散斑图.rar
- ZBlog 宁静致远主题压缩包
- 基于matlab实现水声通信仿真代码,包括时间反转镜均衡,各种调制解调,信道仿真扥等.rar
- 基于matlab实现水声通信中的psk方法,信道为多径信道,接收段进行均衡处理,是较为完整的psk水声通信系统仿真.rar
- 基于matlab实现水声信道传输特性进行仿真的程序,包括水声信道特性对时反镜稳健性能影响等仿真程序 .rar
- 基于matlab实现水声信道仿真程序 用于水下声信道的仿真 .rar
- GEE训练-如何确定影像数据集多个指定时间范围的影像数量(sentinel-1数据为例)含全部代码.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈