Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Security - RSA Public Key & Private Key Code Issue
    text
    copied!<p>Here is my error </p> <pre><code>java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: DerInputStream.getLength(): lengthTag=127, too big. at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(Unknown Source) at java.security.KeyFactory.generatePublic(Unknown Source) at com.sanjaya.rsa.PublicKeyReader.get(PublicKeyReader.java:22) at com.sanjaya.rsa.MainKeyClass.main(MainKeyClass.java:8) Caused by: java.security.InvalidKeyException: IOException: DerInputStream.getLength(): lengthTag=127, too big. at sun.security.x509.X509Key.decode(Unknown Source) at sun.security.x509.X509Key.decode(Unknown Source) at sun.security.rsa.RSAPublicKeyImpl.&lt;init&gt;(Unknown Source) at sun.security.rsa.RSAKeyFactory.generatePublic(Unknown Source) ... 4 more </code></pre> <p>Here is my code - <strong>MAIN CLASS</strong></p> <pre><code>public static void main(String[] args) { PublicKeyReader publicKeyReader=new PublicKeyReader(); try { publicKeyReader.get("c:\\public.key"); } catch (Exception e) { e.printStackTrace(); } PrivateKeyReader privateKeyReader=new PrivateKeyReader(); try { privateKeyReader.get("c:\\private.key"); } catch (Exception e) { e.printStackTrace(); } </code></pre> <p>Here is public key related method</p> <pre><code> public static PublicKey get(String filename) throws Exception { File f = new File(filename); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int)f.length()]; dis.readFully(keyBytes); dis.close(); X509EncodedKeySpec spec =new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(spec); } </code></pre> <p>Here is private key related method </p> <pre><code> public static PrivateKey get(String filename)throws Exception { File f = new File(filename); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int)f.length()]; dis.readFully(keyBytes); dis.close(); PKCS8EncodedKeySpec spec =new PKCS8EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(spec); } </code></pre> <p>Please help me to sort out this issue</p> <p>Need to create public key &amp; private key &amp; Encrypt ,decrypt string from those under RSA encyption</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload