一个Java配置文件加密解密工具类分享

所属分类: 软件编程 / java 阅读数: 58
收藏 0 赞 0 分享

常见的如: 数据库用户密码,短信平台用户密码,系统间校验的固定密码等。
本工具类参考了 《Spring.3.x企业应用开发实战》一书 5.3节的实现。
完整代码与注释信息如下:

复制代码 代码如下:

package com.cncounter.util.comm;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESUtils {

 // 密钥
 private static Key key;
 // KEY种子
 private static String KEY_STR = "encrypt@cncounter.com";
 // 常量
 public static final String UTF_8 = "UTF-8";
 public static final String DES = "DES";

 // 静态初始化
 static{
  try {
   // KEY 生成器
   KeyGenerator generator = KeyGenerator.getInstance(DES);
   // 初始化,安全随机算子
   generator.init(new SecureRandom( KEY_STR.getBytes(UTF_8) ));
   // 生成密钥
   key = generator.generateKey();
   generator = null;
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }

 /**
  * 对源字符串加密,返回 BASE64编码后的加密字符串
  * @param source 源字符串,明文
  * @return 密文字符串
  */
 public static String encode(String source){
  try {
   // 根据编码格式获取字节数组
   byte[] sourceBytes = source.getBytes(UTF_8);
   // DES 加密模式
   Cipher cipher = Cipher.getInstance(DES);
   cipher.init(Cipher.ENCRYPT_MODE, key);
   // 加密后的字节数组
   byte[] encryptSourceBytes = cipher.doFinal(sourceBytes);
   // Base64编码器
   BASE64Encoder base64Encoder = new BASE64Encoder();
   return base64Encoder.encode(encryptSourceBytes);
  } catch (Exception e) {
   // throw 也算是一种 return 路径
   throw new RuntimeException(e);
  }
 }

 /**
  * 对本工具类 encode() 方法加密后的字符串进行解码/解密
  * @param encrypted 被加密过的字符串,即密文
  * @return 明文字符串
  */
 public static String decode(String encrypted){
  // Base64解码器
  BASE64Decoder base64Decoder = new BASE64Decoder();
  try {
   // 先进行base64解码
   byte[] cryptedBytes = base64Decoder.decodeBuffer(encrypted);
   // DES 解密模式
   Cipher cipher = Cipher.getInstance(DES);
   cipher.init(Cipher.DECRYPT_MODE, key);
   // 解码后的字节数组
   byte[] decryptStrBytes = cipher.doFinal(cryptedBytes);
   // 采用给定编码格式将字节数组变成字符串
   return new String(decryptStrBytes, UTF_8);
  } catch (Exception e) {
   // 这种形式确实适合处理工具类
   throw new RuntimeException(e);
  }
 }
 // 单元测试
 public static void main(String[] args) {
  // 需要加密的字符串
  String email = "renfufei@qq.com";
  // 加密
  String encrypted = DESUtils.encode(email);
  // 解密
  String decrypted = DESUtils.decode(encrypted);
  // 输出结果;
  System.out.println("email: " + email);
  System.out.println("encrypted: " + encrypted);
  System.out.println("decrypted: " + decrypted);
  System.out.println("email.equals(decrypted): " + email.equals(decrypted));
 }
}

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多