日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java des3加密_JAVA加密算法(3)- 对称加密算法(DES、3DES、AES)

發布時間:2024/9/27 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java des3加密_JAVA加密算法(3)- 对称加密算法(DES、3DES、AES) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對稱加密算法概念

加密密鑰和解密密鑰相同,大部分算法加密揭秘過程互逆。

特點:算法公開、(相比非對稱加密)計算量小、加密速度快、效率高。

弱點:雙方都使用同樣的密鑰,安全性得不到保證。

常用對稱加密算法

DES(Data Encryption Standard)

3DES(DES加強版,使用3次DES計算,Triple DES,DESede)

AES(Advanced Encryption Standard,3DES加強版)

JDK版DES/3DES/AES算法調用模板

1. 生成密鑰

//KeyGenerator,密鑰生成器

KeyGenerator keyGen = KeyGenerator.getInstance("DES");//算法:DES,DESede,AES

//初始化密鑰生成器

keyGen.init(56); //各算法密鑰長度不同,參見說明

//生成密鑰

SecretKey secretKey = keyGen.generateKey();

//生產字節碼數據

byte[] key = secretKey.getEncoded();

說明:

1.通過「KeyGenerator.getInstance("DES")」生成密鑰,

2.參數為算法名稱:分別對應DES、DESede(即3DES)、AES

3.每種算法密鑰長度參數:DES(56),3DES(112,168),AES(192,256)

2.加/解密

//通過字節碼數據key 恢復密鑰

SecretKey secretKey = new SecretKeySpec(key, "DES");

//Cipher完成加密/解密工作

Cipher cipher = Cipher.getInstance("DES");

//根據密鑰,對Cipher初始化,并選擇加密還是解密

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

byte[] result = cipher.doFinal(data);

1.加密或解密都通過cipher.init()設置,參數:ENCRYPT_MODE/DECRYPT_MODE

2.加密或解密都通過cipher.doFinal() 執行,獲得byte[]類型結果。

代碼示例

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class DESUtil {

/*

* 生成密鑰

*/

public static byte[] initKey() throws Exception{

KeyGenerator keyGen = KeyGenerator.getInstance("DES");

keyGen.init(56);

SecretKey secretKey = keyGen.generateKey();

return secretKey.getEncoded();

}

/*

* DES 加密

*/

public static byte[] encrypt(byte[] data, byte[] key) throws Exception{

SecretKey secretKey = new SecretKeySpec(key, "DES");

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

byte[] cipherBytes = cipher.doFinal(data);

return cipherBytes;

}

/*

* DES 解密

*/

public static byte[] decrypt(byte[] data, byte[] key) throws Exception{

SecretKey secretKey = new SecretKeySpec(key, "DES");

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE, secretKey);

byte[] plainBytes = cipher.doFinal(data);

return plainBytes;

}

//Test

public static void main(String[] args) throws Exception {

byte[] desKey = DESUtil.initKey();

System.out.println("DES KEY : " + BytesToHex.fromBytesToHex(desKey));

byte[] desResult = DESUtil.encrypt(DATA.getBytes(), desKey);

System.out.println(DATA + ">>>DES 加密結果>>>" + BytesToHex.fromBytesToHex(desResult));

byte[] desPlain = DESUtil.decrypt(desResult, desKey);

System.out.println(DATA + ">>>DES 解密結果>>>" + new String(desPlain));

}

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java des3加密_JAVA加密算法(3)- 对称加密算法(DES、3DES、AES)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。