Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / June 2004

Tip: Looking for answers? Try searching our database.

problem in opening  signature file

Thread view: 
chandan kumar - 10 Jun 2004 13:20 GMT
Dear sir,
i am creating a signature file.but the file is not opening.
here is the list what i am doing.
1)i am importing a .pfx file by giving the password
2)i am extracting the private key from the .pfx.file.
3)i am creating a message digest array of a file masher.java
4)then  i am storing the mesage digest array  in a file named
digest.jar.
5)then i am reading the digest.jar file.
6)with the help of private key i am creating the signature instance.
7) then i am getting the signature array by the sign method of
signature class
8)i am storing this array   in a .p7s file
9)then i am opening this file by double clicking on the file but the
error message is "this is an invalid PKCS#7 file".
10) i can 't understand what are the wrong going on in my file
11)in my mail i am sending a file whose name is fourthofx.java
12)in this file i am importing the file by giving the password.
13)then i am creating an messagedigest  array from the file masher
.java
14)then to this  extracting array  i am storing in a file whose name
is "Digest.jar"
15)then i am reading this file and through the private key i  am
making signature  .
16) the  array return by making signature  , i am storing in  a file
"mime.p7s"
17) now i am openning this file by double clicking .
18)the result is file could not be opened.the message is "this is an
invalid PKCS#7 file".
19) please help me
20) i am suffering in this problem till twenty days, please help me
       
here is my coding follows:--

import sun.misc.BASE64Encoder;
import java.security.cert.Certificate;
import java.security.*;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
import java.io.*;

class FourthPfx {
   Signature setsign;
   public static void main(String args[]) throws Exception{
          FourthPfx myep = new FourthPfx();
          myep.doit();
   }
   public void doit() throws Exception{
          KeyStore ks = KeyStore.getInstance("pkcs12");
          String fileName = "c:/chandanfirst/pamas123.pfx";
          char[] passPhrase = "pamas123".toCharArray();
          BASE64Encoder myB64 = new BASE64Encoder();
          File certificateFile = new File(fileName);
          ks.load(new FileInputStream(certificateFile), passPhrase);
          String duke="";
          Enumeration enum = ks.aliases();
          while(enum.hasMoreElements()){
               String ali = enum.nextElement().toString();
               if(ks.isKeyEntry(ali)){
                    duke = ali;
               }
          }
          KeyPair kp = getPrivateKey(ks, duke, passPhrase);
         
          PrivateKey privKey = kp.getPrivate();

               String b64 = myB64.encode(privKey.getEncoded());
               System.out.println("-----BEGIN PRIVATE KEY-----");
               System.out.println(b64);
               System.out.println("-----END PRIVATE KEY-----");
               MessageDigest md= MessageDigest.getInstance("MD5");
               FileInputStream in = new
FileInputStream("C:/27092002/CD0870/CC_N_CD0870_27092002.htm");
               byte[] buffer = new byte[8193];
               int length;
               while((length=in.read(buffer))!=-1)
               md.update(buffer,0,length);
               byte[] raw = md.digest();
               FileOutputStream out = new
FileOutputStream("DIGEST.JAR");
               out.write(raw);
               out.close();
               Provider[] pvd = Security.getProviders();
               for(int i=0;i<pvd.length;i++)
                  System.out.println("provider"+pvd[i].toString());
               FileInputStream secin = new
FileInputStream("DIGEST.JAR");
                     try{
                        setsign =
Signature.getInstance("MD5withRSA","SunJSSE");
                     } catch(NoSuchAlgorithmException e){
                         System.out.println("alg
exception"+e.getMessage());
                     }

                     
               setsign.initSign(privKey);
               byte[] secbuffer = new byte[8193];
               int seclength;
               while((seclength=secin.read(secbuffer))!=-1){
                     setsign.update(secbuffer,0,seclength);
               }
               secin.close();
               byte secraw[] = setsign.sign();
               String rite =  myB64.encode(secraw);
               FileOutputStream out1 = new
FileOutputStream("c:/project/Mime.p7s");
               System.out.println("base64"+rite);
               out1.write(secraw);
               out1.close();

   }
     
   public KeyPair getPrivateKey(KeyStore keystore, String alias,
char[] password) {
        try {
                    // Get private key
            Key key = keystore.getKey(alias, password);
            if (key instanceof PrivateKey) {
                    // Get certificate of public key
                    Certificate cert =
keystore.getCertificate(alias);
                    // Get public key
                    PublicKey publicKey = cert.getPublicKey();
                    // Return a key pair
                    return new KeyPair(publicKey, (PrivateKey)key);
            }
        } catch (UnrecoverableKeyException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (KeyStoreException e) {
        }
        return null;
   }
}
Michael Amling - 11 Jun 2004 04:50 GMT
> Dear sir,
> i am creating a signature file.but the file is not opening.
[quoted text clipped - 9 lines]
> signature class
> 8)i am storing this array   in a .p7s file

  Where did you find documentation about the format of a .p7s file? Is
it supposed to contain anything in addition to a raw signature bytes?

--Mike Amling


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.