当前位置:网站首页>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"));
}
}
边栏推荐
- Security baseline of network security
- Segment paging and segment page combination
- 简易计算器微信小程序项目源码
- Cv520 domestic replacement of ci521 13.56MHz contactless reader chip
- [beauty of software engineering - column notes] 22 | how to do a good job in technology selection for the project?
- 阿里巴巴政委体系-第三章、阿里政委与文化对接
- UE4 highlight official reference value
- 数仓分层设计及数据同步问题,,220728,,,,
- Time function in MySQL
- Huawei wireless device configuration uses WDS technology to deploy WLAN services
猜你喜欢

集群使用规范
![[academic related] why can't many domestic scholars' AI papers be reproduced?](/img/1a/7b162741aa7ef09538355001bf45e7.png)
[academic related] why can't many domestic scholars' AI papers be reproduced?

Time function in MySQL

A problem encountered in SQL interview

BiSeNet v2

Security baseline of network security
![[beauty of software engineering - column notes] 30 | make good use of source code management tools to make your collaboration more efficient](/img/d1/5b980d8b9580b9808b2b3f51d5b9c6.png)
[beauty of software engineering - column notes] 30 | make good use of source code management tools to make your collaboration more efficient

Unity beginner 4 - frame animation and protagonist attack (2D)

深度学习(1):银行客户流失预测

Compatible with cc1101/cmt2300-dp4301 sub-1g wireless transceiver chip
随机推荐
PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled
[beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?
[beauty of software engineering - column notes] "one question and one answer" issue 3 | 18 common software development problem-solving strategies
简易计算器微信小程序项目源码
125kHz wake-up function 2.4GHz single transmitter chip-si24r2h
Unity multiplayer online framework mirror learning record (I)
[beauty of software engineering - column notes] 23 | Architect: programmers who don't want to be architects are not good programmers
torch.nn.functional.one_ hot()
STM32 MDK (keil5) contents mismatch error summary
[beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)
Data warehouse layered design and data synchronization,, 220728,,,,
Beautiful girls
Privacy is more secure in the era of digital RMB
Qt/pyqt window type and window flag
网络安全之安全基线
Arduinoide + stm32link burning debugging
[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?
AES 双向加密解密工具
Low power Bluetooth 5.0 chip nrf52832-qfaa
Nrf52832-qfaa Bluetooth wireless chip