Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to implement AES encryption and decryption algorithm

Shulou Source: shulou.com Published: 2022-05-31 15:45:20 10月03日 Update

This article mainly introduces "how to realize the AES encryption and decryption algorithm". In the daily operation, I believe that many people have doubts about how to realize the AES encryption and decryption algorithm. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the AES encryption and decryption algorithm". Next, please follow the editor to study!

/ * base64 encode * @ param bytes byte [] * @ return encoded base64 code * / public static String base64Encode (byte [] bytes) {return Base64.encodeBase64String (bytes) } / * * base64 decode * @ param base64Code base64 code * @ return decoded byte [] * @ throws Exception * / public static byte [] base64Decode (String base64Code) throws Exception {return StringUtils.isEmpty (base64Code)? Null: Base64.decodeBase64 (base64Code);} public static byte [] aesEncryptToBytes (String content, byte [] encryptKey) throws Exception {KeyGenerator kgen = KeyGenerator.getInstance ("AES"); kgenInit (kgen, encryptKey); Cipher cipher = Cipher.getInstance ("AES"); cipher.init (Cipher.ENCRYPT_MODE, new SecretKeySpec (kgen.generateKey (). GetEncoded (), "AES")); return cipher.doFinal (content.getBytes ("utf-8")) } / * AES encryption for base64 code * @ param content content to be encrypted * @ param encryptKey encryption key * @ return encrypted base64 code * @ throws Exception * / public static String aesEncrypt (String content, String encryptKey) throws Exception {return base64Encode (aesEncryptToBytes (content, getAESKey (encryptKey) } public static String aesDecryptByBytes (byte [] encryptBytes, byte [] decryptKey) throws Exception {KeyGenerator kgen = KeyGenerator.getInstance ("AES"); kgenInit (kgen, decryptKey); Cipher cipher = Cipher.getInstance ("AES"); cipher.init (Cipher.DECRYPT_MODE, new SecretKeySpec (kgen.generateKey (). GetEncoded (), "AES"); byte [] decryptBytes = cipher.doFinal (encryptBytes) Return new String (decryptBytes, "utf-8");} / * * decrypt base64 code AES * @ param encryptStr base64 code * @ param decryptKey decryption key * @ return decrypted string * @ throws Exception * / public static String aesDecrypt (String encryptStr, String decryptKey) throws Exception {return StringUtils.isEmpty (encryptStr)? Null: aesDecryptByBytes (base64Decode (encryptStr), getAESKey (decryptKey));} public static byte [] getAESKey (String encodingAESKey) {byte [] array = Base64.decodeBase64 (encodingAESKey+ "="); return array } / * prevent randomly generating key * @ param kgen * @ param bytes * @ throws NoSuchAlgorithmException * / public static void kgenInit (KeyGenerator kgen, byte [] bytes) throws NoSuchAlgorithmException {/ / 1 under linux. Prevent random generation of key SecureRandom secureRandom = SecureRandom.getInstance ("SHA1PRNG"); secureRandom.setSeed (bytes); / / 2 under linux. Initialize the key generator kgen.init (128, secureRandom) according to the key;} public static void main (String [] args) throws Exception {/ * * Test Line secret_key * / String encoding_aes_key= "88assadsfsdfsffsf6dsfsdfd"; / * * Parameter encryption * / String encrypt = aesEncrypt (content, encoding_aes_key); System.out.println ("after encryption:" + encrypt) / * * Parameter decryption * / String decrypt = aesDecrypt (encrypt, encoding_aes_key); System.out.println ("after decryption:" + decrypt);} at this point, the study of "how to implement the AES encryption and decryption algorithm" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Encryption algorithms keys learning generation parameters more coding utf-8 help utility next content articles methods theory generators knowledge articles websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Shulou Tech Info Shulou Technology Huawei