Java 實(shí)現(xiàn)對(duì)稱加密算法
概述
采用單鑰密碼系統(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ó)際上廣泛流傳開來(lái)。
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)用來(lái)替代原先的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)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java網(wǎng)絡(luò)編程基礎(chǔ)用法詳解
網(wǎng)絡(luò)編程是指編寫運(yùn)行在多個(gè)設(shè)備(計(jì)算機(jī))的程序,這些設(shè)備都通過網(wǎng)絡(luò)連接起來(lái),本文將帶大家詳細(xì)了解Java的網(wǎng)絡(luò)編程,文中有相關(guān)的代碼示例,需要的朋友可以參考下2023-05-05
Java使用itextpdf實(shí)現(xiàn)PDF轉(zhuǎn)文本以及轉(zhuǎn)圖片
PDF轉(zhuǎn)文本的插件常用的有pdfbox ,itextpdf 和 spire.pdf,本文主要介紹如何使用itextpdf實(shí)現(xiàn)PDF轉(zhuǎn)文本以及轉(zhuǎn)圖片,需要的可以參考一下2025-01-01
淺談@RequestParam 參數(shù)是否必須傳的問題
這篇文章主要介紹了淺談@RequestParam 參數(shù)是否必須傳的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-02-02
mybatisPlus填坑之邏輯刪除的實(shí)現(xiàn)
本文主要介紹了mybatisPlus填坑之邏輯刪除的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
SpringBoot?整合Security權(quán)限控制的初步配置
這篇文章主要為大家介紹了SpringBoot?整合Security權(quán)限控制的初步配置實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
springboot2自動(dòng)加載sql文件的實(shí)現(xiàn)
本文主要介紹了springboot2自動(dòng)加載sql文件的實(shí)現(xiàn),通過配置文件或注解的方式,我們可以輕松地將SQL語(yǔ)句映射到數(shù)據(jù)庫(kù)中,實(shí)現(xiàn)自動(dòng)加載,感興趣的可以了解一下2023-11-11

