Java/Clojure BouncyCastle报告了错误的密钥大小,但密钥大小是正确的
我正在尝试使用ISO9797算法3生成MAC。 我在Clojure中这样做,但我想我这里有更多的Java问题。我运行以下代码:Java/Clojure BouncyCastle报告了错误的密钥大小,但密钥大小是正确的,java,clojure,bouncycastle,des,Java,Clojure,Bouncycastle,Des,我正在尝试使用ISO9797算法3生成MAC。 我在Clojure中这样做,但我想我这里有更多的Java问题。我运行以下代码: (defn mac2 [key message] (let [engine (org.bouncycastle.crypto.engines.DESedeEngine.) mac (org.bouncycastle.crypto.macs.ISO9797Alg3Mac. engine) bytes (byte-array (.getM
(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)))
并获取此输出(异常在(.init mac…)处引发:
好的,我在这里发布了工作代码。问题是我传递的是
org.bouncycastle.crypto.engines.desedengine
,而不是org.bouncycastle.crypto.engines.DESEngine
org.bouncycastle.crypto.macs.ISO9797Alg3Mac
将密钥分成3段,然后将第一段传递给其引擎。因此desedengine
报告错误的密钥大小,尽管原始密钥的大小正确
(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)))
(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)))
(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)))