mybatis中对数据库连接信息进行加密

 时间:2024-10-31 10:22:29

1、第一步在自己的项目中写一个简单加密解密算法类,在这里我选择的使用DES加密,类的主要内容如下:/*** <描述>* @author <姓名>* @date 2014-10-8 下午3:24:14* DESUtils*/package com.cyl.util;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/*** DES加密算法工具类** @author RyanCai* @date 2014-10-8 下午3:24:14*/public class DESUtils { private static Key key; private static String KEY_STR = "myKeyRyanCai";// 密钥 private static String CHARSETNAME = "UTF-8";// 编码 private static String ALGORITHM = "DES";// 加密类型 static { try { KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM); generator.init(new SecureRandom(KEY_STR.getBytes())); key = generator.generateKey(); generator = null; } catch (Exception e) { throw new RuntimeException(e); } } /** * 对str进行DES加密 * * @param str * @return */ public static String getEncryptString(String str) { BASE64Encoder base64encoder = new BASE64Encoder(); try { byte[] bytes = str.getBytes(CHARSETNAME); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] doFinal = cipher.doFinal(bytes); return base64encoder.encode(doFinal); } catch (Exception e) { throw new RuntimeException(e); } } /** * 对str进行DES解密 * * @param str * @return */ public static String getDecryptString(String str) { BASE64Decoder base64decoder = new BASE64Decoder(); try { byte[] bytes = base64decoder.decodeBuffer(str); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); byte[] doFinal = cipher.doFinal(bytes); return new String(doFinal, CHARSETNAME); } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) { String cod = getEncryptString("123456"); System.out.println(cod); }}

2、再使用加密类将自己jdbc.properties中重要的文件进行加密,这样jdbc.properties文件中显示的就是加密信息,如下图

mybatis中对数据库连接信息进行加密
  • PLSQL编辑器怎么不允许将光标放到行末端的后面
  • 如何预防网络安全
  • 如何注销漫播APP账号?
  • 怎样炒“八月豆”好吃?
  • 家里装修之后怎么快速去除甲醛??
  • 热门搜索
    贵州旅游自由行攻略 西湖旅游攻略 宣城旅游 毕节旅游 南宁旅游攻略 四川省旅游学校 中华人民共和国文化和旅游部 毕业旅游 春季旅游 兰州旅游攻略