3DES算法简介
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。
3DES算法实现
package com.jianggujin.codec;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption
* Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法
*
* @author jianggujin
*
*/
public class HQTripleDES
{
private static HQTripleDES tripleDES = new HQTripleDES();
public static HQTripleDES getInstance()
{
return tripleDES;
}
private HQTripleDES()
{
}
/**
* DES算法
*
* @author jianggujin
*
*/
public static enum HQDESAlgorithm
{
DES("DES"), DESede("DESede");
private String name;
private HQDESAlgorithm(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
/**
* 工作模式
*
* @author jianggujin
*
*/
public static enum HQWorkingMode
{
/**
* 该模式不能使用向量
*/
ECB("ECB"), CBC("CBC"), CFB("CFB"), OFB("OFB"), CTR("CTR");
private String name;
private HQWorkingMode(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
/**
* 填充方式
*
* @author jianggujin
*
*/
public static enum HQPadding
{
NoPadding("NoPadding"), PKCS5Padding("PKCS5Padding");
private String name;
private HQPadding(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
public byte[] encrypt(byte[] data, byte[] key, HQDESAlgorithm algorithm, HQWorkingMode workingMode,
HQPadding padding, byte[] iv)
{
return encrypt(data, key, algorithm.getName(), workingMode.getName(), padding.getName(), iv);
}
public byte[] encrypt(byte[] data, byte[] key, String algorithm, String workingMode, String padding, byte[] iv)
{
return operate(Cipher.ENCRYPT_MODE, data, key, algorithm, workingMode, padding, iv);
}
public byte[] decrypt(byte[] data, byte[] key, HQDESAlgorithm algorithm, HQWorkingMode workingMode,
HQPadding padding, byte[] iv)
{
return decrypt(data, key, algorithm.getName(), workingMode.getName(), padding.getName(), iv);
}
public byte[] decrypt(byte[] data, byte[] key, String algorithm, String workingMode, String padding, byte[] iv)
{
return operate(Cipher.DECRYPT_MODE, data, key, algorithm, workingMode, padding, iv);
}
private byte[] operate(int mode, byte[] data, byte[] key, String algorithm, String workingMode, String padding,
byte[] iv)
{
try
{
String fullAlg = algorithm + "/" + workingMode + "/" + padding;
Cipher cipher = Cipher.getInstance(fullAlg);
SecretKey secretKey = new SecretKeySpec(key, algorithm);
if (!HQWorkingMode.ECB.getName().equalsIgnoreCase(workingMode))
{
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(mode, secretKey, ivSpec);
}
else
{
cipher.init(mode, secretKey);
}
return cipher.doFinal(data);
}
catch (Exception e)
{
throw new IllegalArgumentException(e);
}
}
public byte[] initKey(HQDESAlgorithm algorithm)
{
return initKey(algorithm.getName());
}
public byte[] initKey(String algorithm)
{
try
{
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
return kg.generateKey().getEncoded();
}
catch (Exception e)
{
throw new IllegalArgumentException(e);
}
}
public byte[] initIv(HQDESAlgorithm algorithm, HQWorkingMode workingMode, HQPadding padding)
{
return initIv(algorithm.getName(), workingMode.getName(), padding.getName());
}
/**
* 初始化向量
*
* @param algorithm
* @param workingMode
* @param padding
* @return
*/
public byte[] initIv(String algorithm, String workingMode, String padding)
{
try
{
String fullAlg = algorithm + "/" + workingMode + "/" + padding;
Cipher cipher = Cipher.getInstance(fullAlg);
int blockSize = cipher.getBlockSize();
byte[] iv = new byte[blockSize];
for (int i = 0; i < blockSize; i++)
{
iv[i] = 0;
}
return iv;
}
catch (Exception e)
{
throw new IllegalArgumentException(e);
}
}
}
- 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
- 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
测试代码:
import org.junit.Test;
import com.jianggujin.codec.HQBase64;
import com.jianggujin.codec.HQTripleDES;
import com.jianggujin.codec.HQTripleDES.HQDESAlgorithm;
import com.jianggujin.codec.HQTripleDES.HQPadding;
import com.jianggujin.codec.HQTripleDES.HQWorkingMode;
public class TripleDESTest
{
HQTripleDES tripleDES = HQTripleDES.getInstance();
HQBase64 base64 = HQBase64.getInstance();
@Test
public void encode() throws Exception
{
byte[] data = "jianggujin111111".getBytes();
HQDESAlgorithm[] algorithms = HQDESAlgorithm.values();
for (HQDESAlgorithm algorithm : algorithms)
{
System.err.println("==================================");
System.err.println(algorithm);
byte[] key = tripleDES.initKey(algorithm);
System.err.println("密钥:" + base64.encodeToString(key));
HQWorkingMode[] workingModes = HQWorkingMode.values();
for (HQWorkingMode workingMode : workingModes)
{
HQPadding[] paddings = HQPadding.values();
for (HQPadding padding : paddings)
{
byte[] iv = tripleDES.initIv(algorithm, workingMode, padding);
System.err.println(algorithm + "/" + workingMode + "/" + padding);
byte[] result = tripleDES.encrypt(data, key, algorithm, workingMode, padding, iv);
System.err.println("加密:" + base64.encodeToString(result));
System.err
.println("解密:" + new String(tripleDES.decrypt(result, key, algorithm, workingMode, padding, iv)));
}
}
}
}
}
- 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
- 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
测试结果
==================================
DES
密钥:ofIC5uXZ754=
DES/ECB/NoPadding
加密:qaEHXwEVY9iuFZLTD2bDyA==
解密:jianggujin111111
DES/ECB/PKCS5Padding
加密:qaEHXwEVY9iuFZLTD2bDyOQlcSKjJb+i
解密:jianggujin111111
DES/CBC/NoPadding
加密:qaEHXwEVY9jgG/JKXYLcgw==
解密:jianggujin111111
DES/CBC/PKCS5Padding
加密:qaEHXwEVY9jgG/JKXYLcg3N/cxnimaLm
解密:jianggujin111111
DES/CFB/NoPadding
加密:dsPgiySVucQEVUAtgK0ZyQ==
解密:jianggujin111111
DES/CFB/PKCS5Padding
加密:dsPgiySVucQEVUAtgK0ZyaVTB89CwHpP
解密:jianggujin111111
DES/OFB/NoPadding
加密:dsPgiySVucTWkKy2vR2yiQ==
解密:jianggujin111111
DES/OFB/PKCS5Padding
加密:dsPgiySVucTWkKy2vR2yiQBec6XLe53i
解密:jianggujin111111
DES/CTR/NoPadding
加密:dsPgiySVucSH/JuiQdPsTw==
解密:jianggujin111111
DES/CTR/PKCS5Padding
加密:dsPgiySVucSH/JuiQdPsTw==
解密:jianggujin111111
==================================
DESede
密钥:wrXx75tblFffNzjsnq7aYWsCa6Hqzvhd
DESede/ECB/NoPadding
加密:MrKciDR8RABzp56s4JcC9w==
解密:jianggujin111111
DESede/ECB/PKCS5Padding
加密:MrKciDR8RABzp56s4JcC98Lz4tvLaEjk
解密:jianggujin111111
DESede/CBC/NoPadding
加密:MrKciDR8RACeMsUkhLbNrw==
解密:jianggujin111111
DESede/CBC/PKCS5Padding
加密:MrKciDR8RACeMsUkhLbNryFRHLVNiiNU
解密:jianggujin111111
DESede/CFB/NoPadding
加密:RnEVDpqPPEc6CTyUfmPFJQ==
解密:jianggujin111111
DESede/CFB/PKCS5Padding
加密:RnEVDpqPPEc6CTyUfmPFJUYfDNnsJTd2
解密:jianggujin111111
DESede/OFB/NoPadding
加密:RnEVDpqPPEd7Qlc9kgBgBQ==
解密:jianggujin111111
DESede/OFB/PKCS5Padding
加密:RnEVDpqPPEd7Qlc9kgBgBXDUv7EOlzgi
解密:jianggujin111111
DESede/CTR/NoPadding
加密:RnEVDpqPPEdLUDlgMH+QWQ==
解密:jianggujin111111
DESede/CTR/PKCS5Padding
加密:RnEVDpqPPEdLUDlgMH+QWQ==
解密:jianggujin111111