2015-06-13 77 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)))