如果没有明确指定,bouncycastle CMSSignedDataGenerator的默认签名算法是什么

时间:2012-03-16 08:38:51

标签: algorithm cryptography bouncycastle signature

我想知道BouncyCastle默认使用哪种签名算法(digestOID),如果你没有明确指定它,就像在下面的代码中那样:

  List             certList = new ArrayList();
  CMSTypedData     msg = new CMSProcessableByteArray("Hello world!".getBytes());

  certList.add(signCert);

  Store           certs = new JcaCertStore(certList);

  CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
  ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(signKP.getPrivate());

  gen.addSignerInfoGenerator(
            new JcaSignerInfoGeneratorBuilder(
                 new JcaDigestCalculatorProviderBuilder().setProvider("BC").build())
                 .build(sha1Signer, signCert));

  gen.addCertificates(certs);

  CMSSignedData sigData = gen.generate(msg, false);

下面是我想知道的代码示例,因为你看到没有digestOID(SHA1withRSA)所以它使用什么类型的签名:

import java.io.*;
import java.util.*;
import java.security.*;
import java.security.Security;
import java.security.cert.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.cms.*;


/* Create CMS/pkcs #7 signature using BC provider
                 M. Gallant  07/02/2003  */

class BCSignFile {
 static final boolean DEBUG = false;

 public static void main(String args[]) {
 System.out.println("");

  if (args.length < 4)
    usage();

 Security.addProvider(new BouncyCastleProvider());

 String INFILE   = args[0]; // Input file to be signed
 String KEYSTORE = args[1]; // Java 2 keystore file
 String ALIAS    = args[2]; // Java 2 key entry alias
 String PSWD     = args[3]; // keystore password

 // ---- in real implementation, provide some SECURE way to get keystore
 // ---- password from user! -------

 KeyStore keystore = null;
 PublicKey pub = null;
 PrivateKey priv = null;
 java.security.cert.Certificate storecert = null;
 java.security.cert.Certificate[] certChain = null;
 ArrayList certList = new ArrayList();
 CertStore certs =null;

 try{
   keystore = KeyStore.getInstance("JKS");
   keystore.load(new FileInputStream(KEYSTORE), PSWD.toCharArray());

   certChain = keystore.getCertificateChain(ALIAS);
   for ( int i = 0; i < certChain.length;i++)
    certList.add(certChain[i]);      
   certs = CertStore.getInstance("Collection", new     CollectionCertStoreParameters(certList), "BC");

       priv = (PrivateKey)(keystore.getKey(ALIAS, PSWD.toCharArray()));

   storecert = keystore.getCertificate(ALIAS);
   pub = keystore.getCertificate(ALIAS).getPublicKey();
 }
 catch(Exception exc){
  System.out.println("Problem with keystore access: " + exc.toString()) ;
  return;
  }


  if(DEBUG){
   System.out.println("Public Key Format: " + pub.getFormat()) ;
   System.out.println("Certificate " + storecert.toString()) ;
  }

  FileInputStream freader = null;
  File f = null;

    //------  Get the content data from file -------------
      f = new File(INFILE) ;
  int sizecontent = ((int) f.length());
  byte[] contentbytes = new byte[sizecontent];

  try {
    freader = new FileInputStream(f);
    System.out.println("\nContent Bytes: " + freader.read(contentbytes, 0,     sizecontent));
    freader.close();
   }
  catch(IOException ioe) {
    System.out.println(ioe.toString());
    return;
    }


// --- Use Bouncy Castle provider to create CSM/PKCS#7 signed message ---
 try{
  CMSSignedDataGenerator signGen = new CMSSignedDataGenerator();
  signGen.addSigner(priv, (X509Certificate)storecert,     CMSSignedDataGenerator.DIGEST_SHA1);
  signGen.addCertificatesAndCRLs(certs);
  CMSProcessable content = new CMSProcessableByteArray(contentbytes);

  CMSSignedData signedData = signGen.generate(content,"BC");
  byte[] signeddata = signedData.getEncoded();
  System.out.println("Created signed message: " + signeddata.length + " bytes") ;
  FileOutputStream envfos = new FileOutputStream("BCsigned.p7s");
                  envfos.write(signeddata);
  envfos.close();
 }
 catch(Exception ex){
  System.out.println("Couldn't generate CMS signed message\n" + ex.toString()) ;
 }
}


 private static void usage() {
  System.out.println("Usage:\n java BCSignFile  <contentfile> <keystore> <alias>     <keypasswd>") ;
  System.exit(1);
 }
    }

1 个答案:

答案 0 :(得分:4)

相关的一行是:

signGen.addSigner(priv, (X509Certificate)storecert, CMSSignedDataGenerator.DIGEST_SHA1);

此行指定摘要算法将为SHA-1,并且将根据priv中私钥的类型决定签名算法。

如果priv包含RSA密钥,它将使用带有SHA-1的PKCS#1 v.1.5(“SHA1withRSA”)进行签名。您可以查看CMSSignedGenerator.getEncOID()的来源,了解其他类型的私钥会发生什么。