本文实例讲述了java基于Des对称加密算法实现的加密与解密功能。分享给大家供大家参考,具体如下:
Des 加密相关类介绍:
SecureRandom 这个类是继承自java.util.Random 这个类
SecureRandom 这个类的构造器有三种,下面例举两种:
SecureRandom()构造一个实现默认随机数算法的安全随机数生成器 (RNG)。
SecureRandom(byte[] seed)构造一个实现默认随机数算法的安全随机数生成器 (RNG)。
DESKeySpec 这个类是用来使用原始秘钥来生成秘钥的秘钥内容
DESKeySpec 有两个构造函数:
DESKeySpec(byte[] key) 创建一个 DESKeySpec 对象,使用 key 中的前 8 个字节作为 DES 密钥的密钥内容。
DESKeySpec(byte[] key, int offset) 创建一个 DESKeySpec 对象,使用 key 中始于且包含 offset 的前 8 个字节作为 DES-EDE 密钥的密钥内容。
SecretKeyFactory , 密钥工厂用来将密钥(类型 Key 的不透明加密密钥)转换为密钥规范(底层密钥材料的透明表示形式),反之亦然。秘密密钥工厂只对秘密(对称)密钥进行操作。
SecretKey对象,秘钥对象,通过调用秘钥工厂的generateSecret(DESKeySpec deskeyspace) 方法来生成秘钥
Cipher 类为加密和解密提供密码功能,通过调用Cipher的getInstance("des") 来获取实例
Cipher 对象调用init() 方法进行对象的初始化,init() 方法的具体参数按照具体情况而定,有加密的也有解密的常量
最后调用Cipher的doFinal() 方法进行加密解密。
在这里请教大家一个问题,不管是第一种使用BASE64Encoder编码还是第二种org.apache.commons.codec.binary.Base64编码,在将String 转化为byte以及将byte转化为String 时需要 UTF-8/GBK 等编码来编码,解码吗?
一、使用了 sun.misc.BASE64Decoder 和BASE64Encoder 进行解码,编码
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
 
								48
 
								49
 
								50
 
								51
 
								52
 
								53
 
								54
 
								55
 
								56
 
								57
 
								58
 
								59
 
								60
 
								61
 
								62
 
								63
 
								64
 
								65
 
								66
 
								67
 
								68
 
								69
 
								70
 
								71
 
								72
 
								73
 
								74
 
								75
 
								76
 
								77
 
								78
 
								79
 
								80
 
								81
 
								82
 
								83
 
								84
 
								85
 
								86
 
								87
 
								88
 
								89
 
								90
 
								91
 
								92
 
								93
 
								94
 
								95
 
								96
 
								97
 
								98
 
								99
 
								100
 
								101
 
								102
 
								103
 
								104
 
								105
 
								106
 
								107
 
								108
 
								109
 
								110
 
								111
 
								112
 
								113
 
								114
 
								115
 
								116
 
								117
 
								118
 
								119
 
								120
 
								121
 
								122
						  | 
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;
// 导入sun的64位编码
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*@author WHD
*
*即使导入sun.misc这个架包也会报错,这时首先把你的JRE架包移除再导入一次就可以了
*/
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的倍数
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 根据键值进行加密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
public static String encrypt(String data, String key) throws Exception {
// 使用指定的编码获取要加密的内容,一般秘钥都是字母或数字不用指定编码,但指定也可以
byte[] bt = encrypt(data.getBytes(UTF8), key.getBytes(UTF8));
//注意:在加密和解密的时候使用sun的BASE64Encoder()进行编码和解码不然会有乱码
//网上查看了很多实例,都没有编码和解码,也说没有乱码问题,而我这里出现了乱码,所以使用BASE64Encoder()进行了编码解码
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* Description 根据键值进行解密
* @param data
* @param key 加密键byte数组
* @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 根据键值进行加密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象,也就是创建秘钥的秘钥内容
DESKeySpec dks = new DESKeySpec(key);
// 密钥工厂用来将密钥(类型 Key 的不透明加密密钥)转换为密钥规范(底层密钥材料的透明表示形式),反之亦然。秘密密钥工厂只对秘密(对称)密钥进行操作。
// 这里改为使用单例模式
//SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//根据提供的密钥规范(密钥材料)生成 SecretKey(秘钥) 对象。
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作,此类为加密和解密提供密码功能
Cipher cipher = Cipher.getInstance(DES);
// 用密钥和随机源初始化此 Cipher。ENCRYPT_MODE用于将 Cipher 初始化为加密模式的常量。
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
//正式执行加密操作
return cipher.doFinal(data);
}
/**
* Description 根据键值进行解密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象,也就是创建秘钥的秘钥内容
DESKeySpec dks = new DESKeySpec(key);
// 密钥工厂用来将密钥(类型 Key 的不透明加密密钥)转换为密钥规范(底层密钥材料的透明表示形式),反之亦然。秘密密钥工厂只对秘密(对称)密钥进行操作。
// 这里改为使用单例模式
//SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//根据提供的密钥规范(密钥材料)生成 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 进行解码,编码
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
 
								48
 
								49
 
								50
 
								51
 
								52
 
								53
 
								54
 
								55
 
								56
 
								57
 
								58
 
								59
 
								60
 
								61
 
								62
 
								63
 
								64
 
								65
 
								66
 
								67
 
								68
 
								69
 
								70
 
								71
 
								72
 
								73
 
								74
 
								75
 
								76
 
								77
 
								78
 
								79
 
								80
 
								81
 
								82
 
								83
 
								84
 
								85
 
								86
 
								87
 
								88
 
								89
 
								90
 
								91
 
								92
 
								93
 
								94
 
								95
 
								96
 
								97
 
								98
 
								99
 
								100
 
								101
 
								102
 
								103
 
								104
 
								105
 
								106
 
								107
 
								108
 
								109
 
								110
 
								111
 
								112
 
								113
 
								114
 
								115
 
								116
 
								117
						  | 
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的倍数
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 根据键值进行加密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
public static String encrypt(String data, String key) throws Exception {
// 使用指定的编码获取要加密的内容,一般秘钥都是字母或数字不用指定编码,但指定也可以
byte[] bt = encrypt(data.getBytes(UTF8), key.getBytes());
// 第一个使用了sun.misc.BASE64Encoder;进行了编码,但网上说使用org.apache.commons.codec.binary.Base64比较好所以拿来试试
String strs = Base64.encodeBase64String(bt);
return strs;
}
/**
* Description 根据键值进行解密
* @param data
* @param key 加密键byte数组
* @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 根据键值进行加密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象,也就是创建秘钥的秘钥内容
DESKeySpec dks = new DESKeySpec(key);
// 密钥工厂用来将密钥(类型 Key 的不透明加密密钥)转换为密钥规范(底层密钥材料的透明表示形式),反之亦然。秘密密钥工厂只对秘密(对称)密钥进行操作。
// 这里改为使用单例模式
//SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//根据提供的密钥规范(密钥材料)生成 SecretKey(秘钥) 对象。
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作,此类为加密和解密提供密码功能
Cipher cipher = Cipher.getInstance(DES);
// 用密钥和随机源初始化此 Cipher。ENCRYPT_MODE用于将 Cipher 初始化为加密模式的常量。
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
//正式执行加密操作
return cipher.doFinal(data);
}
/**
* Description 根据键值进行解密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象,也就是创建秘钥的秘钥内容
DESKeySpec dks = new DESKeySpec(key);
// 密钥工厂用来将密钥(类型 Key 的不透明加密密钥)转换为密钥规范(底层密钥材料的透明表示形式),反之亦然。秘密密钥工厂只对秘密(对称)密钥进行操作。
// 这里改为使用单例模式
//SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//根据提供的密钥规范(密钥材料)生成 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编码、解码器 。
三、未使用任何编码,解码架包
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
 
								48
 
								49
 
								50
 
								51
 
								52
 
								53
 
								54
 
								55
 
								56
 
								57
 
								58
 
								59
 
								60
 
								61
 
								62
 
								63
 
								64
 
								65
 
								66
 
								67
 
								68
 
								69
 
								70
 
								71
 
								72
 
								73
 
								74
 
								75
 
								76
 
								77
 
								78
 
								79
 
								80
 
								81
 
								82
 
								83
 
								84
 
								85
 
								86
 
								87
 
								88
 
								89
 
								90
 
								91
 
								92
 
								93
 
								94
 
								95
 
								96
 
								97
 
								98
 
								99
 
								100
 
								101
 
								102
 
								103
 
								104
 
								105
 
								106
 
								107
 
								108
 
								109
 
								110
 
								111
 
								112
 
								113
 
								114
 
								115
 
								116
 
								117
 
								118
 
								119
 
								120
 
								121
 
								122
 
								123
 
								124
 
								125
 
								126
 
								127
 
								128
 
								129
 
								130
 
								131
 
								132
 
								133
 
								134
 
								135
 
								136
 
								137
 
								138
 
								139
 
								140
 
								141
 
								142
 
								143
 
								144
 
								145
 
								146
 
								147
 
								148
 
								149
 
								150
 
								151
 
								152
 
								153
 
								154
 
								155
 
								156
 
								157
 
								158
 
								159
 
								160
 
								161
 
								162
 
								163
 
								164
 
								165
 
								166
 
								167
 
								168
 
								169
 
								170
 
								171
 
								172
 
								173
 
								174
 
								175
 
								176
 
								177
 
								178
 
								179
 
								180
 
								181
 
								182
 
								183
 
								184
 
								185
 
								186
 
								187
 
								188
 
								189
 
								190
 
								191
 
								192
 
								193
 
								194
 
								195
 
								196
 
								197
 
								198
 
								199
 
								200
 
								201
 
								202
 
								203
						  | 
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 {
// 在静态代码块中获取秘钥工程
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) {
// 字符串转化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);
}
}
 | 
希望本文所述对大家java程序设计有所帮助。
相关文章
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
 - 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
 - ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
 - ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
 - 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
 
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
 - 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
 - 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
 - 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
 - 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
 
快网idc优惠网
QQ交流群
- 
            
Sublime Text 3 实现C语言代码的编译和运行(示例讲解)
2025-05-27 65 - 
            
【ASP.NET Core】在node.js上托管Blazor WebAssembly应用
2025-05-29 22 - 
            2025-06-04 25
 - 
            
IOS 使用Block二次封装AFNetworking 3.0详解
2025-05-29 33 - 
            2025-06-04 40
 
        
    		
            	
        
        