jQuery in Action, 2nd edition*
The moose likes Security and the fly likes unable to read key values from file    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "unable to read key values from file    " Watch "unable to read key values from file    " New topic

unable to read key values from file

Nandan Jain
Ranch Hand

Joined: Feb 14, 2006
Posts: 44

My objective is to create public and private key pair, store them in file , for future use. Read them back from the file and then recrypt the message and decrypt the message. I am using the following program but having hard time. It gives me the error message :
Exception in thread "main" java.security.spec.InvalidKeySpecException: Inappropr
iate key specification
at sun.security.provider.DSAKeyFactory.engineGeneratePrivate(DSAKeyFacto
at java.security.KeyFactory.generatePrivate(KeyFactory.java:237)
at SignatureTest.main(SignatureTest.java:79)
here is the java prgram :
// File: src\jsbook\ch3\ex1\SignatureTest.java
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.Signature;
import java.io.FileInputStream;
import java.io.*;
import java.security.spec.X509EncodedKeySpec;
import java.security.*;

public class SignatureTest {

private static byte[] sign(String datafile, PrivateKey prvKey,
String sigAlg) throws Exception {
Signature sig = Signature.getInstance(sigAlg);
FileInputStream fis = new FileInputStream(datafile);
byte[] dataBytes = new byte[1024];
int nread = fis.read(dataBytes);
while (nread > 0) {
sig.update(dataBytes, 0, nread);
nread = fis.read(dataBytes);
return sig.sign();

private static boolean verify(String datafile, PublicKey pubKey,
String sigAlg, byte[] sigbytes) throws Exception {
Signature sig = Signature.getInstance(sigAlg);
FileInputStream fis = new FileInputStream(datafile);
byte[] dataBytes = new byte[1024];
int nread = fis.read(dataBytes);
while (nread > 0) {
sig.update(dataBytes, 0, nread);
nread = fis.read(dataBytes);
return sig.verify(sigbytes);

public static void main(String[] unused) throws Exception {
// Generate a key-pair
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
kpg.initialize(512); // 512 is the keysize.
KeyPair kp = kpg.generateKeyPair();
PublicKey pubk = kp.getPublic();
PrivateKey prvk = kp.getPrivate();

/* store key values in file for future use

byte[] key1 = pubk.getEncoded();
FileOutputStream keyfos1 = new FileOutputStream("pubKey");

byte[] key2 = prvk.getEncoded();
FileOutputStream keyfos2 = new FileOutputStream("privKey");

/* reading key values from file
* first reading private key and then public key

FileInputStream keyfis1 = new FileInputStream("privKey");
byte[] encKey = new byte[keyfis1.available()];

X509EncodedKeySpec privKeySpec = new X509EncodedKeySpec(encKey);
KeyFactory keyFactory1 = KeyFactory.getInstance("DSA" );
PrivateKey RprivKey = keyFactory1.generatePrivate(privKeySpec);

FileInputStream keyfis2 = new FileInputStream("privKey");
byte[] decKey = new byte[keyfis2.available()];

X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decKey);
KeyFactory keyFactory2 = KeyFactory.getInstance("DSA" );
PublicKey RpubKey = keyFactory2.generatePublic(pubKeySpec);

/* encrypt the message with private key
String datafile = "data.txt";
byte[] sigbytes = sign(datafile, prvk, "SHAwithDSA");

FileOutputStream sigfos = new FileOutputStream("sig");

/* decrypt the message using public key

boolean result = verify(datafile, RpubKey, "SHAwithDSA", sigbytes);
System.out.println("Signature Verification Result = " + result);




Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Why dont you store them in a Keystore. It will be much easier to write and read?

Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
Well, according to X509EncodedKeySpec docs, "This class represents the ASN.1 encoding of a public key". So I think we can't use it for private keys. That may be the origin of problem while reading private key.

and for your public key It looks like there has been a typing mistake in following line

FileInputStream keyfis2 = new FileInputStream("privKey"); //should be pubkey

what do you say?

Regards<br /> <br />Arvind Giri<br />MCA,SCJP 1.4,SCWCD 1.4<br />Looking for SCDJWS
I agree. Here's the link: http://aspose.com/file-tools
subject: unable to read key values from file
Similar Threads
problem with RSA..... folks please help!
Store PrivateKey PublicKey in DB
need to generate RSA keys from txt files
Get the PrivateKey
create a public key object