vue.jsjava前后台aes对称加密解密CBC模式

更新时间:2023-08-01 22:05:41 阅读: 评论:0

vue.jsjava前后台aes对称加密解密CBC模式JAVA端封装
package com.zdgk.application.pont;
import s.codec.DecoderException;
园林设计图片大全
幼儿园生活import s.codec.binary.Hex;
import s.util.Ba64;
import nv.Environment;
import org.springframework.util.Ba64Utils;
pto.*;
pto.spec.IvParameterSpec;
pto.spec.PBEKeySpec;
pto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.URLDecoder;
import java.curity.InvalidAlgorithmParameterException;
import java.curity.InvalidKeyException;
import java.curity.NoSuchAlgorithmException;
import java.curity.SecureRandom;
import java.curity.spec.InvalidKeySpecException;
import java.curity.spec.KeySpec;
/**
* @description AES加密解密类
* create by hss on 2017/10/16
*/
public class AesUtil {
private final int keySize =128;
private final int iterationCount =147;
private final Cipher cipher;
private static String status =null;
private static String passpha =null;
public static String salt =null;
public AesUtil(){
try{
cipher = Instance("AES/CBC/PKCS5Padding");
}catch(NoSuchAlgorithmException | NoSuchPaddingException e){
贯通是什么意思
throw fail(e);
}
}
public String encrypt(String salt, String iv, String passphra, String plaintext){
try{
SecretKey key =generateKey(salt, passphra);
byte[] encrypted =doFinal(Cipher.ENCRYPT_MODE, key, iv, Bytes("UTF-8"));
return ba64(encrypted);
}catch(UnsupportedEncodingException e){
throw fail(e);
}
}
public String decrypt(String salt, String iv, String passphra, String ciphertext){
try{
SecretKey key =generateKey(salt, passphra);
byte[] text=ba64(ciphertext);
byte[] decrypted =doFinal(Cipher.DECRYPT_MODE, key, iv, text);
return new String(decrypted,"UTF-8");
}catch(UnsupportedEncodingException e){
throw fail(e);
}
}
}
private byte[]doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes){
try{
cipher.init(encryptMode, key,new IvParameterSpec(hex(iv)));
return cipher.doFinal(bytes);
}catch(InvalidKeyException手机美女壁纸
| InvalidAlgorithmParameterException
| IllegalBlockSizeException
| BadPaddingException e){
throw fail(e);
}
}
private SecretKey generateKey(String salt, String passphra){
try{
SecretKeyFactory factory = Instance("PBKDF2WithHmacSHA1");
KeySpec spec =new CharArray(),hex(salt), iterationCount, keySize);            SecretKey key =new ateSecret(spec).getEncoded(),"AES");
return key;
}catch(NoSuchAlgorithmException | InvalidKeySpecException e){
throw fail(e);
}
}
public static String random(int length){
byte[] salt =new byte[length];
new SecureRandom().nextBytes(salt);
return hex(salt);
}
public static String ba64(byte[] bytes){
try{
deBa64String(bytes);
惹是生非
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static byte[]ba64(String str){
try{
String placeAll(" ","");
return Ba64.decodeBa64(str1);
}catch(Exception e){
e.printStackTrace();
}
return null;
辞去现任职务申请书}
public static String hex(byte[] bytes){
return new deHex(bytes));
}
public static byte[]hex(String str){
try{
return Hex.CharArray());
}catch(DecoderException e){
throw new IllegalStateException(e);
}
}
private IllegalStateException fail(Exception e){
return new IllegalStateException(e);
}
/**
* @description 进⾏加密的⽅法
十分近义词* @param originText
* @param iv
* @return String 加密后的结果
*/
public static String en(String originText,String iv){
if("0".equals(status)){
return originText;
}
try{
return new AesUtil().encrypt(salt,iv,passpha,originText);
}catch(Exception e){
return"";
}
}
/**
* @description 进⾏解密的⽅法
* @param aesText
* @param iv
* @return String 解密后的结果
*/
public static String de(String aesText,String iv){
if("0".equals(status)){
return aesText;
}
try{
return new AesUtil().decrypt(salt,iv,passpha,aesText);
}catch(Exception e){
e.printStackTrace();
return"";
}
}
public static void main(String[] args) throws Exception {
String IV="随机的IV";//tyg
String SALT="随机SALT";
String PLAIN_TEXT="你是谁?";
//String PASSPHRASE = "<script>var tyg='zytang';var ganggang = 1; var zfq = '1234567890'</script>";
String PASSPHRASE="铭⽂的KEY";
AesUtil util =new AesUtil();
String encrypt = pt(SALT,IV,PASSPHRASE,PLAIN_TEXT);
System.out.println(encrypt);
System.out.Bytes()));
String decrypt = util.decrypt(SALT,IV,PASSPHRASE, encrypt);
System.out.println(decrypt);
//        String data = pt(SALT,"40e6caa88cc44193b4be8030029f85bf",PASSPHRASE,PLAIN_TEXT);
//        System.out.println(data);
//        String str = URLDecoder.decode("O5zrbt4B2Y%2BN0LAfM%2F%2BpYlzFJqtALTavfkBTzZaygiSwl1tkCDUTNqylkS0986exmPl8e%2BJEEdUESiGtO4 ScYQ%3D%3D", Property("encoding", "UTF-8"));
//        System.out.println(str);
//        System.out.println(util.decrypt(SALT,"40e6caa88cc44193b4be8030029f85bf",PASSPHRASE,"O5zrbt4B2Y+N0LAfM/+pYlzFJqtALTavfkBTzZaygiSwl1 tkCDUTNqylkS0986exmPl8e+JEEdUESiGtO4ScYQ=="));
}
}
VUE
安装依赖crypto-js
npm install crypto-js --save
import CryptoJS from'crypto-js'
let keySizes,iterationCounts;
let AesUtil=function(keySize, iterationCount){
keySizes = keySize /32;
iterationCounts = iterationCount;
};
const that =new AesUtil(128,147);
ateKey=function(salt, passPhra){
let key = CryptoJS.PBKDF2(
passPhra,
{ keySize: keySizes, iterations: iterationCounts });
return key;
}
pt=function(salt, iv, passPhra, plainText){
let key = ateKey(salt, passPhra);
let encrypted = pt(
plainText,
key,
{ iv: Hex.par(iv)});
return Ba64);
}
AesUtil.prototype.decrypt=function(salt, iv, passPhra, cipherText){
let key = ateKey(salt, passPhra);
let cipherParams = CryptoJS.ate({
ciphertext: Ba64.par(cipherText)
});
let decrypted = CryptoJS.AES.decrypt(
cipherParams,
key,
{ iv: Hex.par(iv)});
Utf8);
}
//let aes = new AesUtil(128,147);
// let decrypted = aes.decrypt("这⾥是SALT","这⾥是IV","这⾥是明⽂的固定KEY","要解密字符串"); // console.log(decrypted);
// let encrypted = pt("这⾥是SALT","这⾥是IV","这⾥是明⽂的固定KEY","要加密字符串"); // console.log(encrypted);
export default{
pt,
decrypt:that.decrypt,
孩子长高的食物}
导出来的对象⾥⾯encrypt和decrypt就是加密和解密函数
参考资料:

本文发布于:2023-08-01 22:05:41,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1126134.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:加密   模式   辞去   图片
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图