Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation of AES encryption by java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException Public class AESUtil {private static final String IV_STRING = "sdf4ddfsFD86Vdf2"; private static final String encoding = "UTF-8"; public static String encryptAES (String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {byte [] byteContent = content.getBytes (encoding) / / Note: in order to be unified with iOS, KeyGenerator, SecureRandom and SecretKey cannot be used to generate byte [] enCodeFormat = key.getBytes (encoding); SecretKeySpec secretKeySpec = new SecretKeySpec (enCodeFormat, "AES"); byte [] initParam = IV_STRING.getBytes (encoding); IvParameterSpec ivParameterSpec = new IvParameterSpec (initParam) / / specify the encryption algorithm, working mode and filling mode Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS5Padding"); cipher.init (Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte [] encryptedBytes = cipher.doFinal (byteContent); / / also base64 encode encrypted data String base64 = new Base64 () .encodeToString (encryptedBytes) / perform url encoding and remove =? & return URLEncoder.encode (base64,encoding);} public static String decryptAES (String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {/ / URL decode content = URLDecoder.decode (content,encoding); / / base64 decode byte [] encryptedBytes = Base64.decodeBase64 (content) Byte [] enCodeFormat = key.getBytes (encoding); SecretKeySpec secretKey = new SecretKeySpec (enCodeFormat, "AES"); byte [] initParam = IV_STRING.getBytes (encoding); IvParameterSpec ivParameterSpec = new IvParameterSpec (initParam); Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS5Padding"); cipher.init (Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); byte [] result = cipher.doFinal (encryptedBytes) Return new String (result, encoding);} public static void main (String [] args) throws Exception {JSONObject json = new JSONObject (); json.put ("custNum", "111111"); / * json.put ("name", "Zhang San"); json.put ("cityCode", "100001"); json.put ("cityName", "Beijing"); json.put ("mobileNo", "15651876590") * / String content = json.toJSONString (); System.out.println ("before encryption: + content); String key =" djadiKJdj49dFJLd "; System.out.println (" encryption key and decryption key: "+ key); String encrypt = encryptAES (content, key); System.out.println (" after encryption: "+ encrypt); String decrypt = decryptAES (encrypt, key) System.out.println ("decrypted:" + decrypt);}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report