2015-06-13 106 views
1

我試圖使用ISO9797 Alghrythm 3生成一個MAC。 我在Clojure中這樣做,但我想我有更多的Java問題這裏。我運行此代碼:Java/Clojure BouncyCastle報告錯誤的密鑰大小,但密鑰大小正確

(defn mac2 [key message] 
    (let [engine (org.bouncycastle.crypto.engines.DESedeEngine.) 
     mac (org.bouncycastle.crypto.macs.ISO9797Alg3Mac. engine) 
     bytes (byte-array (.getMacSize mac)) 
     key (->bytes key) 
     msg (->bytes E-IFD)] 
    (prn key (count key)) 
    (.init mac (org.bouncycastle.crypto.params.DESedeParameters. key)) 
    (.update mac msg 0 (count msg)) 
    (.doFinal mac bytes 0) 
    (->hex-string bytes))) 

並獲得此輸出(例外是在(MAC的.init拋出...):

#<byte[] [[email protected]> 16 
IllegalArgumentException key size must be 16 or 24 bytes. org.bouncycastle.crypto.engines.DESedeEngine.init (:-1) 

現在你看,PRN IST印刷放鍵 - 長度,這是16. 但BouncyCastle抱怨,它不是16或24(更改密鑰長度爲24的鍵也沒有幫助)

此外,當我運行此代碼,沒有問題:

(defn mac1 [key message] 
    (let [engine (org.bouncycastle.crypto.engines.DESedeEngine.) 
     mac (org.bouncycastle.crypto.macs.CMac. engine) 
     bytes (byte-array (.getMacSize mac)) 
     msg (->bytes E-IFD)] 
    (.init mac (org.bouncycastle.crypto.params.DESedeParameters. (->bytes key))) 
    (.update mac msg 0 (count msg)) 
    (.doFinal mac bytes 0) 
    (->hex-string bytes))) 

回答

2

好吧,我在此發佈工作代碼。問題是我通過org.bouncycastle.crypto.engines.DESedeEngine而不是org.bouncycastle.crypto.engines.DESEngine

org.bouncycastle.crypto.macs.ISO9797Alg3Mac將鑰匙拆分爲3個,然後將第一個鑰匙傳遞給它的引擎。因此,DESedeEngine報告的密鑰大小錯誤,但原始密鑰的大小合適。

(defn mac2 [key message] 
    (let [engine (org.bouncycastle.crypto.engines.DESEngine.) 
     mac (org.bouncycastle.crypto.macs.ISO9797Alg3Mac. engine) 
     bytes (byte-array (.getMacSize mac)) 
     key (->bytes key) 
     msg (->bytes E-IFD)] 
    (prn key (count key)) 
    (.init mac (org.bouncycastle.crypto.params.DESedeParameters. key)) 
    (.update mac msg 0 (count msg)) 
    (.doFinal mac bytes 0) 
    (->hex-string bytes)))