The reason your byte-seq fails is because you coerce the int result to
a byte before comparing to -1. You should compare the int result to -1
and coerce to a byte after:
(defn byte-seq [rdr]
  (let [result (. rdr read)]
    (if (= result -1)
      (do (. rdr close) nil)
      (lazy-seq (cons (byte result) (byte-seq rdr))))))

Because
user=> (byte 255)
-1

-1 is a valid byte to read in the file and will be returned as int 255
by read

security=> pk
#<RSAPublicKeyImpl Sun RSA public key, 2048 bits


Regards,
Tim.



Alternatively here is a more direct translation of your java version
which also works:
(let [file (new File "public.der")
      byte-arr (make-array Byte/TYPE (.length file))
      stream (new FileInputStream file)]
  (println "READ: " (.read stream byte-arr))
  (let [pk-spec (new X509EncodedKeySpec byte-arr)
        kf (KeyFactory/getInstance "RSA")]
    (.generatePublic kf pk-spec)))
READ:  294
#<RSAPublicKeyImpl Sun RSA public key, 2048 bits


On Sep 1, 9:18 am, Timothy Pratley <timothyprat...@gmail.com> wrote:
> security=> (count byte-arr)
> 115
> tprat...@neuromancer:~$ wc public.der
>   5  11 294 public.der
>
> your byte-seq does not do what the java version does :)
>
> On Aug 31, 9:19 pm, 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