色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Java 實現對稱加密算法

瀏覽:4日期:2022-08-20 18:05:42

概述

采用單鑰密碼系統的加密方法,同一個密鑰可以同時用作信息的加密和解密,這種加密方法稱為對稱加密,也稱為單密鑰加密。在對稱加密算法中,DES算法最具有代表性,DESede是DES算法的變種,AES算法則作為DES算法的替代者。

DES

DES(Data Encryption Standard),即數據加密標準,是一種使用密鑰加密的塊算法,1977年被美國聯邦政府的國家標準局確定為聯邦資料處理標準(FIPS),并授權在非密級政府通信中使用,隨后該算法在國際上廣泛流傳開來。

import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class DesUtil { /** * DES加密 * @param content 待加密數據 * @param key 密鑰 * @return * @throws Exception */ public static String desEncrypt(String content, String key) throws Exception { //指定加密算法、加密模式、填充模式 Cipher cipher = Cipher.getInstance('DES/ECB/PKCS5Padding'); //創建加密規則:指定key和加密類型 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 'DES'); //指定加密模式為加密,指定加密規則 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); //調用加密方法 byte[] result = cipher.doFinal(content.getBytes()); //用Base64編碼 return new String(Base64.getEncoder().encode(result)); } /** * DES解密 * @param content 待解密數據 * @param key 密鑰 * @return * @throws Exception */ public static String desDecrypt(String content, String key) throws Exception { //Base64解碼 byte[] result = Base64.getDecoder().decode(content); //指定加密算法、加密模式、填充模式 Cipher cipher = Cipher.getInstance('DES/ECB/PKCS5Padding'); //創建加密規則:指定key和加密類型 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 'DES'); //指定加密模式為解密,指定加密規則 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return new String(cipher.doFinal(result)); } public static void main(String[] args) throws Exception { //key要8位,不然會報錯:java.security.InvalidKeyException: Wrong key size String key = '12345678'; //待加密數據 String content = '對稱加密算法'; //加密 System.out.println(desEncrypt(content, key));//qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor //解密 System.out.println(desDecrypt('qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor', key));//對稱加密算法 }}

DESede

DESede是由DES改進后的一種對稱加密算法,針對其密鑰長度偏短和迭代次數偏少等問題做了相應改進,提高了安全強度。

import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class DesedeUtil { /** * Desede加密 * @param content 待加密數據 * @param key 密鑰 * @return * @throws Exception */ public static String desEncrypt(String content, String key) throws Exception { //指定加密算法、加密模式、填充模式 Cipher cipher = Cipher.getInstance('DESede/ECB/PKCS5Padding'); //創建加密規則:指定key和加密類型 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 'DESede'); //指定加密模式為加密,指定加密規則 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); //調用加密方法 byte[] result = cipher.doFinal(content.getBytes()); //用Base64編碼 return new String(Base64.getEncoder().encode(result)); } /** * Desede解密 * @param content 待解密數據 * @param key 密鑰 * @return * @throws Exception */ public static String desDecrypt(String content, String key) throws Exception { //Base64解碼 byte[] result = Base64.getDecoder().decode(content); //指定加密算法、加密模式、填充模式 Cipher cipher = Cipher.getInstance('DESede/ECB/PKCS5Padding'); //創建加密規則:指定key和加密類型 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 'DESede'); //指定加密模式為解密,指定加密規則 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return new String(cipher.doFinal(result)); } public static void main(String[] args) throws Exception { //key要24位,不然會報錯:java.security.InvalidKeyException: Wrong key size String key = '123456781234567812345678'; //待加密數據 String content = '對稱加密算法'; //加密 System.out.println(desEncrypt(content, key));//qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor //解密 System.out.println(desDecrypt('qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor', key));//對稱加密算法 }}

AES

AES(Advanced Encryption Standard),即高級加密標準,在密碼學中又稱Rijndael加密法,是美國聯邦政府采用的一種區塊加密標準。這個標準用來替代原先的DES,已經被多方分析且廣為全世界所使用。

import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class AesUtil { /** * aes加密 * @param content 待加密數據 * @param key 密鑰 * @return * @throws Exception */ public static String aesEncrypt(String content, String key) throws Exception { //指定加密算法 Cipher cipher = Cipher.getInstance('AES'); //創建加密規則:指定key和加密類型 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 'AES'); //指定加密模式為加密,指定加密規則 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); //調用加密方法 byte[] result = cipher.doFinal(content.getBytes()); //用Base64編碼 return new String(Base64.getEncoder().encode(result)); } /** * aes解密 * @param content 待解密數據 * @param key 密鑰 * @return * @throws Exception */ public static String aesDecrypt(String content, String key) throws Exception { //Base64解碼 byte[] result = Base64.getDecoder().decode(content); //指定加密算法 Cipher cipher = Cipher.getInstance('AES'); //創建加密規則:指定key和加密類型 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 'AES'); //指定加密模式為解密,指定加密規則 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return new String(cipher.doFinal(result)); } public static void main(String[] args) throws Exception { //key要16/24/32位,不然會報錯:java.security.InvalidKeyException: Wrong key size String key = '12345678123456781234567812345678'; String content = '對稱加密算法'; //加密 System.out.println(aesEncrypt(content, key));//yrdeR6atwBX0yeXzudk/al6q8K61gyPylX7GfwsKP9w= //解密 System.out.println(aesDecrypt('yrdeR6atwBX0yeXzudk/al6q8K61gyPylX7GfwsKP9w=', key)); }}

以上就是Java 實現對稱加密算法的詳細內容,更多關于Java 對稱加密算法的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 一色屋成人免费精品网 | 台湾三级毛片 | 欧美在线视频观看 | 亚洲清纯自偷自拍另类专区 | 久草视频在线观 | 亚洲一区二区三区高清视频 | 亚洲厕拍| 欧美另类在线视频 | 亚洲天堂视频网 | 欧美日韩在线国产 | 国产剧情一区二区 | 成人自拍小视频 | 免费一级毛片在级播放 | 91精品手机国产露脸 | 久久精品香蕉视频 | 亚洲视频自拍 | 欧美一级毛片日本 | 特黄aaaaaa久久片 | 亚洲综合一 | 一区二区中文字幕在线观看 | 免费看黄色的网址 | 久久久99精品免费观看精品 | 日本成aⅴ人片日本伦 | 国产精品黄网站免费观看 | 国产精品成人久久久久久久 | 欧美日韩国产综合一区二区三区 | 狠狠色丁香九九婷婷综合五月 | 国产一级做a爱片久久毛片a | 欧美极品video粗暴 | 在线免费成人网 | 成年男人午夜片免费观看 | 永久免费精品视频 | 欧美极品在线视频 | 欧美三级在线观看不卡视频 | 网友自拍第一页 | 国产日本欧美高清免费区 | 高清一区在线 | 女在床上被男的插爽叫视频 | 免费一级特黄欧美大片久久网 | 欧美日韩视频一区二区 | 99精品这里只有精品高清视频 |