Uderc programming article aggregator sites

How to strore keys in KeyStores..of JCE Api..

Answers: Have 4 answers
Hi ,
i'm trying to implement a program mySignature that creates and verifies digital signatures using Java Cryptography Extension.
I am using the DSA Algorithm for encryption.
Now the problem is once i create the private keys and public keys i am not able to store them to the KeyStore.
here is the code I wrote
package my.security.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Certificate;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAKeyGenParameterSpec;
* @author sandeepk
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
public class MySignature {
     public static void main(String[] args) {
          KeyPairGenerator keyGen = null;
          FileOutputStream sigfos = null;
          try {
               Signature signature = Signature.getInstance("SHA1withDSA", "SUN");
               keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
               KeyPair keyPair = keyGen.generateKeyPair();
               PrivateKey privateKey = keyPair.getPrivate();
               PublicKey publicKey = keyPair.getPublic();
               KeyStore ks = KeyStore.getInstance("JKS", "SUN");
               ks.setKeyEntry("privkey", privateKey, "pass".toCharArray(), null);
               ks.setKeyEntry("pubkey", publicKey, "pass".toCharArray(), null);
               sigfos = new FileOutputStream("C:\\keystore");
               ks.store(sigfos, "pass".toCharArray());
               // Reading the contents of them message file               
               FileInputStream fis = new FileInputStream("C:\\message.txt");
               BufferedInputStream bufin = new BufferedInputStream(fis);
               byte[] buffer = new byte[1024];
               int len;
               while (bufin.available() != 0) {
                    len = bufin.read(buffer);
                    signature.update(buffer, 0, len);
               // Writng the signature to a file
               byte[] realSig = signature.sign();
               sigfos = new FileOutputStream("C:\\sig.txt");
          } catch (InvalidKeyException e) {
          } catch (NoSuchAlgorithmException e) {
          } catch (FileNotFoundException e) {
          } catch (SignatureException e) {
          } catch (IOException e) {
          } catch (NoSuchProviderException e) {
          } catch (KeyStoreException e) {
          }catch(CertificateException e){
I get an error while i try to store the key on to the KeyStore, if I comment out these lines of code the program runs without any probs.
Can any one help me with this issue, i'ld be thank full to them
The best answer: We use 'PDDocCreateWordFinder' , 'PDWordFinderEnumWords' to extract the text from Adobe Reader document through Code, Not tool.
But, by using these APIs, I can't see any difference in 'new line/row' or 'paragraph' if exists in PDF doc.
I need such API which can give the exact format of Adobe Reader doc according to the content exists in that PDF doc.