Amit
asked on
Java Security - RSA Key generation
I have created the following class RSAM that generates a RSA public key and writes it to exportedRSAKey
Then another class ImportRSA reads exportedRSAKey and generates the key, however the key is not generated and "key" contains null values , could some body find what's wrong
I have done the same thing for the public key and its working fine. The purpose is well known - to do asymettric encryption
__________________________ __________ __________ __________ ______
import java.security.*;
import java.security.spec.*;
import java.io.*;
public class RSAM {
public static void main(String args[]) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstan ce("RSA");
kpg.initialize(512, new SecureRandom( ));
KeyPair kp = kpg.generateKeyPair( );
Class spec = Class.forName(
"java.security.spec.RSAPri vateKeySpe c");
KeyFactory kf = KeyFactory.getInstance("RS A");
RSAPrivateKeySpec ks = (RSAPrivateKeySpec)
kf.getKeySpec(kp.getPrivat e( ), spec);
FileOutputStream fos = new FileOutputStream("exported RSAKey");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(ks.getModu lus());
oos.writeObject(ks.getPriv ateExponen t( ));
} catch (Exception e) {
e.printStackTrace( );
}
}
}
__________________________ __________ __________ __________ __________ ____
import java.security.*;
import java.security.spec.*;
import java.io.*;
import java.math.*;
public class ImportRSA {
public static void main(String args[]) {
try {
FileInputStream fis = new FileInputStream("exportedR SAKey");
ObjectInputStream ois = new ObjectInputStream(fis);
RSAPrivateKeySpec ks = new RSAPrivateKeySpec(
(BigInteger) ois.readObject( ),
(BigInteger) ois.readObject( )
);
KeyFactory kf = KeyFactory.getInstance("RS A");
PrivateKey pk = kf.generatePrivate(ks);
System.out.println("Got private key");
System.out.println("privat e Key encoded :"+pk.getEncoded());
} catch (Exception e) {
e.printStackTrace( );
}
}
}
Then another class ImportRSA reads exportedRSAKey and generates the key, however the key is not generated and "key" contains null values , could some body find what's wrong
I have done the same thing for the public key and its working fine. The purpose is well known - to do asymettric encryption
__________________________
import java.security.*;
import java.security.spec.*;
import java.io.*;
public class RSAM {
public static void main(String args[]) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstan
kpg.initialize(512, new SecureRandom( ));
KeyPair kp = kpg.generateKeyPair( );
Class spec = Class.forName(
"java.security.spec.RSAPri
KeyFactory kf = KeyFactory.getInstance("RS
RSAPrivateKeySpec ks = (RSAPrivateKeySpec)
kf.getKeySpec(kp.getPrivat
FileOutputStream fos = new FileOutputStream("exported
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(ks.getModu
oos.writeObject(ks.getPriv
} catch (Exception e) {
e.printStackTrace( );
}
}
}
__________________________
import java.security.*;
import java.security.spec.*;
import java.io.*;
import java.math.*;
public class ImportRSA {
public static void main(String args[]) {
try {
FileInputStream fis = new FileInputStream("exportedR
ObjectInputStream ois = new ObjectInputStream(fis);
RSAPrivateKeySpec ks = new RSAPrivateKeySpec(
(BigInteger) ois.readObject( ),
(BigInteger) ois.readObject( )
);
KeyFactory kf = KeyFactory.getInstance("RS
PrivateKey pk = kf.generatePrivate(ks);
System.out.println("Got private key");
System.out.println("privat
} catch (Exception e) {
e.printStackTrace( );
}
}
}
ASKER
Hi Girionis,
I have done the same things for the Public Key. If you edit the above two files and replace "private" by "public" then it actually works. I don't get a null value for the public key.
I have done the same things for the Public Key. If you edit the above two files and replace "private" by "public" then it actually works. I don't get a null value for the public key.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works but doesn't solve my purpose. Here's what I am doing I have created 3 java files
One generates a key pair and writes the public and private key in byte formats to two different files
exportedRSAKey - stores primary key
exportedRSAPublicKey - stores public key
Then I use the other two files to read them and generate the keys and use them to encrypt and decrypt.
By using your program , I still need to write the key bytes in two files and generate the keys at some other location
One generates a key pair and writes the public and private key in byte formats to two different files
exportedRSAKey - stores primary key
exportedRSAPublicKey - stores public key
Then I use the other two files to read them and generate the keys and use them to encrypt and decrypt.
By using your program , I still need to write the key bytes in two files and generate the keys at some other location
Hmm.. Sorry I am not sure what's going on, I have only limited experience with security and encryption. Maybe someone else can help you.
(from: http://www.geocities.com/herong_yang/jdk/jca_encoding.html)
From what I can see you are actually using a key specification, not the key itself. SO you do not encode anything. In your second programme (ImportRSA) you get the specification of the key from the saved file (exportedRSAKey) but not the actual key and therefore not the encoding of the key. The encoding is always with regards to the key not to the key specification.