AES 加盐加密明文传输数据

可以直接使用的工具类
public class DataEncryptUtils {

/** 👇👇👇👇👇👇👇👇👇👇👇👇👇自定义加密👇👇👇👇👇👇👇👇👇👇👇👇👇 */


/**
* F
* 加盐字符
*/
public static final String ENCRYPT_PLAINTEXT = "osHwxCAndFxwSc";

/**
* 加密方法 加盐加随机数 返回加密字符串
**/
public static String encode(String code) {
int random = (int) ((Math.random() * 9 + 1) * 100);
code = random + code;

byte[] codeBytes = code.getBytes();
for (int i = 0; i < codeBytes.length; i++) {
codeBytes[i] = (byte) (codeBytes[i] ^ 'a');
}
String data = new String(codeBytes);
byte[] b = data.getBytes();
//遍历
for (int i = 0; i < b.length; i++) {
b[i] += 1;//在原有的基础上+1
}
//给密文加盐
return ENCRYPT_PLAINTEXT + new String(b);
}

/**
* 解密方法 去除加密字符串 返回解密字符
**/
public static String decode(String data) {
//截取到原有的密文
data = data.substring(ENCRYPT_PLAINTEXT.length(), data.length());
//把字符串转为字节数组
byte[] b = data.getBytes();
//遍历
for (int i = 0; i < b.length; i++) {
b[i] -= 1;//在原有的基础上-1
}
String code = new String(b);
byte[] a = code.getBytes();
for (int i = 0; i < a.length; i++) {
a[i] = (byte) (a[i] ^ 'a');
}
String decodeData = new String(a);
return decodeData.substring(3, decodeData.length());
}


/** 👇👇👇👇👇👇👇👇👇👇👇👇👇AES加密👇👇👇👇👇👇👇👇👇👇👇👇👇 */

/**
* AES加密 使用秘钥进行AES加密 privateKey必须为长度16位(否则会报错哦)
**/
public static String encryptAES(String data, String privateKey) {
try {
data = encodeAES(data);
byte[] bytes = data.getBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec("HWXCFX==20200907".getBytes());
SecretKeySpec secretKeySpec = new SecretKeySpec(privateKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
bytes = cipher.doFinal(bytes);
bytes = Base64.getEncoder().encode(bytes);
return new String(bytes);
} catch (Exception e) {
log.error("XXXX:data= {},key={}", data, privateKey);
log.error(e.getMessage(), e);
return null;
}
}

/**
* AES解密 使用秘钥进行AES解密 privateKey必须为长度16位(否则会报错哦)
**/
public static String decryptAES(String data, String privateKey) {
try {
byte[] bytes = Base64.getDecoder().decode(data);
IvParameterSpec ivParameterSpec = new IvParameterSpec("HWXCFX==20200907".getBytes());
SecretKeySpec secretKeySpec = new SecretKeySpec(privateKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
bytes = cipher.doFinal(bytes);
String result = new String(bytes);
return decodeAES(result);
} catch (Exception e) {
log.error("XXXXX:data= {},key={}", data, privateKey);
log.error(e.getMessage(), e);
return null;
}
}


public static String encodeAES(String code) {
byte[] codeBytes = code.getBytes();
for (int i = 0; i < codeBytes.length; i++) {
codeBytes[i] = (byte) (codeBytes[i] ^ 'a');
}
String data = new String(codeBytes);
byte[] b = data.getBytes();
//遍历
for (int i = 0; i < b.length; i++) {
b[i] += 1;//在原有的基础上+1
}
//给密文加盐
return ENCRYPT_PLAINTEXT + new String(b);
}

public static String decodeAES(String data) {
//截取到原有的密文
data = data.substring(ENCRYPT_PLAINTEXT.length(), data.length());
//把字符串转为字节数组
byte[] b = data.getBytes();
//遍历
for (int i = 0; i < b.length; i++) {
b[i] -= 1;//在原有的基础上-1
}
String code = new String(b);
byte[] a = code.getBytes();
for (int i = 0; i < a.length; i++) {
a[i] = (byte) (a[i] ^ 'a');
}
return new String(a);
}

//47oKPEAneQuZJpMD95ROK0NhMuq71AxjBofFKSbAejw= Ms5J66kPK/LgXoyHZJv5wBu2XfW6OJKc5uPXL/k0Wbo=

public static void main(String[] args) {
//数据加密开始
System.out.println("解密:" + decryptAES("47oKPEAneQuZJpMD95ROK0NhMuq71AxjBofFKSbAejw=", "vwceJf5MrhQsUfpB"));

System.out.println(encryptAES("456", "1234567891234567"));
}
}



package com.hwxc.utils;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

/**
* @author yhs
* @Description:
* @creatDate 2022/7/13 15:24
*/
public class ASEUtils {

public static String encrypt(String algorithm, String input, SecretKey key,
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {

Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] cipherText = cipher.doFinal(input.getBytes());
return Base64.getEncoder().encodeToString(cipherText);

}

public static String decrypt(String algorithm, String cipherText, SecretKey key,
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] plainText = cipher.doFinal(Base64.getDecoder()
.decode(cipherText));
return new String(plainText);
}

public static IvParameterSpec generateIv() {
byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
return new IvParameterSpec(iv);
}

public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(n);
SecretKey key = keyGenerator.generateKey();
String algorithm = key.getAlgorithm();
System.out.println(algorithm);
return key;
}



public static void main(String[] args) {
try {
String input = "baeldung";
SecretKey key = generateKey(128);
IvParameterSpec ivParameterSpec = generateIv();
// System.out.println(new String(ivParameterSpec.getIV()));
String algorithm = "AES/CBC/PKCS5Padding";
String cipherText = encrypt(algorithm, input, key, ivParameterSpec);
String plainText = decrypt(algorithm, cipherText, key, ivParameterSpec);
System.out.println(cipherText);
System.out.println(plainText);
} catch (Exception e) {
e.printStackTrace();
}
}

}
 
posted @   沐春风-燕南飞  阅读(1121)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示