62,616
社区成员
发帖
与我相关
我的任务
分享
import java.security.Key;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
public class BcTest {
// 3DES key = 00 01 02 03 04 05 06 07 01 02 03 04 05 06 07 08 02 03 04 05 06 07 08 09
// data ("01234567") = 30 31 32 33 34 35 36 37
// 3des cipher = 5a b3 fd 7b 2a ca b2 95
// cipher mode = DESede/ECB/NoPadding
public static void main(String[] args) throws Exception {
byte[] key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7,
1, 2, 3, 4, 5, 6, 7, 8,
2, 3, 4, 5, 6, 7, 8, 9
};
byte[] data = "01234567".getBytes();
byte[] crypt = encrypt(data, key); // 3DES 加密
byte[] plain = decrypt(crypt, key); // 3DES 解密
System.out.println(" key: " + ByteUtil.bytes2HexSpace(key));
System.out.println(" data: " + ByteUtil.bytes2HexSpace(data));
System.out.println("crypt: " + ByteUtil.bytes2HexSpace(crypt));
System.out.println("plain: " + ByteUtil.bytes2HexSpace(plain));
}
public static byte[] decrypt(byte[] crypt, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(crypt);
}
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
public static SecretKey toKey(byte[] key) throws Exception {
KeySpec dks = new DESedeKeySpec(key); // 这里改为了 DESedeKeySpec
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
return keyFactory.generateSecret(dks);
}
}
import java.security.Key;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class BcTest {
// 3DES key = 00 01 02 03 04 05 06 07 01 02 03 04 05 06 07 08 02 03 04 05 06 07 08 09
// data ("01234567") = 30 31 32 33 34 35 36 37
// 3des cipher = 5a b3 fd 7b 2a ca b2 95
// cipher mode = DESede/ECB/NoPadding
public static void main(String[] args) throws Exception {
byte[] key1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
byte[] key2 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] key3 = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] data = "01234567".getBytes(); // 由于未采用填充模式,因此原文长度必须为 8 的倍数
// 3DES ciphertext = EK3(DK2(EK1(plaintext)))
byte[] crypt = encrypt(decrypt(encrypt(data, key1), key2), key3);
// 3DES plaintext = DK1(EK2(DK3(ciphertext)))
byte[] plain = decrypt(encrypt(decrypt(crypt, key3), key2), key1);
System.out.println(" key: " + ByteUtil.bytes2HexSpace(key1) + " "
+ ByteUtil.bytes2HexSpace(key2) + " "
+ ByteUtil.bytes2HexSpace(key3));
System.out.println(" data: " + ByteUtil.bytes2HexSpace(data));
System.out.println("crypt: " + ByteUtil.bytes2HexSpace(crypt));
System.out.println("plain: " + ByteUtil.bytes2HexSpace(plain));
}
public static byte[] decrypt(byte[] crypt, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(crypt);
}
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
public static SecretKey toKey(byte[] key) throws Exception {
KeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
return keyFactory.generateSecret(dks);
}
}
class ByteUtil {
private static final char HEX[] = "0123456789abcdef".toCharArray();
public static String bytes2HexSpace(byte bys[]) {
char chs[] = new char[(bys.length * 2 + bys.length) - 1];
int i = 0;
int offset = 0;
for (; i < bys.length; i++) {
if (i > 0)
chs[offset++] = ' ';
chs[offset++] = HEX[bys[i] >> 4 & 15];
chs[offset++] = HEX[bys[i] & 15];
}
return new String(chs);
}
}