Here is my error
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.<init>(Unknown Source)
at sun.security.rsa.RSAKeyFactory.generatePublic(Unknown Source)
... 4 more
Here is my code - MAIN CLASS
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();
}
Here is public key related method
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);
}
Here is private key related method
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);
}
Please help me to sort out this issue
Need to create public key & private key & Encrypt ,decrypt string from those under RSA encyption