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

How to write the JavaAES256 encryption and decryption code?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to write the JavaAES256 encryption and decryption code. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Java supports many secure encryption algorithms, but some of them are weak and cannot be used in applications with high security requirements. For example, the data encryption Standard (DES) encryption algorithm is considered highly insecure. Today we will introduce AES 256 encryption and decryption.

What is AES 256?

Advanced encryption Standard (Advanced Encryption Standard, abbreviation: AES), also known as Rijndael encryption method in cryptography, is a block encryption standard adopted by the federal government of the United States. This standard, which is used to replace the original DES, has been analyzed by many parties and is widely used all over the world. AES is a symmetric encryption algorithm. It is designed to be easy to implement in hardware and software, as well as in restricted environments, and to provide good defense against a variety of attack technologies. AES is a block cipher that can handle 128-bit blocks with keys of 128,192 and 256 bits in size. Each password uses 128-bit, 192-bit and 256-bit encryption keys to encrypt and decrypt the data in the 128-bit block. It uses the same key for encryption and decryption, so both the sender and receiver must know and use the same secret key.

In the following encryption and decryption example, I used base64 encoding in the UTF-8 character set. Used to display the output of the program. You can also store and validate data in a byte array format.

AES 256 encryption

In a Java program, it is used to encrypt passwords (or any information) using AES 256 bits.

Private static String secretKey = "Boooooooooooooooooooooooooooooooooooooooooooomy"; private static String salt = "ssshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh SecretKey tmp = factory.generateSecret (spec); SecretKeySpec secretKey = new SecretKeySpec (tmp.getEncoded (), "AES"); Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS5Padding"); cipher.init (Cipher.ENCRYPT_MODE, secretKey, ivspec); return Base64.getEncoder (). EncodeToString (strToEncrypt.getBytes ("UTF-8"));} catch (Exception e) {System.out.println ("Error while encrypting:" + e.toString ());} return null;}

AES 256 decryption

Java program for decrypting passwords (or any information) using AES 256 bits.

Private static String secretKey = "Boooooooooooooooooooooooooooooooooooooooooooomy"; private static String salt = "ssshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh SecretKey tmp = factory.generateSecret (spec); SecretKeySpec secretKey = new SecretKeySpec (tmp.getEncoded (), "AES"); Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS5PADDING"); cipher.init (Cipher.DECRYPT_MODE, secretKey, ivspec); return new String (cipher.doFinal (Base64.getDecoder (). Decode (strToDecrypt));} catch (Exception e) {System.out.println ("Error while decrypting:" + e.toString ());} return null;}

Test AES256 encryption and decryption methods

Test our AES256 encryption and decryption method with a simple string

Public static void main (String [] args) {String originalString = "www.csdn.net"; String encryptedString = AES.encrypt (originalString, secretKey); String decryptedString = AES.decrypt (encryptedString, secretKey); System.out.println (originalString); System.out.println (encryptedString); System.out.println (decryptedString);}

Output result

Www.csdn.netbiXhp3Ha1fgxVEp48zHrvVoXMStmxPuAPHo3TVz5lHU=www.csdn.net

About JavaAES256 encryption and decryption code how to write it is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report