java實(shí)現(xiàn)AES可逆加密算法
更新時間:2019年03月22日 09:21:05 作者:風(fēng)過留痕丶
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)AES可逆加密算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了java實(shí)現(xiàn)AES可逆加密算法的具體代碼,供大家參考,具體內(nèi)容如下
package com.hdu.encode;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES 是一種可逆加密算法,對用戶的敏感信息加密處理 對原始數(shù)據(jù)進(jìn)行AES加密后,在進(jìn)行Base64編碼轉(zhuǎn)化;
*/
public class AESOperator {
/*
* 加密用的Key 可以用26個字母和數(shù)字組成 此處使用AES-128-CBC加密模式,key需要為16位。
*/
// a0b891c2d563e4f7
private String sKey = "abcdef0123456789";
private String ivParameter = "0123456789abcdef";
private static AESOperator instance = null;
private AESOperator() {
}
public static AESOperator getInstance() {
if (instance == null)
instance = new AESOperator();
return instance;
}
// 加密
public String encrypt(String sSrc){
String result = "";
try {
Cipher 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,可增加加密算法的強(qiáng)度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
result = new BASE64Encoder().encode(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
// 此處使用BASE64做轉(zhuǎn)碼。
return result;
}
// 解密
public String decrypt(String sSrc){
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, "utf-8");
return originalString;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static void main(String[] args){
// 需要加密的字串
String cSrc = "測試";
System.out.println(cSrc + " 長度為" + cSrc.length());
// 加密
long lStart = System.currentTimeMillis();
String enString = AESOperator.getInstance().encrypt(cSrc);
System.out.println("加密后的字串是:" + enString + "長度為" + enString.length());
long lUseTime = System.currentTimeMillis() - lStart;
System.out.println("加密耗時:" + lUseTime + "毫秒");
// 解密
lStart = System.currentTimeMillis();
String DeString = AESOperator.getInstance().decrypt(enString);
System.out.println("解密后的字串是:" + DeString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println("解密耗時:" + lUseTime + "毫秒");
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA中springboot提示java:找不到符號符號:變量log問題
這篇文章主要介紹了IDEA中springboot提示java:找不到符號符號:變量log問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
IntelliJ?IDEA?2022.2最新版本激活教程(親測可用版)永久激活工具分享
Jetbrains官方發(fā)布了?IntelliJ?IDEA2022.2?正式版,每次大的版本更新,都會有較大的調(diào)整和優(yōu)化,除本次更新全面擁抱?Java?17?外,還有對IDE?UI界面,安全性,便捷性等都做了調(diào)整和優(yōu)化完善,用戶體驗(yàn)提升不少,相信后面會有不少小伙伴跟著更新2022-08-08
Springboot中Dependency not found解決方案
本文主要介紹了Springboot中Dependency not found解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
SpringMVC加載控制與Postmand的使用和Rest風(fēng)格的引入及RestFul開發(fā)全面詳解
SpringMVC是一種基于Java,實(shí)現(xiàn)了Web MVC設(shè)計模式,請求驅(qū)動類型的輕量級Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡埱篁?qū)動指的就是使用請求-響應(yīng)模型,框架的目的就是幫助我們簡化開發(fā),SpringMVC也是要簡化我們?nèi)粘eb開發(fā)2022-10-10
Java SpringMVC實(shí)現(xiàn)PC端網(wǎng)頁微信掃碼支付(完整版)
這篇文章主要介紹了Java SpringMVC實(shí)現(xiàn)PC端網(wǎng)頁微信掃碼支付(完整版)的相關(guān)資料,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下2016-11-11

