当我使用 JAVA 进行一些 3DES-CBC 编程时,我遇到了一些问题。
我有一些功能
private static byte[] xorBytes(byte[] rndA, byte[] rndB) {
// TODO Auto-generated method stub
byte[] b = new byte[rndB.length];
for (int i = 0; i < rndB.length; i++) {
b[i] = (byte) (rndA[i] ^ rndB[i]);
}
return b;
}
public static byte[] decrypt(byte[] key, byte[] enciphered_data) {
try {
byte[] iv = new byte[] { 0,0,0,0,0,0,0,0 };
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
SecretKey s = new SecretKeySpec(key, "DESede");
Cipher cipher;
cipher = Cipher.getInstance("DESede/CBC/NoPadding", "BC");
cipher.init(Cipher.DECRYPT_MODE, s, ivParameterSpec);
byte[] deciphered_data = cipher.doFinal(enciphered_data);
return deciphered_data;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
和一些变量
byte[] key = new byte[] { (byte) 0x0, (byte) 0x0, (byte) 0x0,
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
(byte) 0x0, (byte) 0x0, (byte) 0x0 };
byte[] rndB = new byte[] { (byte) 0xe4, (byte) 0xee, (byte) 0x2e,
(byte) 0x8b, (byte) 0x4b, (byte) 0xf7, (byte) 0xb1, (byte) 0x98 };
byte[] rndA = new byte[] { (byte) 0x00, (byte) 0x11, (byte) 0x22,
(byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66, (byte) 0x77 };
首先我做
rndA = decrypt(key, rndA);
byte[] rndAB = new byte[16];
System.arraycopy(rndA, 0, rndAB, 0, 8);
rndB = xorBytes(rndA, rndB);
rndB = decrypt(key, rndB);
System.arraycopy(rndB, 0, rndAB, 8, 8);
结果是
rndAB = 74 f4 ae 77 7a a4 31 e8 4b 18 ba 8f 74 cf 80 63
然后我做
byte[] rndAB = new byte[16];
System.arraycopy(rndA, 0, rndAB, 0, 8);
System.arraycopy(rndB, 0, rndAB, 8, 8);
rndAB = decrypt(key, rndAB);
但结果是 rndAB = 74 f4 ae 77 7a a4 31 e8 a6 54 dc e3 27 01 2b 39
我很困惑为什么这两个结果不同。DES(A+B) 是否等于 DES(A) + DES(DES(A) xor B) 且 IV = {0,0,0,0,0,0,0,0} ???