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