AESUtils.java
package com.vcredit.framework.utils;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AESUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(AESUtils.class);
// 加密
public static String Encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
LOGGER.error("AES加密失败!--密钥为null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
LOGGER.error("AES加密失败!--密钥只能为16位字符串");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new Base64().encodeToString(encrypted);// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
// 解密
public static String Decrypt(String sSrc, String sKey) throws Exception {
try {
// 判断Key是否正确
if (sKey == null) {
LOGGER.error("AES加密失败!--密钥为null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
LOGGER.error("AES加密失败!--密钥只能为16位字符串");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = new Base64().decode(sSrc);// 先用base64解密
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
LOGGER.error("AES加密失败!", ex);
return null;
}
}
public static void main(String[] args) throws Exception {
/*
* 此处使用AES-128-ECB加密模式,key需要为16位。
*/
// String cKey = "1234567890123456";
String cKey = "activityzhounian";
// 需要加密的字串
String cSrc = "12313123";
System.out.println(cSrc);
// 加密
String enString = AESUtils.Encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = AESUtils.Decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
}
}
AESUtils.java的更多相关文章
- AES加密跨平台出现的问题
javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.S ...
- AES加密【转】
. 此时就一定要使用如下代码步骤 : 1.SecureRandom的key定下来. SecureRandom 实现完全隨操作系统本身的內部狀態,除非調用方在調用 getInstance 方法之後又 ...
- 利用maven进行项目管理
下面为maven项目管理的一个结构 首先pom是路径文件,我们在编译或是运行程序时调用到jdk或一些自己写的jar包时会需要指明物理路径,这里的pom是一样的道理,同时在maven的管理下多出来了一些 ...
- AES采用CBC模式128bit加密工具类
写在前面 安全测试ECB模式过于简单需要改为CBC模式加密以下为工具类及测试 AESUtils.java package com.sgcc.mobile.utils; import sun.misc. ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java中的AES加解密工具类:AESUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...
- java AES 加密与解密
package com.ss.util.secret; import java.io.UnsupportedEncodingException; import java.security.Inva ...
- 加密:HashUtils,RSAUtil,AESUtils
import java.security.MessageDigest; public class HashUtils { public static String getMD5(String sour ...
- java的AES对称加密和解密,有偏移量
import java.math.BigDecimal; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; i ...
随机推荐
- Linux系统性能10条命令监控
Linux系统性能10条命令监控 概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 p ...
- JVM参数调优
JVM参数调优 JVM参数调优是一个很头痛的问题,可能和应用有关系,下面是本人一些调优的实践经验,希望对读者能有帮助,环境LinuxAS4,resin2.1.17,JDK6.0,2CPU,4G内存,d ...
- LoadRunner脚本
Action() { char *str1=(char *)malloc(1024*sizeof(char)); char *str2="0"; char *str3=(char ...
- 开发Portlet第二步:如何将Crystal静态Portlet转变成基于测试数据的动态Portlet?
当基于Crystal的静态Portlet开发完成后,在与后台服务联调前,还需要将Portlet转换成基于测试数据的动态Portlet.具体步骤如下: 分步指南 复制Portlet项目,并修改相关的po ...
- C# 静态方法和非静态方法
转载:http://www.cnblogs.com/mikelij/archive/2010/08/13/1798578.html 本文将围绕c#静态方法和实例方法讨论一下.针对一些观点,如:&quo ...
- AngularJS学习之全局API(应用程序编程接口)
1.AngularJS全局API用于执行常见任务的Javascript函数集合: **比较对象 **迭代对象 **转换对象 2.全局API函数使用angularJS对象进行访问:以下是通用API函数: ...
- 关于__IPHONE_OS_VERSION_MAX_ALLOWED和__IPHONE_OS_VERSION_MIN_ALLOWED的用法
简单来说, 这些编译期常量, 对运行时的环境判断完全无效, 它告诉编译器用哪一段代码来进行编译, 那那段代码里, 你仍然可以写运行到目标机器里会崩溃的代码, 从stackoverflow里拷两段示例代 ...
- Android自动化测试 - MonkeyRunner(三) 随手练习测试脚本
#coding=utf-8 import os import time #import MonkeyRunner three module from com.android.monkeyrunner ...
- 拓展企业VR培训业务,这家VR训练公司StriVR完成500万美元融资!
虚拟现实初创公司StriVR最近发布了新的企业VR训练产品项目,并宣布在刚刚结束的首轮融资中获得500万美元投资.由Signia Venture Partners领投,宝马i Venturesi.Ad ...
- HDU 2852 (树状数组+无序第K小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...