现代密码学之对称加密-DES及AES算法
生活随笔
收集整理的這篇文章主要介紹了
现代密码学之对称加密-DES及AES算法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
提示:
本文章基于上一篇基礎理論知識介紹 鏈接: 現代密碼學之描述概要(一)
DES加密
示例代碼 des加密算法
Cipher :文檔 https://docs.oracle.com/javase/8/docs/api/javax/crypto/Cipher.html#getInstance-java.lang.String-
package com.atguigu.desaes; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec;public class DesAesDemo {public static void main(String[] args) throws Exception{// 原文String input = "硅谷";// des加密必須是8位String key = "12345678";// 算法String algorithm = "DES";String transformation = "DES";// Cipher:密碼,獲取加密對象// transformation:參數表示使用什么類型加密Cipher cipher = Cipher.getInstance(transformation);// 指定秘鑰規則// 第一個參數表示:密鑰,key的字節數組// 第二個參數表示:算法SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);// 對加密進行初始化// 第一個參數:表示模式,有加密模式和解密模式// 第二個參數:表示秘鑰規則cipher.init(Cipher.ENCRYPT_MODE,sks);// 進行加密byte[] bytes = cipher.doFinal(input.getBytes());// 打印字節,因為ascii碼有負數,解析不出來,所以亂碼 // for (byte b : bytes) { // System.out.println(b); // }// 打印密文System.out.println(new String(bytes));} }運行:
修改 密鑰 key = “12345678” ,再次運行 ,出現亂碼是因為對應的字節出現負數,但負數,沒有出現在 ascii 碼表里面,所以出現亂碼,需要配合base64進行轉碼
使用 base64 進行編碼
base64 導包的時候,需要注意 ,別導錯了,需要導入 apache 包
運行程序
DES解密:
package com.atguigu.desaes;import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec;public class DesDemo {// DES加密算法,key的大小必須是8個字節public static void main(String[] args) throws Exception {String input ="硅谷";// DES加密算法,key的大小必須是8個字節String key = "12345678";String transformation = "DES"; // 9PQXVUIhaaQ=// 指定獲取密鑰的算法String algorithm = "DES";String encryptDES = encryptDES(input, key, transformation, algorithm);System.out.println("加密:" + encryptDES);String s = decryptDES(encryptDES, key, transformation, algorithm);System.out.println("解密:" + s);}/*** 使用DES加密數據** @param input : 原文* @param key : 密鑰(DES,密鑰的長度必須是8個字節)* @param transformation : 獲取Cipher對象的算法* @param algorithm : 獲取密鑰的算法* @return : 密文* @throws Exception*/private static String encryptDES(String input, String key, String transformation, String algorithm) throws Exception {// 獲取加密對象Cipher cipher = Cipher.getInstance(transformation);// 創建加密規則// 第一個參數key的字節// 第二個參數表示加密算法SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);// ENCRYPT_MODE:加密模式// DECRYPT_MODE: 解密模式// 初始化加密模式和算法cipher.init(Cipher.ENCRYPT_MODE,sks);// 加密byte[] bytes = cipher.doFinal(input.getBytes());// 輸出加密后的數據String encode = Base64.encodeBase64String(bytes);return encode;}/*** 使用DES解密** @param input : 密文* @param key : 密鑰* @param transformation : 獲取Cipher對象的算法* @param algorithm : 獲取密鑰的算法* @throws Exception* @return: 原文*/private static String decryptDES(String input, String key, String transformation, String algorithm) throws Exception {// 1,獲取Cipher對象Cipher cipher = Cipher.getInstance(transformation);// 指定密鑰規則SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);cipher.init(Cipher.DECRYPT_MODE, sks);// 3. 解密,上面使用的base64編碼,下面直接用密文byte[] bytes = cipher.doFinal(Base64.decodeBase64(input));// 因為是明文,所以直接返回return new String(bytes);} }運行程序:
AES加密解密
AES 加密解密和 DES 加密解密代碼一樣,只需要修改加密算法就行,拷貝 ESC 代碼
package com.atguigu.desaes; import com.sun.org.apache.xml.internal.security.utils.Base64;import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec;public class AesDemo {// DES加密算法,key的大小必須是8個字節public static void main(String[] args) throws Exception {String input ="硅谷";// AES加密算法,比較高級,所以key的大小必須是16個字節String key = "1234567812345678";String transformation = "AES"; // 9PQXVUIhaaQ=// 指定獲取密鑰的算法String algorithm = "AES";// 先測試加密,然后在測試解密String encryptDES = encryptDES(input, key, transformation, algorithm);System.out.println("加密:" + encryptDES);String s = dncryptDES(encryptDES, key, transformation, algorithm);System.out.println("解密:" + s);}/*** 使用DES加密數據** @param input : 原文* @param key : 密鑰(DES,密鑰的長度必須是8個字節)* @param transformation : 獲取Cipher對象的算法* @param algorithm : 獲取密鑰的算法* @return : 密文* @throws Exception*/private static String encryptDES(String input, String key, String transformation, String algorithm) throws Exception {// 獲取加密對象Cipher cipher = Cipher.getInstance(transformation);// 創建加密規則// 第一個參數key的字節// 第二個參數表示加密算法SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);// ENCRYPT_MODE:加密模式// DECRYPT_MODE: 解密模式// 初始化加密模式和算法cipher.init(Cipher.ENCRYPT_MODE,sks);// 加密byte[] bytes = cipher.doFinal(input.getBytes());// 輸出加密后的數據String encode = Base64.encode(bytes);return encode;}/*** 使用DES解密** @param input : 密文* @param key : 密鑰* @param transformation : 獲取Cipher對象的算法* @param algorithm : 獲取密鑰的算法* @throws Exception* @return: 原文*/private static String dncryptDES(String input, String key, String transformation, String algorithm) throws Exception {// 1,獲取Cipher對象Cipher cipher = Cipher.getInstance(transformation);// 指定密鑰規則SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);cipher.init(Cipher.DECRYPT_MODE, sks);// 3. 解密byte[] bytes = cipher.doFinal(Base64.decode(input));return new String(bytes);} }運行程序:AES 加密的密鑰key , 需要傳入16個字節
運行程序
總結
以上是生活随笔為你收集整理的现代密码学之对称加密-DES及AES算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 移动端UI框架大比拼
- 下一篇: C# - 习题07_计算1分2分5分硬币