java基于Des對稱加密算法實現(xiàn)的加密與解密功能詳解
本文實例講述了java基于Des對稱加密算法實現(xiàn)的加密與解密功能。分享給大家供大家參考,具體如下:
Des 加密相關(guān)類介紹:
SecureRandom 這個類是繼承自java.util.Random 這個類
SecureRandom 這個類的構(gòu)造器有三種,下面例舉兩種:
SecureRandom()構(gòu)造一個實現(xiàn)默認(rèn)隨機數(shù)算法的安全隨機數(shù)生成器 (RNG)。
SecureRandom(byte[] seed)構(gòu)造一個實現(xiàn)默認(rèn)隨機數(shù)算法的安全隨機數(shù)生成器 (RNG)。
DESKeySpec 這個類是用來使用原始秘鑰來生成秘鑰的秘鑰內(nèi)容
DESKeySpec 有兩個構(gòu)造函數(shù):
DESKeySpec(byte[] key) 創(chuàng)建一個 DESKeySpec 對象,使用 key 中的前 8 個字節(jié)作為 DES 密鑰的密鑰內(nèi)容。
DESKeySpec(byte[] key, int offset) 創(chuàng)建一個 DESKeySpec 對象,使用 key 中始于且包含 offset 的前 8 個字節(jié)作為 DES-EDE 密鑰的密鑰內(nèi)容。
SecretKeyFactory , 密鑰工廠用來將密鑰(類型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。
SecretKey對象,秘鑰對象,通過調(diào)用秘鑰工廠的generateSecret(DESKeySpec deskeyspace) 方法來生成秘鑰
Cipher 類為加密和解密提供密碼功能,通過調(diào)用Cipher的getInstance("des") 來獲取實例
Cipher 對象調(diào)用init() 方法進行對象的初始化,init() 方法的具體參數(shù)按照具體情況而定,有加密的也有解密的常量
最后調(diào)用Cipher的doFinal() 方法進行加密解密。
在這里請教大家一個問題,不管是第一種使用BASE64Encoder編碼還是第二種org.apache.commons.codec.binary.Base64編碼,在將String 轉(zhuǎn)化為byte以及將byte轉(zhuǎn)化為String 時需要 UTF-8/GBK 等編碼來編碼,解碼嗎?
一、使用了 sun.misc.BASE64Decoder 和BASE64Encoder 進行解碼,編碼
package com.soufun.com;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
// 導(dǎo)入sun的64位編碼
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*@author WHD
*
*即使導(dǎo)入sun.misc這個架包也會報錯,這時首先把你的JRE架包移除再導(dǎo)入一次就可以了
*/
public class DesUtil {
// 定義加密方式
private final static String DES = "DES";
private final static String UTF8="GBK";
static SecretKeyFactory keyFactory = null;
static {
try {
keyFactory=SecretKeyFactory.getInstance("DES");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
long begin=new Date().getTime();
String data = "aaades加密測試";
// 注意:DES加密和解密過程中,密鑰長度都必須是8的倍數(shù)
String key = "qazwsxed";
System.err.println(encrypt(data, key));
System.err.println(decrypt(encrypt(data, key), key));
long end =new Date().getTime();
System.out.println(end-begin);
}
/**
* Description 根據(jù)鍵值進行加密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws Exception
*/
public static String encrypt(String data, String key) throws Exception {
// 使用指定的編碼獲取要加密的內(nèi)容,一般秘鑰都是字母或數(shù)字不用指定編碼,但指定也可以
byte[] bt = encrypt(data.getBytes(UTF8), key.getBytes(UTF8));
//注意:在加密和解密的時候使用sun的BASE64Encoder()進行編碼和解碼不然會有亂碼
//網(wǎng)上查看了很多實例,都沒有編碼和解碼,也說沒有亂碼問題,而我這里出現(xiàn)了亂碼,所以使用BASE64Encoder()進行了編碼解碼
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* Description 根據(jù)鍵值進行解密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws IOException
* @throws Exception
*/
public static String decrypt(String data, String key) throws IOException,
Exception {
if (data == null)
return null;
//注意:在加密和解密的時候使用sun的BASE64Encoder()進行編碼和解碼不然會有亂碼
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf,key.getBytes());
return new String(bt,UTF8);
}
/**
* Description 根據(jù)鍵值進行加密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws Exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數(shù)源
SecureRandom sr = new SecureRandom();
// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對象,也就是創(chuàng)建秘鑰的秘鑰內(nèi)容
DESKeySpec dks = new DESKeySpec(key);
// 密鑰工廠用來將密鑰(類型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。
// 這里改為使用單例模式
//SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//根據(jù)提供的密鑰規(guī)范(密鑰材料)生成 SecretKey(秘鑰) 對象。
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher對象實際完成加密操作,此類為加密和解密提供密碼功能
Cipher cipher = Cipher.getInstance(DES);
// 用密鑰和隨機源初始化此 Cipher。ENCRYPT_MODE用于將 Cipher 初始化為加密模式的常量。
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
//正式執(zhí)行加密操作
return cipher.doFinal(data);
}
/**
* Description 根據(jù)鍵值進行解密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws Exception
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數(shù)源
SecureRandom sr = new SecureRandom();
// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對象,也就是創(chuàng)建秘鑰的秘鑰內(nèi)容
DESKeySpec dks = new DESKeySpec(key);
// 密鑰工廠用來將密鑰(類型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。
// 這里改為使用單例模式
//SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//根據(jù)提供的密鑰規(guī)范(密鑰材料)生成 SecretKey(秘鑰)對象。
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher類為加密和解密提供密碼功能
Cipher cipher = Cipher.getInstance(DES);
// DECRYPT_MODE用于將 Cipher 初始化為解密模式的常量。
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// 正式進行解密操作
return cipher.doFinal(data);
}
}
二、使用org.apache.commons.codec.binary.Base64 進行解碼,編碼
package com.soufun.com;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
*@author WHD
*
*/
public class DesUtil {
// 定義加密方式
private final static String DES = "DES";
private final static String UTF8="GBK";
static SecretKeyFactory keyFactory = null;
static {
try {
keyFactory=SecretKeyFactory.getInstance("DES");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
long begin=new Date().getTime();
String data = "aaades加密測試";
// 注意:DES加密和解密過程中,密鑰長度都必須是8的倍數(shù)
String key = "qazwsxed";
System.err.println(encrypt(data, key));
System.err.println(decrypt(encrypt(data, key), key));
long end =new Date().getTime();
System.out.println(end-begin);
}
/**
* Description 根據(jù)鍵值進行加密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws Exception
*/
public static String encrypt(String data, String key) throws Exception {
// 使用指定的編碼獲取要加密的內(nèi)容,一般秘鑰都是字母或數(shù)字不用指定編碼,但指定也可以
byte[] bt = encrypt(data.getBytes(UTF8), key.getBytes());
// 第一個使用了sun.misc.BASE64Encoder;進行了編碼,但網(wǎng)上說使用org.apache.commons.codec.binary.Base64比較好所以拿來試試
String strs = Base64.encodeBase64String(bt);
return strs;
}
/**
* Description 根據(jù)鍵值進行解密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws IOException
* @throws Exception
*/
public static String decrypt(String data, String key) throws IOException,
Exception {
if (data == null)
return null;
// 使用org.apache.commons.codec.binary.Base64解碼
byte [] buf=Base64.decodeBase64(data);
byte[] bt = decrypt(buf,key.getBytes());
return new String(bt,UTF8);
}
/**
* Description 根據(jù)鍵值進行加密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws Exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數(shù)源
SecureRandom sr = new SecureRandom();
// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對象,也就是創(chuàng)建秘鑰的秘鑰內(nèi)容
DESKeySpec dks = new DESKeySpec(key);
// 密鑰工廠用來將密鑰(類型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。
// 這里改為使用單例模式
//SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//根據(jù)提供的密鑰規(guī)范(密鑰材料)生成 SecretKey(秘鑰) 對象。
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher對象實際完成加密操作,此類為加密和解密提供密碼功能
Cipher cipher = Cipher.getInstance(DES);
// 用密鑰和隨機源初始化此 Cipher。ENCRYPT_MODE用于將 Cipher 初始化為加密模式的常量。
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
//正式執(zhí)行加密操作
return cipher.doFinal(data);
}
/**
* Description 根據(jù)鍵值進行解密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws Exception
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數(shù)源
SecureRandom sr = new SecureRandom();
// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對象,也就是創(chuàng)建秘鑰的秘鑰內(nèi)容
DESKeySpec dks = new DESKeySpec(key);
// 密鑰工廠用來將密鑰(類型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。
// 這里改為使用單例模式
//SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//根據(jù)提供的密鑰規(guī)范(密鑰材料)生成 SecretKey(秘鑰)對象。
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher類為加密和解密提供密碼功能
Cipher cipher = Cipher.getInstance(DES);
// DECRYPT_MODE用于將 Cipher 初始化為解密模式的常量。
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// 正式進行解密操作
return cipher.doFinal(data);
}
}
一、二中使用到的架包下載地址:
下載: sun.misc.BASE64Decoder 。
下載:apache的Base64編碼、解碼器 。
三、未使用任何編碼,解碼架包
package com.soufun.com;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
*@author WHD
*
*/
public class DESCrypt {
static SecretKeyFactory secretKeyFactory = null;
//Cipher 的“算法/模式/填充”
static final String CIPHER = "DES/CBC/PKCS5Padding";
static {
try {
// 在靜態(tài)代碼塊中獲取秘鑰工程
secretKeyFactory = SecretKeyFactory.getInstance("DES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
// 定義常量 ,編碼格式
private static final String UTF8 = "GBK";
/*
* 對象緩存的容器
*/
static abstract class Cache {
private final Map innerCache = new HashMap();
protected abstract Object createValue(Object key) throws Exception;
public Object get(Object key) throws Exception {
Object value;
synchronized (innerCache) {
value = innerCache.get(key);
if (value == null) {
value = new CreationPlaceholder();
innerCache.put(key, value);
}
}
if (value instanceof CreationPlaceholder) {
synchronized (value) {
CreationPlaceholder progress = (CreationPlaceholder) value;
if (progress.value == null) {
progress.value = createValue(key);
synchronized (innerCache) {
innerCache.put(key, progress.value);
}
}
return progress.value;
}
}
return value;
}
static final class CreationPlaceholder {
Object value;
}
}
/*
* hex->str & str->hex
*/
public static byte[] stringToHex(String ss) {
// 字符串轉(zhuǎn)化we
byte digest[] = new byte[ss.length() / 2];
for (int i = 0; i < digest.length; i++) {
String byteString = ss.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte) byteValue;
}
return digest;
}
public static String hexToString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2) {
hexString.append("0");
}
hexString.append(plainText);
}
return hexString.toString();
}
private static byte[] _convertKeyIv(String text) throws IOException {
if (text.length() == 8) {
return text.getBytes(UTF8);
}
if (text.startsWith("0x") && text.length() == 32) {
byte[] result = new byte[8];
for (int i = 0; i < text.length(); i += 2) {
if (text.charAt(i++) == '0' && text.charAt(i++) == 'x') {
try {
result[i / 4] = (byte) Integer.parseInt(
text.substring(i, i + 2), 16);
} catch (Exception e) {
throw new IOException("TXT '" + text + "' is invalid!");
}
}
}
return result;
}
throw new IOException("TXT '" + text + "' is invalid!");
}
/*
* SecretKey & IvParameterSpec的緩存
*/
private static Cache SecretKeySpecs = new Cache() {
protected Object createValue(Object key) throws Exception {
SecretKey secretKeyObj = null;
try {
secretKeyObj = secretKeyFactory.generateSecret(new DESKeySpec(
_convertKeyIv((String) key)));
} catch (Exception e) {
e.printStackTrace();
}
return secretKeyObj;
}
};
private static Cache IvParamSpecs = new Cache() {
protected Object createValue(Object key) throws Exception {
IvParameterSpec ivObj = null;
ivObj = new IvParameterSpec(_convertKeyIv((String) key));
return ivObj;
}
};
/*
* 加密&解密
*/
public static String encrypt(String text, String authKey, String authIv) {
SecretKey secretKeyObj = null;
IvParameterSpec ivObj = null;
try {
secretKeyObj = (SecretKey) SecretKeySpecs.get(authKey);
ivObj = (IvParameterSpec) IvParamSpecs.get(authIv);
} catch (Exception e) {
e.printStackTrace();
}
byte[] data = null;
try {
data = text.getBytes(UTF8);
} catch (Exception e) {
e.printStackTrace();
}
byte[] authToken = null;
try {
authToken = encrypt(data, secretKeyObj, ivObj);
} catch (Exception e) {
e.printStackTrace();
}
return hexToString(authToken);
}
public static byte[] encrypt(byte[] data, SecretKey secretKey,
IvParameterSpec iv) throws Exception {
Cipher cipher = Cipher.getInstance(CIPHER);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return cipher.doFinal(data);
}
public static String decrypt(String hexString, String authKey, String authIv)
throws Exception {
SecretKey secretKeyObj = null;
IvParameterSpec ivObj = null;
try {
secretKeyObj = (SecretKey) SecretKeySpecs.get(authKey);
ivObj = (IvParameterSpec) IvParamSpecs.get(authIv);
} catch (Exception e) {
e.printStackTrace();
}
String text = decrypt(hexString, secretKeyObj, ivObj);
return text;
}
public static String decrypt(String message, SecretKey secretKey,
IvParameterSpec iv) throws Exception {
byte[] data = stringToHex(message);
return decrypt(data, secretKey, iv);
}
public static String decrypt(byte[] data, SecretKey secretKey,
IvParameterSpec iv) throws Exception {
Cipher cipher = Cipher.getInstance(CIPHER);
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(data);
return new String(retByte);
}
public static void main(String[] args) throws Exception {
long begin= new Date().getTime();
String authKey = "w8f3k9c2";
String authIv = "w8f3k9c2";
String text = "aaades加密測試";
// 140CB412BA03869F
// 140cb412ba03869f
// 對原文進行加密
String encryptedText = encrypt(text, authKey, authIv);
System.out.println("encryptedText:" + encryptedText);
// 對密文進行還原
String plainText = decrypt(encryptedText, authKey, authIv);
System.out.println("plainText:" + plainText);
//2a329740ce15f549be64190b183a5be2
long end =new Date().getTime();
System.out.println(end-begin);
}
}
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
密碼安全性在線檢測:
http://tools.jb51.net/password/my_password_safe
高強度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword
迅雷、快車、旋風(fēng)URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
基于Spring的Maven項目實現(xiàn)發(fā)送郵件功能的示例
這篇文章主要介紹了基于Spring的Maven項目實現(xiàn)發(fā)送郵件功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Java中String和StringBuffer及StringBuilder?有什么區(qū)別
這篇文章主要介紹了Java中String和StringBuffer及StringBuilder?有什么區(qū)別,String?是?Java?語言非?;A(chǔ)和重要的類,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-06-06
以用戶名注冊為例分析三種Action獲取數(shù)據(jù)的方式
這篇文章主要介紹了以用戶名注冊為例分析三種Action獲取數(shù)據(jù)的方式的相關(guān)資料,需要的朋友可以參考下2016-03-03
使用spring mail發(fā)送html郵件的示例代碼
本篇文章主要介紹了使用spring mail發(fā)送html郵件的示例代碼,這里整理了詳細(xì)的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09
Java連接MySQL數(shù)據(jù)庫并實現(xiàn)數(shù)據(jù)交互功能
在現(xiàn)代應(yīng)用中,數(shù)據(jù)庫是不可或缺的一部分,Java 作為一種廣泛使用的編程語言,提供了豐富的 API 來與各種數(shù)據(jù)庫進行交互,本文將詳細(xì)介紹如何在 Java 中連接 MySQL 數(shù)據(jù)庫,并實現(xiàn)基本的數(shù)據(jù)交互功能,需要的朋友可以參考下2024-10-10
Spring Boot集成Redis實戰(zhàn)操作功能
這篇文章主要介紹了Spring Boot集成Redis實戰(zhàn)操作,包括如何集成redis以及redis的一些優(yōu)點,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-11-11

