我从字节数组(“skBytes”)创建一个三重DES密钥,但是当在三重DES密钥(“sk”)上调用getEncoded并将其与字节数组进行比较时,它们会有所不同!但是,如果你看一下控制台输出,它们几乎是一样的。我如何创建一个完全像“skBytes”的三重DES密钥?
byte[] skBytes = {(byte) 0x41, (byte) 0x0B, (byte) 0xF0, (byte) 0x9B, (byte) 0xBC, (byte) 0x0E, (byte) 0xC9, (byte) 0x4A, (byte) 0xB5, (byte) 0xCE, (byte) 0x0B, (byte) 0xEA, (byte) 0x05, (byte) 0xEF, (byte) 0x52, (byte) 0x31, (byte) 0xD7, (byte) 0xEC, (byte) 0x2E, (byte) 0x75, (byte) 0xC3, (byte) 0x1D, (byte) 0x3E, (byte) 0x61};
DESedeKeySpec keySpec = new DESedeKeySpec(skBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey sk = keyFactory.generateSecret(keySpec);
for(int i = 0; i < skBytes.length; i++)
{
System.out.println("(sk.getEncoded()[i], skBytes[i]) = (" + sk.getEncoded()[i] +", " + skBytes[i] + ")");
}
控制台输出:
(sk.getEncoded()[i], skBytes[i]) = (64, 65)
(sk.getEncoded()[i], skBytes[i]) = (11, 11)
(sk.getEncoded()[i], skBytes[i]) = (-15, -16)
(sk.getEncoded()[i], skBytes[i]) = (-101, -101)
(sk.getEncoded()[i], skBytes[i]) = (-68, -68)
(sk.getEncoded()[i], skBytes[i]) = (14, 14)
(sk.getEncoded()[i], skBytes[i]) = (-56, -55)
(sk.getEncoded()[i], skBytes[i]) = (74, 74)
(sk.getEncoded()[i], skBytes[i]) = (-75, -75)
(sk.getEncoded()[i], skBytes[i]) = (-50, -50)
(sk.getEncoded()[i], skBytes[i]) = (11, 11)
(sk.getEncoded()[i], skBytes[i]) = (-22, -22)
(sk.getEncoded()[i], skBytes[i]) = (4, 5)
(sk.getEncoded()[i], skBytes[i]) = (-17, -17)
(sk.getEncoded()[i], skBytes[i]) = (82, 82)
(sk.getEncoded()[i], skBytes[i]) = (49, 49)
(sk.getEncoded()[i], skBytes[i]) = (-42, -41)
(sk.getEncoded()[i], skBytes[i]) = (-20, -20)
(sk.getEncoded()[i], skBytes[i]) = (47, 46)
(sk.getEncoded()[i], skBytes[i]) = (117, 117)
(sk.getEncoded()[i], skBytes[i]) = (-62, -61)
(sk.getEncoded()[i], skBytes[i]) = (28, 29)
(sk.getEncoded()[i], skBytes[i]) = (62, 62)
(sk.getEncoded()[i], skBytes[i]) = (97, 97)
答案 0 :(得分:5)
DES密钥为56位,以8个字节表示。每个字节携带一个奇偶校验位。三重DES密钥只是连接在一起的3个DES密钥。
使用DESedeKeySpec制作密钥时,它会为您调整奇偶校验。因此,您必须提供具有适当奇偶校验的字节以获得相同的编码值。