现代密码学之对称加密-DES及AES算法
生活随笔
收集整理的這篇文章主要介紹了
现代密码学之对称加密-DES及AES算法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
提示:
本文章基于上一篇基礎(chǔ)理論知識(shí)介紹 鏈接: 現(xiàn)代密碼學(xué)之描述概要(一)
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:密碼,獲取加密對(duì)象// transformation:參數(shù)表示使用什么類型加密Cipher cipher = Cipher.getInstance(transformation);// 指定秘鑰規(guī)則// 第一個(gè)參數(shù)表示:密鑰,key的字節(jié)數(shù)組// 第二個(gè)參數(shù)表示:算法SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);// 對(duì)加密進(jìn)行初始化// 第一個(gè)參數(shù):表示模式,有加密模式和解密模式// 第二個(gè)參數(shù):表示秘鑰規(guī)則cipher.init(Cipher.ENCRYPT_MODE,sks);// 進(jìn)行加密byte[] bytes = cipher.doFinal(input.getBytes());// 打印字節(jié),因?yàn)閍scii碼有負(fù)數(shù),解析不出來(lái),所以亂碼 // for (byte b : bytes) { // System.out.println(b); // }// 打印密文System.out.println(new String(bytes));} }運(yùn)行:
修改 密鑰 key = “12345678” ,再次運(yùn)行 ,出現(xiàn)亂碼是因?yàn)閷?duì)應(yīng)的字節(jié)出現(xiàn)負(fù)數(shù),但負(fù)數(shù),沒(méi)有出現(xiàn)在 ascii 碼表里面,所以出現(xiàn)亂碼,需要配合base64進(jìn)行轉(zhuǎn)碼
使用 base64 進(jìn)行編碼
base64 導(dǎo)包的時(shí)候,需要注意 ,別導(dǎo)錯(cuò)了,需要導(dǎo)入 apache 包
運(yùn)行程序
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個(gè)字節(jié)public static void main(String[] args) throws Exception {String input ="硅谷";// DES加密算法,key的大小必須是8個(gè)字節(jié)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加密數(shù)據(jù)** @param input : 原文* @param key : 密鑰(DES,密鑰的長(zhǎng)度必須是8個(gè)字節(jié))* @param transformation : 獲取Cipher對(duì)象的算法* @param algorithm : 獲取密鑰的算法* @return : 密文* @throws Exception*/private static String encryptDES(String input, String key, String transformation, String algorithm) throws Exception {// 獲取加密對(duì)象Cipher cipher = Cipher.getInstance(transformation);// 創(chuàng)建加密規(guī)則// 第一個(gè)參數(shù)key的字節(jié)// 第二個(gè)參數(shù)表示加密算法SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);// ENCRYPT_MODE:加密模式// DECRYPT_MODE: 解密模式// 初始化加密模式和算法cipher.init(Cipher.ENCRYPT_MODE,sks);// 加密byte[] bytes = cipher.doFinal(input.getBytes());// 輸出加密后的數(shù)據(jù)String encode = Base64.encodeBase64String(bytes);return encode;}/*** 使用DES解密** @param input : 密文* @param key : 密鑰* @param transformation : 獲取Cipher對(duì)象的算法* @param algorithm : 獲取密鑰的算法* @throws Exception* @return: 原文*/private static String decryptDES(String input, String key, String transformation, String algorithm) throws Exception {// 1,獲取Cipher對(duì)象Cipher cipher = Cipher.getInstance(transformation);// 指定密鑰規(guī)則SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);cipher.init(Cipher.DECRYPT_MODE, sks);// 3. 解密,上面使用的base64編碼,下面直接用密文byte[] bytes = cipher.doFinal(Base64.decodeBase64(input));// 因?yàn)槭敲魑?#xff0c;所以直接返回return new String(bytes);} }運(yùn)行程序:
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個(gè)字節(jié)public static void main(String[] args) throws Exception {String input ="硅谷";// AES加密算法,比較高級(jí),所以key的大小必須是16個(gè)字節(jié)String key = "1234567812345678";String transformation = "AES"; // 9PQXVUIhaaQ=// 指定獲取密鑰的算法String algorithm = "AES";// 先測(cè)試加密,然后在測(cè)試解密String encryptDES = encryptDES(input, key, transformation, algorithm);System.out.println("加密:" + encryptDES);String s = dncryptDES(encryptDES, key, transformation, algorithm);System.out.println("解密:" + s);}/*** 使用DES加密數(shù)據(jù)** @param input : 原文* @param key : 密鑰(DES,密鑰的長(zhǎng)度必須是8個(gè)字節(jié))* @param transformation : 獲取Cipher對(duì)象的算法* @param algorithm : 獲取密鑰的算法* @return : 密文* @throws Exception*/private static String encryptDES(String input, String key, String transformation, String algorithm) throws Exception {// 獲取加密對(duì)象Cipher cipher = Cipher.getInstance(transformation);// 創(chuàng)建加密規(guī)則// 第一個(gè)參數(shù)key的字節(jié)// 第二個(gè)參數(shù)表示加密算法SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);// ENCRYPT_MODE:加密模式// DECRYPT_MODE: 解密模式// 初始化加密模式和算法cipher.init(Cipher.ENCRYPT_MODE,sks);// 加密byte[] bytes = cipher.doFinal(input.getBytes());// 輸出加密后的數(shù)據(jù)String encode = Base64.encode(bytes);return encode;}/*** 使用DES解密** @param input : 密文* @param key : 密鑰* @param transformation : 獲取Cipher對(duì)象的算法* @param algorithm : 獲取密鑰的算法* @throws Exception* @return: 原文*/private static String dncryptDES(String input, String key, String transformation, String algorithm) throws Exception {// 1,獲取Cipher對(duì)象Cipher cipher = Cipher.getInstance(transformation);// 指定密鑰規(guī)則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);} }運(yùn)行程序:AES 加密的密鑰key , 需要傳入16個(gè)字節(jié)
運(yùn)行程序
總結(jié)
以上是生活随笔為你收集整理的现代密码学之对称加密-DES及AES算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 移动端UI框架大比拼
- 下一篇: 文件批量重命名001到100的办法