package com.luo.util;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class DecriptUtil {
/**
* @author:罗国辉
* @date: 2015年12月17日 上午9:16:22
* @description: AES加密
* @parameter: str:待加密字符串,secretKeyBase:用于生成密钥的基础字符串
* @return: 加密字节数组
**/
public static byte[] encryptAES(String str, String secretKeyBase) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(secretKeyBase.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = str.getBytes("UTF-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* @author:罗国辉
* @date: 2015年12月17日 上午9:16:22
* @description: AES解密
* @parameter: strByteArray:待解密字节数组,
* @parameter: secretKeyBase:用于生成密钥的基础字符串, 需要注意的是EAS是对称加密,所以secretKeyBase在加密解密时要一样的
* @return: 解密后字符串
**/
public static String decryptAES(byte[] strByteArray, String secretKeyBase) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(secretKeyBase.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
String result = new String(cipher.doFinal(strByteArray),"UTF-8");
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* @author:罗国辉
* @date: 2015年12月17日 上午9:24:43
* @description: SHA、SHA1加密
* @parameter: str:待加密字符串
* @return: 加密串
**/
public static String SHA1(String str) {
try {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可
digest.update(str.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexStr = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexStr.append(0);
}
hexStr.append(shaHex);
}
return hexStr.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
/**
* @author:罗国辉
* @date: 2015年12月17日 上午9:24:43
* @description: MD5加密
* @parameter: str:待加密字符串
* @return: 加密串
**/
public static String MD5(String str) {
try {
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(str.getBytes());
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < md.length; i++) {
String shaHex = Integer.toHexString(md[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
/**
* @author:罗国辉
* @date: 2015年12月17日 上午9:24:43
* @description: MD5加密 返回二进制
* @parameter: str:待加密字符串
* @return: 加密二进制
**/
public static byte[] MD5ToByteArray(String str) {
try {
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(str.getBytes());
// 获得密文
byte[] md = mdInst.digest();
return md;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
shiro登录验证实例
共173个文件
js:78个
css:28个
xml:11个
5星 · 超过95%的资源 需积分: 49 2.5k 下载量 169 浏览量
2016-02-03
15:06:23
上传
评论 39
收藏 1.29MB ZIP 举报
温馨提示
shiro登录验证实例,下载包虽然是web_exception_project.zip,但是确实是shiro登录验证实例,请放心下载,另外,实例详情请访问博主博客:http://blog.csdn.net/u013142781
资源推荐
资源详情
资源评论
收起资源包目录
shiro登录验证实例 (173个子文件)
build.bat 37B
DecriptUtil.class 4KB
UserController.class 4KB
MySimpleMappingExceptionResolver.class 3KB
LuoErrorCode.class 2KB
MyShiroRealm.class 2KB
BusinessException.class 539B
.classpath 2KB
org.eclipse.wst.common.component 793B
org.eclipse.wst.jsdt.ui.superType.container 49B
dpl.css 90KB
dpl.css 84KB
dpl-min.css 78KB
dpl-min.css 72KB
bui.css 60KB
bui.css 56KB
bui-min.css 52KB
bui-min.css 49KB
grid.css 18KB
extend.css 17KB
overlay.css 16KB
grid-min.css 15KB
extend-min.css 15KB
calendar.css 14KB
overlay-min.css 14KB
calendar-min.css 12KB
select.css 7KB
tab.css 6KB
select-min.css 6KB
menu.css 5KB
tab-min.css 5KB
menu-min.css 4KB
imgview.css 3KB
imgview-min.css 3KB
layout.css 3KB
slider.css 2KB
layout-min.css 2KB
slider-min.css 2KB
iconfont.eot 31KB
tab-140-120.gif 6KB
table-191-450.gif 5KB
checkbox.gif 3KB
check_icon-100-100.gif 3KB
tree_icon-80-100.gif 3KB
tree.gif 3KB
radio_icon-64-40.gif 3KB
calendar-200-300.gif 3KB
separator-2-340.gif 1KB
load-16-16.gif 673B
buitest.html 5KB
DecriptUtil.java 6KB
UserController.java 4KB
MySimpleMappingExceptionResolver.java 2KB
MyShiroRealm.java 2KB
LuoErrorCode.java 843B
BusinessException.java 222B
ui-img-view-height.jpg 9KB
ui-img-view-width.jpg 9KB
bui.js 1.09MB
graphic.js 371KB
seed.js 327KB
common.js 303KB
bui-min.js 285KB
chart.js 226KB
grid.js 160KB
form.js 121KB
graphic-min.js 107KB
jquery-1.8.1.min.js 91KB
chart-min.js 83KB
seed-min.js 76KB
uploader.js 73KB
data.js 71KB
common-min.js 68KB
layout.js 61KB
grid-min.js 49KB
tree.js 48KB
list.js 47KB
calendar.js 41KB
form-min.js 41KB
tab.js 38KB
toolbar.js 32KB
imgview.js 30KB
select.js 29KB
uploader-min.js 23KB
overlay.js 22KB
loader.js 21KB
swf.js 20KB
layout-min.js 20KB
editor.js 19KB
menu.js 18KB
calendar-min.js 16KB
data-min.js 16KB
list-min.js 14KB
tree-min.js 14KB
tab-min.js 13KB
picker.js 12KB
imgview-min.js 11KB
multiselect.js 11KB
tooltip.js 11KB
adapter.js 10KB
共 173 条
- 1
- 2
小宝鸽
- 粉丝: 2873
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页