Uderc programming article aggregator sites

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

Tags:
Answers: Have 4 answers
Advertisement
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");
               keyGen.initialize(1024);
               KeyPair keyPair = keyGen.generateKeyPair();
               PrivateKey privateKey = keyPair.getPrivate();
               PublicKey publicKey = keyPair.getPublic();
               KeyStore ks = KeyStore.getInstance("JKS", "SUN");
               System.out.println("Here1");
               ks.setKeyEntry("privkey", privateKey, "pass".toCharArray(), null);
               System.out.println("Here2");
               ks.setKeyEntry("pubkey", publicKey, "pass".toCharArray(), null);
               System.out.println("Here3");
               sigfos = new FileOutputStream("C:\\keystore");
               ks.store(sigfos, "pass".toCharArray());
               signature.initSign(privateKey);
               // 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);
               bufin.close();
               // Writng the signature to a file
               byte[] realSig = signature.sign();
               sigfos = new FileOutputStream("C:\\sig.txt");
               sigfos.write(realSig);
               sigfos.close();
          } catch (InvalidKeyException e) {
               e.printStackTrace();
          } catch (NoSuchAlgorithmException e) {
               e.printStackTrace();
          } catch (FileNotFoundException e) {
               e.printStackTrace();
          } catch (SignatureException e) {
               e.printStackTrace();
          } catch (IOException e) {
               e.printStackTrace();
          } catch (NoSuchProviderException e) {
               e.printStackTrace();
          } catch (KeyStoreException e) {
               e.printStackTrace();
          }catch(CertificateException e){
               e.printStackTrace();
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
-Chau
Sandy
Advertisement
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.
Thanks!