Hey Emeka,

No problem. Yeah, the reason I used the above configuration is because
it ultimately returned the same primitive byte array, to be passed
into the X509EncodedKeySpec constructor.

Sam

On Aug 31, 1:27 pm, Emeka <emekami...@gmail.com> wrote:
> Sorry, I was too quick. I misunderstood your code.
>
> Regards,
> Emeka
>
> On Mon, Aug 31, 2009 at 6:10 PM, Emeka <emekami...@gmail.com> wrote:
>
> > Hello Sam,
>
> > From the Java version you used created array ,byte[] encodedKey = new
> > byte[(int) keyFile.length()];, but in clojure version you did (into-array
> > Byte/TYPE (byte-seq stream)) , why not  use make-array here.
>
> > (byte-seq stream) returns list, is that the right argument for constructor
> > call?
>
> > Did  you try this;
>
> > (make-array Byte/TYPE (int (. (java.io.File. "public.der") length)))
>
> > Regards,
> > Emeka
> >   On Mon, Aug 31, 2009 at 11:19 AM, Sam Hughes 
> > <samuel.jenni...@gmail.com>wrote:
>
> >> Hey,
>
> >> I'm trying to write a Clojure security library. My first step is
> >> porting some working Java code into Clojure. The Java and Clojure
> >> snippets below are more or less the same, but with the Clojure code,
> >> I'm getting: "java.security.InvalidKeyException: IOException: null
> >> [Thrown class java.security.spec.InvalidKeySpecException]," which I
> >> can't seem to replicate with the Java code.
>
> >> The goal of the code is to read in a DER file, use it to encrypt a
> >> "Hello World" message, then output the encrypted message as a new
> >> file.
>
> >> Neither of these snippets necessarily follow good coding standards.
> >> That said, here's the working Java code snippet:
>
> >> final File keyFile = new File("public.der");
> >> byte[] encodedKey = new byte[(int) keyFile.length()];
>
> >> new FileInputStream(keyFile).read(encodedKey);
> >> final byte[] newEncoded = encodedKey;
>
> >> final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(newEncoded);
> >> KeyFactory kf = KeyFactory.getInstance("RSA");
> >> PublicKey pk = kf.generatePublic(keySpec);
>
> >> Cipher rsa = Cipher.getInstance("RSA");
> >> rsa.init(Cipher.ENCRYPT_MODE, pk);
> >> OutputStream os = new CipherOutputStream(new FileOutputStream
> >> ("encrypted.rsa"), rsa);
>
> >> Writer out = new OutputStreamWriter(os);
> >> out.write("Hello World");
> >> out.close();
> >> os.close();
>
> >> And here's the Exception throwing Clojure code:
>
> >> (ns security
> >>  (:import
> >>   [java.io File FileInputStream IOException]
> >>   [java.security.spec X509EncodedKeySpec]
> >>   [java.security KeyFactory PublicKey
> >>    KeyPairGenerator NoSuchAlgorithmException KeyPair]
> >>   [javax.crypto KeyGenerator Cipher]))
>
> >> (defn byte-seq [rdr]
> >>  (let [result (byte (. rdr read))]
> >>    (if (= result -1)
> >>      (do (. rdr close) nil)
> >>      (lazy-seq (cons result (byte-seq rdr))))))
>
> >> (def stream (new FileInputStream (new File "public.der")))
> >> (def byte-arr (into-array Byte/TYPE (byte-seq stream)))
> >> (def pk-spec (new X509EncodedKeySpec byte-arr))
> >> (def kf (. KeyFactory (getInstance "RSA")))
> >> (def pk (. kf (generatePublic pk-spec)))          ; exception thrown
> >> here
>
> >> Does anyone have any suggestion for what could be causing the
> >> exception? I'm perplexed because, right now, I'm just trying to
> >> replicate Java code in Clojure -- nothing too fancy.
>
> >> Thanks a lot,
> >> Sam
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to