Java/Clojure BouncyCastle reports wrong key size, but key size is right -
i'm trying generate mac using iso9797 alghrythm 3. in clojure, guess i'm having more of java problem here. run code:
(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)))   and output (the exception thrown @ (.init mac ...):
#<byte[] [b@65e47e28> 16 illegalargumentexception key size must 16 or 24 bytes.  org.bouncycastle.crypto.engines.desedeengine.init (:-1)   now see, prn ist printing put key-length, 16. bouncycastle complains, not 16 or 24 (changing key key length of 24 not either)
also when run code, there no problem:
(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)))      
alright, post working code here. problem was passing org.bouncycastle.crypto.engines.desedeengine instead of org.bouncycastle.crypto.engines.desengine.
org.bouncycastle.crypto.macs.iso9797alg3mac splits key 3 pieces , passes first 1 engine. hence desedeengine reports wrong key size, although original key had right size.
(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)))      
Comments
Post a Comment