package com.szmsd.common.security; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /** * @FileName AESHelper.java * @Description: AES加密器 * * */ public class AESHelper { /** * AES加密 */ public static String encrypt(String seed, String cleartext) throws Exception { byte[] rawKey = getRawKey(seed.getBytes()); byte[] result = encrypt(rawKey, cleartext.getBytes()); return toHex(result); } /** * AES解密 */ public static String decrypt(String seed, String encrypted) throws Exception { if (seed == null || "".equals(seed) || encrypted == null || "".equals(encrypted)){ return null; } byte[] rawKey = getRawKey(seed.getBytes()); byte[] enc = toByte(encrypted); byte[] result = decrypt(rawKey, enc); return new String(result); } private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed); kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; } private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { if (raw == null || clear == null){ return null; } SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(clear); return encrypted; } private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } public static String toHex(String txt) { return toHex(txt.getBytes()); } public static String fromHex(String hex) { return new String(toByte(hex)); } public static byte[] toByte(String hexString) { if (hexString == null || "".equals(hexString)){ return null; } int len = hexString.length() / 2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) { result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue(); } return result; } public static String toHex(byte[] buf) { if (buf == null) return ""; StringBuffer result = new StringBuffer(2 * buf.length); for (int i = 0; i < buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } private final static String HEX = "0123456789ABCDEFGHIJKLNMOPQRSTUVWSYZ"; private static void appendHex(StringBuffer sb, byte b) { sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f)); } public static void main(String[] args) throws Exception { String key = "abc123def456ghi789dsfsfaf24142342414"; String dbUrl="jdbc:oracle:thin:@192.168.100.211:1521:orcl"; String dbUser="javatest"; String dbPassword="oracle"; String encryptDbUrl = encrypt(key, dbUrl); String encryptDbUser = encrypt(key, dbUser); String encryptDbPassword = encrypt(key, dbPassword); System.out.println("--------加密后的字符串:"); System.out.println(encryptDbUrl); System.out.println(encryptDbUser); System.out.println(encryptDbPassword); String decryptDbUrl = decrypt(key, encryptDbUrl); String decryptDbUser = decrypt(key, encryptDbUser); String decryptDbPassword = decrypt(key, encryptDbPassword); System.out.println("--------解密后的字符串:"); System.out.println(decryptDbUrl); System.out.println(decryptDbUser); System.out.println(decryptDbPassword); // String mypassword="jdbc:oracle:thin:@192.168.100.211:1521:orcl"; // String encryptPassword = encrypt(key, mypassword); // System.out.println("--------加密后的字符串:"); // System.out.println(encryptPassword); // String decryptPassword=decrypt(key, encryptPassword); // System.out.println("--------解密后的字符串:"); // System.out.println(decryptPassword); } }2. 使用附件中的工具方便使用与检测
首先下载AesTools.jar文件,然后在同一目录新建一个AesRun.bat文件,内容如下:
相关推荐
使用java工具类可有效的提高开发效率! 没有CSDN积分的朋友到这里源头下载:http://www.javacs.cn/bbs/thread-382-1-1.html 感谢支持 [工具类] CookieCounter .java.txt [工具类] 验证码img .jsp.txt [工具类] Java中...
[工具类] CookieCounter .java.txt [工具类] 验证码img .jsp.txt [工具类] Java中计算任意两个日期之间的工作天数 .java.txt [工具类] java抓取网页 .java.txt [工具类] MD5 .java.txt [工具类] MD5强化版 .java.txt...
1、java工具类RabbitmqUtil是本人封装好的直接使用 2、常用的方法都在里面 3、改一下初始化配置参数就行
jedis的工具类,java代码写的,非常全面,jedis的工具类,java代码写的,非常全面jedis的工具类,java代码写的,非常全面jedis的工具类,java代码写的,非常全面jedis的工具类,java代码写的,非常全面jedis的工具类...
Java实现的Https请求工具类。 https的java工具类 Https
java常用的工具类整理28个java常用的工具类整理28个java常用的工具类整理28个
该工具类下载后拷贝到你要用的包,只需该包名即可
本源码包含公众号菜单初始化开发,菜单事件开发,普通token和网页授权token开发工具类,js_ticket获取和缓存,获取openid开发,公众号网络请求封装,xml格式转换工具类,文件上传工具类,关注事件/取消关注事件开发...
java各种工具类:包括日期工具类、字符串工具类、文件工具类、xml读写工具类、json工具类、http访问工具类、压缩包工具类等等。非常齐全
包含了IBM的MQ初始化,发送,接收的工具类,方便极了,可直接放入到项目中。
C#常用工具类代码集合Util第二版本(自己工作总结),包括常用工具类,扩展方法工具类,百度地图C#工具类,Echart工具类,Office工具类,Autofac工具类,Web开发常用工具类,Winform开发常用工具类,是自己工作十年...
该类是jedis操作redis的工具类,使用该工具类之后,无需配置spring,只需要显示调用工具类中的方法就好了。此类为工作中在用,所以确定可用。 共有两个类,一个工具类和一个测试调用demo
J2EE常用工具类汇总 J2EE常用工具类—Value Object工具类 J2EE常用工具类—Json工具 J2EE常用工具类—Jdbc操作 J2EE常用工具类—Hibernate操作 J2EE常用工具类—Cookie操作 J2EE常用工具类—数组操作 J2EE...
HTTPClient工具类,完整,HTTPClient工具类,完整,HTTPClient工具类,完整HTTPClient工具类,完整,HTTPClient工具类,完整
c++常用的工具类源码 包括:文件处理,编码处理,字符串处理,网络爬虫,网页数据的抓取 ,可根据数组规则抓取指定网页的内容(有一个例子)等等
该工具类是Java编写下载即可使用,该工具类中有生成各种长度的订单号,有英文数字混合的,有数字的
RabbitMQClientUtil是MQ的测试工具类,他封装了fanout、direct、topic三种exchange模式,并包括发送数据和接收数据。 Test1、Test2是测试类 使用maven管理,在pom.xml文件中引入如下代码: <!-- Rabbitmq工具包...
C# HttpClientHelper(HttpClient工具类) 包含 同步/异步请求 返回 string/泛型类型/Xml 及一个单例模式 的 SingleHelper 适合做爬虫
概述:Java poi导入导出EXCEL工具类(兼容各版本) 一、功能说明 允许同时导入或导出多个sheet,同一sheet可同时存在多个数据块,按数据块划分处理数据。 二、配置文件示例及详细说明 1、导入xml配置示例如下(见...