首先,我不是密碼學方面的專家,所以請採取一些適中的答案。
什麼是double length 3DES?
三重DES加密涉及具有雙長(16字節)的密鑰K =(K 大號一個 8字節密文塊加密的8字節明文塊||ķř )如下:
Y = DES3(K)[X] = DES(K 大號)[DES -1(K ř)[DES(K 大號)[X]]]
解密發生如下:
X = DES -1(K 大號)[DES(K ř)[DES -1(K 大號)[Y]]]
EMV 4.2: Book 2 - Security and Key Management從兩者。
這裏是一個將如何在Java中實現這一點:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Test3DES {
public static void main(String[] args) throws Exception {
//byte length has to be mutiple of 8!
String plaintext = "Attack at dawn!!";
byte[] plainBytes = plaintext.getBytes("UTF-8");
byte[] encrypted = encrypt(plainBytes);
byte[] decrypted = decrypt(encrypted);
System.out.println("Original message: ");
System.out.printf("Text: %s%n", plaintext);
System.out.printf("Raw bytes: %s%n", toHexString(plainBytes));
System.out.println("---");
System.out.println("Encrypted message: ");
System.out.printf("Text: %s%n", new String(encrypted, "UTF-8"));
System.out.printf("Raw bytes: %s%n", toHexString(encrypted));
System.out.println("---");
System.out.println("Decrypted message: ");
System.out.printf("Text: %s%n", new String(decrypted, "UTF-8"));
System.out.printf("Raw bytes: %s%n", toHexString(decrypted));
}
private static String toHexString(byte[] array) {
StringBuilder sb = new StringBuilder();
for (byte b : array) {
sb.append(String.format("%02X ", b));
}
return sb.toString();
}
private static byte[] encrypt(byte[] message) throws Exception {
Cipher encr1, decr2, encr3;
SecretKeySpec kL, kR, tmp;
kL = new SecretKeySpec(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}, "DES");
kR = new SecretKeySpec(new byte[] {8, 9, 10, 11, 12, 13, 14, 15}, "DES");
encr1 = Cipher.getInstance("DES/ECB/NoPadding");
decr2 = Cipher.getInstance("DES/ECB/NoPadding");
encr3 = Cipher.getInstance("DES/ECB/NoPadding");
encr1.init(Cipher.ENCRYPT_MODE, kL);
decr2.init(Cipher.DECRYPT_MODE, kR);
encr3.init(Cipher.ENCRYPT_MODE, kL);
return encr3.doFinal(decr2.doFinal(encr1.doFinal(message)));
}
private static byte[] decrypt(byte[] message) throws Exception {
Cipher decr1, encr2, decr3;
SecretKeySpec kL, kR;
kL = new SecretKeySpec(
new byte[] {0, 1, 2, 3, 4, 5, 6, 7},
"DES"
);
kR = new SecretKeySpec(
new byte[] {8, 9, 10, 11, 12, 13, 14, 15},
"DES"
);
decr1 = Cipher.getInstance("DES/ECB/NoPadding");
encr2 = Cipher.getInstance("DES/ECB/NoPadding");
decr3 = Cipher.getInstance("DES/ECB/NoPadding");
decr1.init(Cipher.DECRYPT_MODE, kL);
encr2.init(Cipher.ENCRYPT_MODE, kR);
decr3.init(Cipher.DECRYPT_MODE, kL);
return decr3.doFinal(encr2.doFinal(decr1.doFinal(message)));
}
}
輸出:
Original message:
Text: Attack at dawn!!
Raw bytes: 41 74 74 61 63 6B 20 61 74 20 64 61 77 6E 21 21
---
Encrypted message:
Text: #�Jɚe�P�ϸ5�%t�
Raw bytes: 23 B4 4A C9 9A 65 C5 50 90 CF B8 35 9A 25 74 A2
---
Decrypted message:
Text: Attack at dawn!!
Raw bytes: 41 74 74 61 63 6B 20 61 74 20 64 61 77 6E 21 21
因此,要回答你的問題 - 你正在尋找代碼中16字節的密鑰大小。在我的代碼片段中,密鑰由kL
和kR
組成,每個8字節,共16個。
您如何期望在沒有大量加密知識背景的情況下查看加密代碼? – zaph
這可能是過時的文檔,或者他們根本不知道他們在說什麼。無論如何,現在不應該使用DES和雙密鑰3DES,因爲它們只提供56位和80位的安全性。我建議使用AES,它具有比任何DES/3DES變體最小的密鑰尺寸更好的安全保證。 –