当前位置:网站首页>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"));
}
}
边栏推荐
- Exercise: store department information
- DAC0832 waveform generator based on 51 single chip microcomputer
- Application of explosion-proof inclination sensor in safe operation of LNG
- Alibaba political commissar system - Chapter 4: political commissars are built on companies
- Alibaba political commissar system - Chapter 1: political commissars are built on companies
- Tle5012b+stm32f103c8t6 (bluepill) reading angle data
- [noi simulation] computational geometry (convex hull, violence, and search set)
- 亚马逊测评自养号是什么,卖家应该怎么做?
- Alibaba political commissar system - Chapter III, Alibaba political commissar and cultural docking
- Back up Google or other browser plug-ins
猜你喜欢
Unity多人联机框架Mirro学习记录(一)
[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?
110道 MySQL面试题及答案 (持续更新)
Official tutorial redshift 01 basic theoretical knowledge and basic characteristics learning
[academic related] why can't many domestic scholars' AI papers be reproduced?
Inclination monitoring solution of Internet of things
Unity multiplayer online framework mirror learning record (I)
Dp1332e multi protocol highly integrated contactless read-write chip
ROS tutorial (Xavier)
Background management system platform of new energy charging pile
随机推荐
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
Preparation of SQL judgment statement
Dynamically load data
Unity beginner 4 - frame animation and protagonist attack (2D)
[beauty of software engineering - column notes] 21 | architecture design: can ordinary programmers also implement complex systems?
BiSeNet v2
Data warehouse layered design and data synchronization,, 220728,,,,
ML.NET相关资源整理
Some tools, plug-ins and software links are shared with you~
Unicode私人使用区域(Private Use Areas)
AES 双向加密解密工具
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification
Unity beginner 2 - tile making and world interaction (2D)
亚马逊测评自养号是什么,卖家应该怎么做?
Reading papers on false news detection (4): a novel self-learning semi supervised deep learning network to detect fake news on
[beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?
为了速率创建线程池,启动核心线程
Cs4344 domestic substitute for dp4344 192K dual channel 24 bit DA converter
随机抽奖转盘微信小程序项目源码
torch.nn.functional.one_ hot()