当前位置:网站首页>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"));
}
}
边栏推荐
- Process and concept of process
- Windows 安装 MySQL 5.7详细步骤
- Internet worm
- Application of explosion-proof inclination sensor in safe operation of LNG
- sql判断语句的编写
- [noi simulation] computational geometry (convex hull, violence, and search set)
- Dynamically load data
- 产品推广的渠道和策略,化妆品品牌推广方法及步骤
- [beauty of software engineering - column notes] 21 | architecture design: can ordinary programmers also implement complex systems?
- STM32 detection signal frequency
猜你喜欢

Cyberpunk special effect shader

A problem encountered in SQL interview
![[beauty of software engineering - column notes] 22 | how to do a good job in technology selection for the project?](/img/1a/72bfb3fef59c54188a823ead3a5390.png)
[beauty of software engineering - column notes] 22 | how to do a good job in technology selection for the project?

STM32 MDK (keil5) contents mismatch error summary
![[beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)](/img/c1/49b4610c483933442fb96b8e3f5315.png)
[beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)

Unity multiplayer online framework mirror learning record (I)

Implementation of support vector machine with ml11 sklearn

PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled

Simplefoc parameter adjustment 2- speed and position control
![[beauty of software engineering - column notes]](/img/b9/43db3fdfe1d9f08035668a66da37e2.png)
[beauty of software engineering - column notes] "one question and one answer" issue 3 | 18 common software development problem-solving strategies
随机推荐
[academic related] why can't many domestic scholars' AI papers be reproduced?
[robomaster] control RM motor from scratch (2) -can communication principle and electric regulation communication protocol
Eps32+platform+arduino running lantern
[beauty of software engineering - column notes] 23 | Architect: programmers who don't want to be architects are not good programmers
AES 双向加密解密工具
[beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?
Privacy is more secure in the era of digital RMB
Inclination sensor is used for long-term monitoring of communication tower and high-voltage tower
[beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
亚马逊测评自养号是什么,卖家应该怎么做?
Tb6600+stm32f407 test
Some tools, plug-ins and software links are shared with you~
commonjs导入导出与ES6 Modules导入导出简单介绍及使用
Noise monitoring and sensing system
Proteus simulation based on msp430f2491
What is Amazon self support number and what should sellers do?
node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
Time function in MySQL
Huawei wireless device configuration uses WDS technology to deploy WLAN services
UE4 highlight official reference value