溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaAES256加密解密代碼怎么寫呢

發(fā)布時間:2021-10-13 10:00:28 來源:億速云 閱讀:132 作者:柒染 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)JavaAES256加密解密代碼怎么寫呢,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

Java支持許多安全的加密算法,但是其中一些功能較弱,無法在安全性要求很高的應(yīng)用程序中使用。例如,數(shù)據(jù)加密標(biāo)準(zhǔn)(DES)加密算法被認(rèn)為是高度不安全的。今天介紹一下AES 256加密解密。

什么是AES 256?

高級加密標(biāo)準(zhǔn) (英語:Advanced Encryption Standard,縮寫:AES ),在密碼學(xué)中又稱Rijndael加密法,是美國聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。這個標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用。  AES是一種對稱加密算法。它旨在易于在硬件和軟件以及受限環(huán)境中實施,并提供針對各種攻擊技術(shù)的良好防御。AES是能夠使用大小為128、192和256位的密鑰處理128位塊的塊密碼。每個密碼分別使用128位,192位和256位的加密密鑰對128位塊中的數(shù)據(jù)進(jìn)行加密和解密。它使用相同的密鑰進(jìn)行加密和解密,因此發(fā)送方和接收方都必須知道并使用相同的秘密密鑰。

在下面的加密和解密示例中,我在UTF-8字符集中使用了base64編碼。用于顯示程序的輸出。也可以以字節(jié)數(shù)組格式存儲和驗證數(shù)據(jù)。

AES 256加密

Java程序中,用于使用AES 256位對密碼(或任何信息)進(jìn)行加密。

private static String secretKey = "boooooooooom!!!!";private static String salt = "ssshhhhhhhhhhh!!!!"; public static String encrypt(String strToEncrypt, String secret){  try  {    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(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(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));  }  catch (Exception e)  {    System.out.println("Error while encrypting: " + e.toString());  }  return null;}

AES 256解密

Java程序,用于使用AES 256位解密密碼(或任何信息)。

private static String secretKey = "boooooooooom!!!!";private static String salt = "ssshhhhhhhhhhh!!!!"; public static String decrypt(String strToDecrypt, String secret) {  try  {    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(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;}

測試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);}

輸出結(jié)果

www.csdn.netbiXhp3Ha1fgxVEp48zHrvVoXMStmxPuAPHo3TVz5lHU=www.csdn.net

關(guān)于JavaAES256加密解密代碼怎么寫呢就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI