国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Java 實(shí)現(xiàn)對(duì)稱加密算法

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

概述

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

DES

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

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

DESede

DESede是由DES改進(jìn)后的一種對(duì)稱加密算法,針對(duì)其密鑰長(zhǎng)度偏短和迭代次數(shù)偏少等問題做了相應(yīng)改進(jìn),提高了安全強(qiáng)度。

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

AES

AES(Advanced Encryption Standard),即高級(jí)加密標(biāo)準(zhǔn),在密碼學(xué)中又稱Rijndael加密法,是美國(guó)聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。這個(gè)標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用。

import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class AesUtil { /** * aes加密 * @param content 待加密數(shù)據(jù) * @param key 密鑰 * @return * @throws Exception */ public static String aesEncrypt(String content, String key) throws Exception { //指定加密算法 Cipher cipher = Cipher.getInstance('AES'); //創(chuàng)建加密規(guī)則:指定key和加密類型 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 'AES'); //指定加密模式為加密,指定加密規(guī)則 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); //調(diào)用加密方法 byte[] result = cipher.doFinal(content.getBytes()); //用Base64編碼 return new String(Base64.getEncoder().encode(result)); } /** * aes解密 * @param content 待解密數(shù)據(jù) * @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'); //創(chuàng)建加密規(guī)則:指定key和加密類型 SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 'AES'); //指定加密模式為解密,指定加密規(guī)則 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return new String(cipher.doFinal(result)); } public static void main(String[] args) throws Exception { //key要16/24/32位,不然會(huì)報(bào)錯(cuò):java.security.InvalidKeyException: Wrong key size String key = '12345678123456781234567812345678'; String content = '對(duì)稱加密算法'; //加密 System.out.println(aesEncrypt(content, key));//yrdeR6atwBX0yeXzudk/al6q8K61gyPylX7GfwsKP9w= //解密 System.out.println(aesDecrypt('yrdeR6atwBX0yeXzudk/al6q8K61gyPylX7GfwsKP9w=', key)); }}

以上就是Java 實(shí)現(xiàn)對(duì)稱加密算法的詳細(xì)內(nèi)容,更多關(guān)于Java 對(duì)稱加密算法的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国内自产拍自a免费毛片 | 成人国产综合 | 毛片天堂 | 日韩乱码中文字幕视频 | 一区二区三区网站在线免费线观看 | 国产成人亚洲综合网站不卡 | 美国三级网 | 中文字幕有码在线观看 | 日本人的色道www免费一区 | 青木玲中文字幕一区二区 | 69欧美 | 波多野结衣在线观看一区二区 | 精品动漫一区二区 | 欧美日韩一区二区三 | 日韩午夜三级 | 国产在线视频欧美亚综合 | 牛牛a级毛片在线播放 | 国产三级在线观看a | 国产黄色小视频在线观看 | 免费va国产高清不卡大片 | 精品久久久久中文字幕日本 | 国产精品麻豆一区二区三区v视界 | 久久成人性色生活片 | 国产91美女 | 国产五区 | 18视频网站在线观看 | 一级做a爱 一区 | 91精品免费高清在线 | 精品一区二区三区免费视频 | 久久精品中文字幕首页 | 欧美一级毛片日本 | 国产精品夜色视频一区二区 | 中文日韩字幕一区在线观看 | 亚洲精品一区二区四季 | 久久99精品免费视频 | 黄网在线观看免费 | 亚洲日本一区二区三区在线 | 99久久精品全部 | 亚洲gogo人体大胆西西安徽 | 草草影院欧美三级日本 | 欧美一级成人影院免费的 |