文章16 | 阅读 7008 | 点赞0
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。
package com.jianggujin.codec;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.jianggujin.codec.util.JCipherInputStream;
import com.jianggujin.codec.util.JCipherOutputStream;
import com.jianggujin.codec.util.JCodecException;
/** * 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption * Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法 * * @author jianggujin * */
public class JTripleDES {
/** * DES算法 * * @author jianggujin * */
public static enum JDESAlgorithm {
DES, DESede;
public String getName() {
return this.name();
}
}
/** * 工作模式 * * @author jianggujin * */
public static enum JWorkingMode {
/** * 该模式不能使用向量 */
ECB, CBC, CFB, OFB, CTR;
public String getName() {
return this.name();
}
}
/** * 填充方式 * * @author jianggujin * */
public static enum JPadding {
NoPadding, PKCS5Padding;
public String getName() {
return this.name();
}
}
/** * 初始化密钥 * * @param algorithm * @return * @throws Exception */
public static byte[] initKey(String algorithm) {
try {
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
return kg.generateKey().getEncoded();
} catch (Exception e) {
throw new JCodecException(e);
}
}
/** * 初始化向量 * * @param algorithm * @param workingMode * @param padding * @return */
public static 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 JCodecException(e);
}
}
/** * 加密 * * @param data * @param key * @param algorithm * @param workingMode * @param padding * @return */
public static byte[] encrypt(byte[] data, byte[] key, String algorithm, String workingMode, String padding) {
return encrypt(data, key, algorithm, workingMode, padding, null);
}
/** * 加密 * * @param data * @param key * @param algorithm * @param workingMode * @param padding * @param iv * 为null时则使用默认向量 * @return */
public static byte[] encrypt(byte[] data, byte[] key, String algorithm, String workingMode, String padding,
byte[] iv) {
// 数据加密
Cipher cipher = getEncryptCipher(key, algorithm, workingMode, padding, iv);
try {
return cipher.doFinal(data);
} catch (Exception e) {
throw new JCodecException(e);
}
}
/** * 包裹输出流,包裹后的输出流为加密输出流 * * @param out * @param key * @param algorithm * @param workingMode * @param padding * @return */
public static OutputStream wrap(OutputStream out, byte[] key, String algorithm, String workingMode, String padding) {
return wrap(out, key, algorithm, workingMode, padding, null);
}
/** * 包裹输出流,包裹后的输出流为加密输出流 * * @param out * @param key * @param algorithm * @param workingMode * @param padding * @param iv * 为null时则使用默认向量 * @return */
public static OutputStream wrap(OutputStream out, byte[] key, String algorithm, String workingMode, String padding,
byte[] iv) {
// 数据加密
Cipher cipher = getEncryptCipher(key, algorithm, workingMode, padding, iv);
return new JCipherOutputStream(cipher, out);
}
/** * 获得加密模式的{@link Cipher} * * @param key * @param algorithm * @param workingMode * @param padding * @return */
public static Cipher getEncryptCipher(byte[] key, String algorithm, String workingMode, String padding) {
return getEncryptCipher(key, algorithm, workingMode, padding, null);
}
/** * 获得加密模式的{@link Cipher} * * @param key * @param algorithm * @param workingMode * @param padding * @param iv * 为null时则使用默认向量 * @return */
public static Cipher getEncryptCipher(byte[] key, String algorithm, String workingMode, String padding, byte[] iv) {
return getCipher(key, algorithm, workingMode, padding, iv, Cipher.ENCRYPT_MODE);
}
/** * 解密 * * @param data * @param key * @param algorithm * @param workingMode * @param padding * @return */
public static byte[] decrypt(byte[] data, byte[] key, String algorithm, String workingMode, String padding) {
return decrypt(data, key, algorithm, workingMode, padding, null);
}
/** * 解密 * * @param data * @param key * @param algorithm * @param workingMode * @param padding * @param iv * 为null时则使用默认向量 * @return */
public static byte[] decrypt(byte[] data, byte[] key, String algorithm, String workingMode, String padding,
byte[] iv) {
// 数据解密
Cipher cipher = getDecryptCipher(key, algorithm, workingMode, padding, iv);
try {
return cipher.doFinal(data);
} catch (Exception e) {
throw new JCodecException(e);
}
}
/** * 包裹输入流,原输入流为加密数据输入流 * * @param in * @param key * @param algorithm * @param workingMode * @param padding * @return */
public static InputStream wrap(InputStream in, byte[] key, String algorithm, String workingMode, String padding) {
return wrap(in, key, algorithm, workingMode, padding, null);
}
/** * 包裹输入流,原输入流为加密数据输入流 * * @param in * @param key * @param algorithm * @param workingMode * @param padding * @param iv * 为null时则使用默认向量 * @return */
public static InputStream wrap(InputStream in, byte[] key, String algorithm, String workingMode, String padding,
byte[] iv) {
// 数据解密
Cipher cipher = getDecryptCipher(key, algorithm, workingMode, padding, iv);
return new JCipherInputStream(cipher, in);
}
/** * 获得解密模式的{@link Cipher} * * @param key * @param algorithm * @param workingMode * @param padding * @return */
public static Cipher getDecryptCipher(byte[] key, String algorithm, String workingMode, String padding) {
return getDecryptCipher(key, algorithm, workingMode, padding, null);
}
/** * 获得解密模式的{@link Cipher} * * @param key * @param algorithm * @param workingMode * @param padding * @param iv * 为null时则使用默认向量 * @return */
public static Cipher getDecryptCipher(byte[] key, String algorithm, String workingMode, String padding, byte[] iv) {
return getCipher(key, algorithm, workingMode, padding, iv, Cipher.DECRYPT_MODE);
}
/** * 获得{@link Cipher} * * @param key * @param algorithm * @param workingMode * @param padding * @param iv * 为null时则使用默认向量 * @param opmode * @return */
private static Cipher getCipher(byte[] key, String algorithm, String workingMode, String padding, byte[] iv,
int opmode) {
// 生成本地密钥
SecretKey secretKey = getSecretKey(key, algorithm);
try {
String fullAlg = algorithm + "/" + workingMode + "/" + padding;
Cipher cipher = Cipher.getInstance(fullAlg);
if (!JWorkingMode.ECB.getName().equalsIgnoreCase(workingMode)) {
if (iv == null) {
int blockSize = cipher.getBlockSize();
iv = new byte[blockSize];
for (int i = 0; i < blockSize; i++) {
iv[i] = 0;
}
}
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(opmode, secretKey, ivSpec);
} else {
cipher.init(opmode, secretKey);
}
return cipher;
} catch (Exception e) {
throw new JCodecException(e);
}
}
/** * 获得密钥 * * @param key * @param algorithm * @return */
private static SecretKey getSecretKey(byte[] key, String algorithm) {
try {
SecretKey secretKey = new SecretKeySpec(key, algorithm);
return secretKey;
} catch (Exception e) {
throw new JCodecException(e);
}
}
}
测试代码:
package com.jianggujin.codec.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.junit.Test;
import com.jianggujin.codec.JBase64;
import com.jianggujin.codec.JBase64.JEncoder;
import com.jianggujin.codec.JTripleDES;
import com.jianggujin.codec.JTripleDES.JDESAlgorithm;
import com.jianggujin.codec.JTripleDES.JPadding;
import com.jianggujin.codec.JTripleDES.JWorkingMode;
public class TripleDESTest {
String str = "jianggujin111111";
File file = new File(getClass().getSimpleName() + ".dat");
@Test
public void test() throws Exception {
System.out.println("原串:" + str);
JEncoder encoder = JBase64.getEncoder();
for (JDESAlgorithm algorithm : JDESAlgorithm.values()) {
System.out.println("算法:" + algorithm.getName());
byte[] key = JTripleDES.initKey(algorithm.getName());
System.out.println("密钥:" + encoder.encodeToString(key, "UTF-8"));
for (JWorkingMode workingMode : JWorkingMode.values()) {
for (JPadding padding : JPadding.values()) {
System.out.println("-----------------------------------------");
System.out.println(algorithm + "/" + workingMode + "/" + padding);
byte[] encrypt = JTripleDES.encrypt(str.getBytes(), key, algorithm.getName(), workingMode.getName(),
padding.getName());
System.out.println("加密:" + encoder.encodeToString(encrypt, "UTF-8"));
System.out.println("解密:" + new String(
JTripleDES.decrypt(encrypt, key, algorithm.getName(), workingMode.getName(), padding.getName())));
System.out.print("输出流加密:" + file.getAbsolutePath());
OutputStream out = JTripleDES.wrap(new FileOutputStream(file), key, algorithm.getName(),
workingMode.getName(), padding.getName());
out.write(str.getBytes());
out.flush();
out.close();
System.out.println();
System.out.print("输入流解密:");
InputStream in = JTripleDES.wrap(new FileInputStream(file), key, algorithm.getName(),
workingMode.getName(), padding.getName());
byte[] buffer = new byte[1024];
int len = in.read(buffer);
System.out.println(new String(buffer, 0, len));
}
}
}
}
}
测试结果
原串:jianggujin111111
算法:DES
密钥:STgOq5j+W2I=
—————————————–
DES/ECB/NoPadding
加密:xReaksflVhoskcgCSbx32g==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/ECB/PKCS5Padding
加密:xReaksflVhoskcgCSbx32nAMw70j5Q4d
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/CBC/NoPadding
加密:xReaksflVhoAZm1EazC1pA==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/CBC/PKCS5Padding
加密:xReaksflVhoAZm1EazC1pABSl+6raOnY
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/CFB/NoPadding
加密:lv3gnIv2ARWE+EURnN8qBQ==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/CFB/PKCS5Padding
加密:lv3gnIv2ARWE+EURnN8qBeyGlXXo79Vj
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/OFB/NoPadding
加密:lv3gnIv2ARWdOf4IFFr2RA==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/OFB/PKCS5Padding
加密:lv3gnIv2ARWdOf4IFFr2REpbraYN8vK1
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/CTR/NoPadding
加密:lv3gnIv2ARX96lBo58xf8A==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DES/CTR/PKCS5Padding
加密:lv3gnIv2ARX96lBo58xf8A==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
算法:DESede
密钥:+MQqI5g+c2GJbdOnTFRUkQ2zdgj96hX4
—————————————–
DESede/ECB/NoPadding
加密:4DfUS0dkn0Yf1NIThNPzRA==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/ECB/PKCS5Padding
加密:4DfUS0dkn0Yf1NIThNPzRG7eKaTs2NC9
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/CBC/NoPadding
加密:4DfUS0dkn0Y5mctZQQs4lg==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/CBC/PKCS5Padding
加密:4DfUS0dkn0Y5mctZQQs4liPGg8/jDzsk
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/CFB/NoPadding
加密:2huLYOt44ucXsUXX2fQuqw==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/CFB/PKCS5Padding
加密:2huLYOt44ucXsUXX2fQuq85yWpJ1qxaq
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/OFB/NoPadding
加密:2huLYOt44uedB3fPCBGGmA==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/OFB/PKCS5Padding
加密:2huLYOt44uedB3fPCBGGmBmF7G+qCWDe
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/CTR/NoPadding
加密:2huLYOt44ucNRy7fPNJGmA==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
—————————————–
DESede/CTR/PKCS5Padding
加密:2huLYOt44ucNRy7fPNJGmA==
解密:jianggujin111111
输出流加密:F:\workspace\java\eclipse\JCodec\TripleDESTest.dat
输入流解密:jianggujin111111
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/jianggujin/article/details/50536949
内容来源于网络,如有侵权,请联系作者删除!