Java 實現(xiàn)對稱加密算法
概述
采用單鑰密碼系統(tǒng)的加密方法,同一個密鑰可以同時用作信息的加密和解密,這種加密方法稱為對稱加密,也稱為單密鑰加密。在對稱加密算法中,DES算法最具有代表性,DESede是DES算法的變種,AES算法則作為DES算法的替代者。
DES
DES(Data Encryption Standard),即數(shù)據(jù)加密標(biāo)準(zhǔn),是一種使用密鑰加密的塊算法,1977年被美國聯(lián)邦政府的國家標(biāo)準(zhǔn)局確定為聯(lián)邦資料處理標(biāo)準(zhǔn)(FIPS),并授權(quán)在非密級政府通信中使用,隨后該算法在國際上廣泛流傳開來。
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位,不然會報錯:java.security.InvalidKeyException: Wrong key size
String key = "12345678";
//待加密數(shù)據(jù)
String content = "對稱加密算法";
//加密
System.out.println(desEncrypt(content, key));//qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor
//解密
System.out.println(desDecrypt("qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor", key));//對稱加密算法
}
}
DESede
DESede是由DES改進后的一種對稱加密算法,針對其密鑰長度偏短和迭代次數(shù)偏少等問題做了相應(yī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位,不然會報錯:java.security.InvalidKeyException: Wrong key size
String key = "123456781234567812345678";
//待加密數(shù)據(jù)
String content = "對稱加密算法";
//加密
System.out.println(desEncrypt(content, key));//qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor
//解密
System.out.println(desDecrypt("qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor", key));//對稱加密算法
}
}
AES
AES(Advanced Encryption Standard),即高級加密標(biāo)準(zhǔn),在密碼學(xué)中又稱Rijndael加密法,是美國聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。這個標(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位,不然會報錯: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 實現(xiàn)對稱加密算法的詳細(xì)內(nèi)容,更多關(guān)于Java 對稱加密算法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)之插入排序
這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)之插入排序的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Java中使用JavaMail多發(fā)郵件及郵件的驗證和附件實現(xiàn)
這篇文章主要介紹了Java中使用Java Mail多發(fā)郵件及郵件的驗證和附件實現(xiàn),包括在郵件中加入圖片等功能的實現(xiàn)講解,需要的朋友可以參考下2016-02-02
Spring獲取當(dāng)前類在容器中的beanname實現(xiàn)思路
這篇文章主要介紹了Spring獲取當(dāng)前類在容器中的beanname,實現(xiàn)思路只需繼承BeanNameAware接口,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
Spring3 MVC請求參數(shù)獲取的幾種方法小結(jié)
本篇文章主要介紹了Spring3 MVC請求參數(shù)獲取的幾種方法小結(jié),非常具有實用價值,需要的朋友可以參考下。2017-03-03
java中 IO 常用IO操作類繼承結(jié)構(gòu)分析
本篇文章小編為大家介紹,java中 IO 常用IO操作類繼承結(jié)構(gòu)分析。需要的朋友參考下2013-04-04

