2010-04-21 80 views
0

我從字節數組(「skBytes」)創建三重DES密鑰,但在三重DES密鑰(「sk」)上調用getEncoded並將其與字節數組進行比較時,它們不同!但是,如果您查看控制檯輸出,它們幾乎是一樣的。我將如何創建與「skBytes」完全相同的三重DES密鑰?從字節創建3DES密鑰

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) 
+1

奇偶校正調整... – AOO 2010-04-21 08:46:12

回答

5

DES鍵是56位,它的8個字節表示。每個字節攜帶一個奇偶校驗位。三重DES密鑰只是3個DES密鑰串聯在一起。

當您使用DESedeKeySpec製作鍵時,它會爲您調整奇偶校驗。所以你必須提供具有適當奇偶校驗的字節以獲得相同的編碼值。