In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.