• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

unable to read key values from file

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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
ry.java:98)
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);
sig.initSign(prvKey);
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);
sig.initVerify(pubKey);
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");
keyfos1.write(key1);
keyfos1.close();

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





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

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

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()];
keyfis2.read(decKey);
keyfis2.close();

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");
sigfos.write(sigbytes);
sigfos.close();

/* decrypt the message using public key
**/

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


}
}

=========================================

ANY ONE PLS HELP ME OUT.

Thanks,
Nandy
 
Ranch Hand
Posts: 290
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why dont you store them in a Keystore. It will be much easier to write and read?
Ahamd
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic