store my GnuPG generated private key in Java Key Store
843811Jun 30 2010 — edited Jul 7 2010Hi
My requirement is to generate a keypair(public and private) using gnupg and export the private key and store it in JKS-JavaKeyStore.
I am exporting the private key which is generated by gnupg and while trying to import it in JKS using Java API i am facing the fallowing exception:
java.security.KeyStoreException: key is not encoded as EncryptedPrivateKeyInfo
at sun.security.provider.JavaKeyStore.engineSetKeyEntry(JavaKeyStore.java:293)
at java.security.KeyStore.setKeyEntry(KeyStore.java:882)
and my code fallows:
String keystorename="C:/Documents and Settings/hponduri/My Documents/WorkspaceTestJKS.jks";
try {
KeyStore ks = KeyStore.getInstance("JKS", "SUN");
ks.load( null , passPhrase.toCharArray());
ks.store(new FileOutputStream(keystorename),passPhrase.toCharArray());
ks.load(new FileInputStream ( keystorename ),passPhrase.toCharArray());
ks.setKeyEntry("java-keyPair", getBytes(), null);
ks.containsAlias("java-keyPair");
ks.getKey("java-keyPair", passPhrase.toCharArray());
} catch (Exception e) {
}
To get the private converted to bytes :
public static byte[] getBytes(){
BufferedReader bufferReader = null;
StringBuffer sb = new StringBuffer();
String s;
bufferReader = new BufferedReader(new FileReader("C:/TEMP/secring.asc"));//secring.asc is my private key generated using gnuPG
while ((s = bufferReader.readLine()) != null) {
sb.append(s + "\n");
}
bufferReader.close();
s=sb.toString();
return s.getBytes();
}
can anyone please help me out in storing my gpg private key into JKS.or is there any other tool from which i can generate keypair and store them in JKS