why can not find the Algorithm about the SignedData?

I'm using the class SignedData of package Codec,and i add the BoundCastle provider.

The code followed:

import java.io.*;

import java.util.*;

import java.security.cert.*;

import java.security.*;

import codec.pkcs7.*;

import codec.Base64;

import codec.asn1.*;

import codec.x501.*;

class PFXSign

{

private SignedData sigdata_;

private codec.pkcs7.Signer signer_;

private SignerInfo siginfo_;

private X509Certificate cert_;

public void loadPFX(String pfxFilename, String passwd)

{

PrivateKey privatekey;

char[] storepass = passwd.toCharArray();

try

{

KeyStore ks = KeyStore.getInstance("PKCS12","SunJSSE");

FileInputStream fis = new FileInputStream(pfxFilename);

ks.load(fis,storepass);

fis.close();

// retrieve information from pfx certificate

Enumeration en = ks.aliases();

String alias = (String) en.nextElement();

//System.out.println("Contains " + alias);

cert_ = (X509Certificate)ks.getCertificate(alias);

//System.out.println(cert.toString());

privatekey = (PrivateKey)ks.getKey(alias,storepass);

//System.out.println(privatekey.getAlgorithm());

// init SignerInfo, Signer

siginfo_ = new SignerInfo(cert_,"SHA1WithRSA");

siginfo_.addAuthenticatedAttribute(

new Attribute(

new ASN1ObjectIdentifier("1.2.840.113549.1.9.5"),

new ASN1UTCTime()));

sigdata_ = new SignedData();

sigdata_.setData(null);

sigdata_.addCertificate(cert_);

signer_ = new codec.pkcs7.Signer(sigdata_, siginfo_, privatekey);

}

catch (Exception e)

{

System.out.println("Error: " + e);

}

}

private String sign(String docFilename)

{

ContentInfo info;

DEREncoder enc;

String sigString=null;

try

{

FileInputStream fis = new FileInputStream(docFilename);

signer_.update(fis);

fis.close();

signer_.sign();

System.out.println(sigdata_.toString());

info = new ContentInfo(sigdata_);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

enc = new DEREncoder(baos);

info.encode(enc);

enc.close();

byte[] sigBytes = baos.toByteArray();

sigString = Base64.encode(sigBytes);

}

catch (Exception e)

{

System.out.println("Error: " + e);

}

return sigString;

}

public static void main(String[] args)

{

if(args.length < 4) {

System.out.println("PFXSign pfxFilename password docFilename signFilename");

return;

}

PFXSign testSign = new PFXSign();

testSign.loadPFX(args[0],args[1]);

String sigString = testSign.sign(args[2]);

System.out.println(sigString);

try

{

FileOutputStream fos = new FileOutputStream(args[3]);

fos.write(sigString.getBytes());

fos.close();

}

catch (Exception e)

{

System.out.println("Error: " + e);

}

}

}

when i run it,it said "NoSuchAlgorithmException" at "siginfo_ = new SignerInfo(cert_,"SHA1WithRSA");"

how can i do?

thanks!

yannqi

[3233 byte] By [yannqia] at [2007-9-21]
# 1
I think you should import java.security.NoSuchAlgorithmException!. Have a good time!
VoBuiMinhThanha at 2007-7-14 > top of java,Security,Cryptography...