DES加解密工具类
来源:互联网 发布:淘宝网男凉鞋 编辑:程序博客网 时间:2024/06/06 02:12
/**
* DES加解密demo
*/
public class DESUtils {
private static Key key;
private static String KEY_STR="rchm";
static{
try{
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(KEY_STR.getBytes()));
key = generator.generateKey();
generator = null;
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* 获得des加密后的字符串
* @param str
* @return String
*/
public static String encrypt(String str){
try{
BASE64Encoder encode = new BASE64Encoder();
byte[] strBytes = str.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return encode.encode(encryptStrBytes);
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* 获得解密后的字符串
* @param str
* @return String
*/
public static String decrypt(String str){
BASE64Decoder decode = new BASE64Decoder();
try{
byte[] strBytes = decode.decodeBuffer(str);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptStrBytes = cipher.doFinal(strBytes);
return new String(decryptStrBytes,"UTF8");
}catch(Exception e){
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String srcStr = "my+N-a;me";
String encryptStr = encrypt(srcStr);
String desStr = decrypt(encryptStr);
System.out.println("strSrc:"+srcStr);
System.out.println("encryptStr:"+encryptStr);
System.out.println("desStr:"+desStr);
System.out.println(srcStr==desStr);
System.out.println(srcStr.equals(desStr));
}
}
* DES加解密demo
*/
public class DESUtils {
private static Key key;
private static String KEY_STR="rchm";
static{
try{
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(KEY_STR.getBytes()));
key = generator.generateKey();
generator = null;
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* 获得des加密后的字符串
* @param str
* @return String
*/
public static String encrypt(String str){
try{
BASE64Encoder encode = new BASE64Encoder();
byte[] strBytes = str.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return encode.encode(encryptStrBytes);
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* 获得解密后的字符串
* @param str
* @return String
*/
public static String decrypt(String str){
BASE64Decoder decode = new BASE64Decoder();
try{
byte[] strBytes = decode.decodeBuffer(str);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptStrBytes = cipher.doFinal(strBytes);
return new String(decryptStrBytes,"UTF8");
}catch(Exception e){
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String srcStr = "my+N-a;me";
String encryptStr = encrypt(srcStr);
String desStr = decrypt(encryptStr);
System.out.println("strSrc:"+srcStr);
System.out.println("encryptStr:"+encryptStr);
System.out.println("desStr:"+desStr);
System.out.println(srcStr==desStr);
System.out.println(srcStr.equals(desStr));
}
}
0 0
- DES加解密工具类
- DES加解密工具类
- DES加解密工具类
- DES加解密工具类
- 3DES加解密工具类
- C#DES文件加解密工具类
- JAVA工具类(16)---3des加解密工具类
- Java DES 加解密字符串、文件 工具类通用
- DES加解密及生成token工具类
- DES加密解密工具类
- DES 加密 解密 工具类
- des加密解密工具类
- 加解密工具类
- C# DES加密码解密类
- DES加解密算法实现类
- DES加解密
- Des加解密源代码
- DES加解密
- Linux IPC简单总结
- Cache地址与主存地址对应关系
- yum安装java8
- OC总结
- javascript继承与原型链
- DES加解密工具类
- [算法]常见算法题
- needImpl
- linux获取系统启动时间
- TOMCAT 6 中 警告“Parameters:Invalid chunk ignored ” 的原因和解决方法
- Spring MVC 解读——@Autowired
- 组合数学---母函数(1)
- ios面试题
- C++中的纯虚函数与抽象类