当前位置:网站首页>AES bidirectional encryption and decryption tool
AES bidirectional encryption and decryption tool
2022-07-29 08:18:00 【Future Farmers】
package com.qhwjw.istc.phapi.util;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
/**
* AES Bidirectional encryption and decryption tool
*
* @author tomsun28
* @date 20:04 2018/2/11
*/
public class AesUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(AesUtil.class);
/**
* Default encryption key AES The encryption secret key is the Convention 16 position , Less than 16 Bit will report an error
*/
private static final String ENCODE_RULES = "tomSun28HaHaHaHa";
/**
* Default algorithm
*/
private static final String ALGORITHM_STR = "AES/CBC/PKCS5Padding";
public static String aesEncode(String content) {
return aesEncode(content, ENCODE_RULES);
}
public static String aesDecode(String content) {
return aesDecode(content, ENCODE_RULES);
}
private AesUtil() {
}
/**
* description encryption aes cbc Pattern
*
* @param content 1
* @param encryptKey 2
* @return java.lang.String
*/
public static String aesEncode(String content, String encryptKey) {
try {
SecretKeySpec keySpec = new SecretKeySpec(encryptKey.getBytes(StandardCharsets.UTF_8), "AES");
// According to the specified algorithm AES Self forming cipher
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
// Initialize cipher , The first parameter is encryption (Encrypt_mode) Or decrypt (Decrypt_mode) operation , The second parameter is used KEY
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(encryptKey.getBytes(StandardCharsets.UTF_8)));
// Get the byte array of encrypted content ( It's set to utf-8) Otherwise, if there is a mixture of Chinese and English in the content, Chinese will be decrypted as garbled code
byte[] byteEncode = content.getBytes(StandardCharsets.UTF_8);
// According to the initialization mode of the cipher -- encryption : Encrypt data
byte[] byteAES = cipher.doFinal(byteEncode);
// Will be encrypted after byte[] Data to Base64 character string
return new String(Base64.encodeBase64(byteAES), StandardCharsets.UTF_8);
// Returns the string to
} catch (Exception e) {
LOGGER.error(" Ciphertext encryption failed " + e.getMessage(), e);
throw new RuntimeException(" Ciphertext encryption failed ");
}
// If there is a mistake, add it back null
}
/**
* description Decrypt
*
* @param content 1
* @param decryptKey 2
* @return java.lang.String
*/
public static String aesDecode(String content, String decryptKey) {
try {
SecretKeySpec keySpec = new SecretKeySpec(decryptKey.getBytes(StandardCharsets.UTF_8), "AES");
// According to the specified algorithm AES Self forming cipher
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
// Initialize cipher , The first parameter is encryption (Encrypt_mode) Or decrypt (Decrypt_mode) operation , The second parameter is used KEY
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(decryptKey.getBytes(StandardCharsets.UTF_8)));
//8. Will be encrypted and encoded base64 String content after base64 Decode into byte array
byte[] bytesContent = Base64.decodeBase64(content);
/*
* Decrypt
*/
byte[] byteDecode = cipher.doFinal(bytesContent);
return new String(byteDecode, StandardCharsets.UTF_8);
} catch (NoSuchAlgorithmException e) {
LOGGER.error(" There is no specified encryption algorithm ::" + e.getMessage(), e);
} catch (IllegalBlockSizeException e) {
LOGGER.error(" Illegal block size " + "::" + e.getMessage(), e);
throw new RuntimeException(" Ciphertext decryption failed ");
} catch (NullPointerException e) {
LOGGER.error(" Secret key parsing null pointer exception " + "::" + e.getMessage(), e);
throw new RuntimeException(" Secret key parsing null pointer exception ");
} catch (Exception e) {
LOGGER.error(" Secret key AES Unknown error occurred in parsing " + "::" + e.getMessage(), e);
throw new RuntimeException(" Ciphertext decryption failed ");
}
// If there is a mistake, go back null
return null;
}
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(aesEncode("[email protected]","fx9iqqas8owe65wk"));
}
}
边栏推荐
- 网络安全之安全基线
- UE4 principle and difference between skylight and reflecting sphere
- 亚马逊测评自养号是什么,卖家应该怎么做?
- A problem encountered in SQL interview
- NFC two-way communication 13.56MHz contactless reader chip -- si512 replaces pn512
- Domestic application of ft232 replacing gp232rl usb-rs232 converter chip
- Alibaba political commissar system - Chapter 4: political commissars are built on companies
- Rotation in model space and rotation in world space
- (视频+图文)机器学习入门系列-第5章 机器学习实践
- 2.4G band wireless transceiver chip si24r1 summary answer
猜你喜欢

DC motor speed regulation system based on 51 single chip microcomputer (use of L298)

Simple calculator wechat applet project source code

集群使用规范
![[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?](/img/e1/8a61f85bf93801d842e78ab4f7edc7.png)
[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?

Cv520 domestic replacement of ci521 13.56MHz contactless reader chip

Simplefoc parameter adjustment 1-torque control

Privacy is more secure in the era of digital RMB
![[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification](/img/0e/e5be0fffb154d081c20b09832530d4.png)
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification

分段分页以及段页结合
![[beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?](/img/09/296185ae299bec898b8bbe9cfc993f.png)
[beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?
随机推荐
简易计算器微信小程序项目源码
Simplefoc parameter adjustment 2- speed and position control
Network Security Learning chapter
SQL 面试碰到的一个问题
Simplefoc+platformio stepping on the path of the pit
DC motor control system based on DAC0832
深度学习(1):银行客户流失预测
Beautiful girls
Cyberpunk special effect shader
Some simple uses of crawler requests Library
125kHz wake-up function 2.4GHz single transmitter chip-si24r2h
Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings
Tb6600+stm32f407 test
Noise monitoring and sensing system
sql判断语句的编写
Inclination monitoring solution of Internet of things
Cv520 domestic replacement of ci521 13.56MHz contactless reader chip
Simplefoc parameter adjustment 1-torque control
torch.Tensor和torch.tensor的区别
ML.NET相关资源整理