介绍
怎么在Java中利用AES256进行加密解密操作?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
<强>什么是,AES 256 ? 强>
- <李>
高级加密标准(英语:高级加密标准,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。
李> <李>AES是一种对称加密算法。它旨在易于在硬件和软件以及受限环境中实施,并提供针对各种攻击技术的良好防御。是能够使用大小为128192和256位的密钥处理128位块的块密码。每个密码分别使用128位,192位和256位的加密密钥对128位块中的数据进行加密和解密。它使用相同的密钥进行加密和解密,因此发送方和接收方都必须知道并使用相同的秘密密钥。
李>在下面的加密和解密示例中,我在utf - 8字符集中使用了base64编码。用于显示程序的输出。也可以以字节数组格式存储和验证数据。
<强> AES 256加密,
强>
Java程序中,用于使用AES 256位对密码(或任何信息)进行加密。
private static String secretKey =,“boooooooooom !“; private static  String salt =,“ssshhhhhhhhhhh !“; , public static  String 加密(String strToEncrypt, String 秘密) { ,试一试 {才能 ,,,byte [], iv =, {, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}; ,,,IvParameterSpec ivspec =, new IvParameterSpec (iv); ,,,, ,,,SecretKeyFactory factory =, SecretKeyFactory.getInstance (“PBKDF2WithHmacSHA256"); ,,,KeySpec spec =, new PBEKeySpec (secretKey.toCharArray (),, salt.getBytes (),, 65536,, 256); ,,,SecretKey tmp =, factory.generateSecret(规范); ,,,SecretKeySpec secretKey =, new SecretKeySpec (tmp.getEncoded (),“AES"); ,,,, ,,,Cipher Cipher =, . getinstance (“AES/CBC/PKCS5Padding"); ,,,cipher.init (Cipher.ENCRYPT_MODE, secretKey,, ivspec); ,,,return Base64.getEncoder () .encodeToString (cipher.doFinal (strToEncrypt.getBytes (“UTF-8"))); ,,} catch 才能;(Exception e) {才能 ,,,System.out.println (“Error  while 加密:,“,+,e.toString ()); ,,} return 才能;零; }
<>强AES 256解密强>
Java程序,用于使用AES 256位解密密码(或任何信息)只
private static String secretKey =,“boooooooooom !“; private static  String salt =,“ssshhhhhhhhhhh !“; , public static  String 解密(String strToDecrypt, String 秘密),{ ,试一试 {才能 ,,,byte [], iv =, {, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}; ,,,IvParameterSpec ivspec =, new IvParameterSpec (iv); ,,,, ,,,SecretKeyFactory factory =, SecretKeyFactory.getInstance (“PBKDF2WithHmacSHA256"); ,,,KeySpec spec =, new PBEKeySpec (secretKey.toCharArray (),, salt.getBytes (),, 65536,, 256); ,,,SecretKey tmp =, factory.generateSecret(规范); ,,,SecretKeySpec secretKey =, new SecretKeySpec (tmp.getEncoded (),“AES"); ,,,, ,,,Cipher Cipher =, . getinstance (“AES/CBC/PKCS5PADDING"); ,,,cipher.init (Cipher.DECRYPT_MODE, secretKey,, ivspec); ,,,return new 字符串(cipher.doFinal (Base64.getDecoder () .decode (strToDecrypt))); ,,} catch 才能;(Exception e), { ,,,System.out.println (“Error  while 解密:,“,+,e.toString ()); ,,} return 才能;零; }
<>强测试AES256加密和解密方法强>
用一个简单的字符串测试我们的AES256加密和解密方法
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); null怎么在Java中利用AES256进行加密解密操作