這篇文章將為大家詳細(xì)講解有關(guān)JavaAES256加密解密代碼怎么寫呢,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
Java支持許多安全的加密算法,但是其中一些功能較弱,無(wú)法在安全性要求很高的應(yīng)用程序中使用。例如,數(shù)據(jù)加密標(biāo)準(zhǔn)(DES)加密算法被認(rèn)為是高度不安全的。今天介紹一下AES 256加密解密。
什么是AES 256?
高級(jí)加密標(biāo)準(zhǔn) (英語(yǔ):Advanced Encryption Standard,縮寫:AES ),在密碼學(xué)中又稱Rijndael加密法,是美國(guó)聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。這個(gè)標(biāo)準(zhǔn)用來(lái)替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用。 AES是一種對(duì)稱加密算法。它旨在易于在硬件和軟件以及受限環(huán)境中實(shí)施,并提供針對(duì)各種攻擊技術(shù)的良好防御。AES是能夠使用大小為128、192和256位的密鑰處理128位塊的塊密碼。每個(gè)密碼分別使用128位,192位和256位的加密密鑰對(duì)128位塊中的數(shù)據(jù)進(jìn)行加密和解密。它使用相同的密鑰進(jìn)行加密和解密,因此發(fā)送方和接收方都必須知道并使用相同的秘密密鑰。
在下面的加密和解密示例中,我在UTF-8字符集中使用了base64編碼。用于顯示程序的輸出。也可以以字節(jié)數(shù)組格式存儲(chǔ)和驗(yàn)證數(shù)據(jù)。
AES 256加密
Java程序中,用于使用AES 256位對(duì)密碼(或任何信息)進(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;}
測(cè)試AES256加密和解密方法
用一個(gè)簡(jiǎn)單的字符串測(cè)試我們的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)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享名稱:JavaAES256加密解密代碼怎么寫呢-創(chuàng)新互聯(lián)
瀏覽地址:http://m.kartarina.com/article22/ccjocc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、靜態(tài)網(wǎng)站、微信公眾號(hào)、Google、網(wǎng)站營(yíng)銷、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容