JAVA_AesCBC例子
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; /**
* AES 是一种可逆加密算法,对用户的敏感信息加密处理
* 对原始数据进行AES加密后,在进行Base64编码转化;
* 正确
*/
public class AesCBC {
/*已确认
* 加密用的Key 可以用26个字母和数字组成
* 此处使用AES-128-CBC加密模式,key需要为16位。
*/
private static String sKey="1234567890123456";
private static String ivParameter="1234567890123456";
private static AesCBC instance=null;
//private static
private AesCBC(){ }
public static AesCBC getInstance(){
if (instance==null)
instance= new AesCBC();
return instance;
}
// 加密
public String encrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));
return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码。
} // 解密
public String decrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original,encodingFormat);
return originalString;
} catch (Exception ex) {
return null;
}
} public static void main(String[] args) throws Exception {
// 需要加密的字串
String cSrc = "123456";
System.out.println("加密前的字串是:"+cSrc);
// 加密
String enString = AesCBC.getInstance().encrypt(cSrc,"utf-8",sKey,ivParameter);
System.out.println("加密后的字串是:"+ enString); System.out.println("1jdzWuniG6UMtoa3T6uNLA==".equals(enString)); // 解密
String DeString = AesCBC.getInstance().decrypt(enString,"utf-8",sKey,ivParameter);
System.out.println("解密后的字串是:" + DeString);
}
}
JAVA_AesCBC例子的更多相关文章
- JAVA_AesCBC纯净例子
import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException ...
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)
第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...
- REGEX例子
作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...
- CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子
CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子 本文涉及的VolumeRendering相关的C#代码是从(https://github.com/toolchai ...
- 简单例子了解View的事件分发
什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...
- 简单的例子了解自定义ViewGroup(一)
在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...
- kqueue例子
网络服务器通常都使用epoll进行异步IO处理,而开发者通常使用mac,为了方便开发,我把自己的handy库移植到了mac平台上.移植过程中,网上居然没有搜到kqueue的使用例子,让我惊讶不已.为了 ...
- 今天有群友不是很清楚htm直接存数据库的危害,我简单举个例子
通过这个案例就知道为什么不要把原生的html放数据库了 常见的几种转码 常用的几种显示方法 只有原生html和最下面一种弹框了,变成了持久xss 如果是Ajax的方式,请用@Ajax.JavaS ...
随机推荐
- Linux环境 tp5.1 Could not open input file: think
服务器命令行执行:php /项目目录/think queue:listen 报如下错误 初步分析是 queue:listen 在代码中要重启一个work进程,用到了think ,导致找不到该文件的路 ...
- 项目Alpha冲刺(团队)-第二天冲刺
格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队)-代码规范.冲刺任务与计划 团队名称:为了交项目干杯 作业目标:描述第二天冲刺的项目进展.问题困难.心得体会 ...
- ELK全Dokcer 部署
环境准备: docker-complete 解压 su root 进入目录 rpm -Uvh *.rpm 安装docker systemctl start docker systemctl enabl ...
- 【easy】235. Lowest Common Ancestor of a Binary Search Tree
题意大概是,找两个节点的最低公共祖先 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod ...
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths
F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 ...
- C# 登陆验证码工具类VerifyCode
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...
- js性能的进阶
为了说明js性能方面的差异用一个简单的例子说明下, <style> #ul1{ padding: 5px; overflow: hidden; } #ul1 li{ list-style: ...
- vscode插件解析-BookMark
BookMark (书签):在编辑器中标记行并轻松跳转到它们. commands 书签:Toggle 标记/取消标记带书签的行 书签:Jump to Next 将光标向前移动到下面的书签 书签: ...
- Motivation
觉得一个需求不错,却没有意愿去做,唯一可能的意愿就是生活需要.可这并不能很好的带动起来什么,除了让自己觉得在逼自己. 后来在这个需求的基础上,延伸出新的需求,可能更适应生活.仍然没有意愿去动手,虽然生 ...
- 屏蔽eslint代码格式报错
1.在文件中找到node_modules 2.node_modules文件夹下的eslint-config-standard 3.打开eslint-config-standard文件夹下的eslint ...