0

I'm new in crypto with Java and I have a simple question. I have JKS keystore with SHA1withRSA trusted private key and certificate and I need to generate PKCS#7 signature for SOAP message. I tried found some info about this and at the moment, I have this:

KeyStore ks = KeyStore.getInstance("JKS");
ks.load(...);//load ks from ks path
//initiate signature(if I do it - Web-Service send me exception:Error while 
//ASN.1-decoding PKCS#7 message
RSAPrivateKey = (RSAPrivateKey) ks.getKey(...);
Signature sign = Signature.getInstance("SHA1WithRSA);
sign.initSign(privatKey);
sign.update(data)//data - final byte[] data - method argument
byte[] bb = sign.sign();
BASE64Encoder enc = new BASE64Encoder();
return encoder.encode(bb);

Please, tell me, where my mistake? Maybe I skiped need classes and this code don't work as good, as I want. Thanks.

Giymose
  • 201
  • 1
  • 6
  • 21
  • Do you get any error or something? It is to much generalized question "where my mistake?" – mazhar islam Jun 22 '15 at 05:08
  • are u sure `Signature.getInstance("SHA1WithRSA);` is correct? – Estimate Jun 22 '15 at 05:12
  • @rakeb.void before this, I used bouncycastle API and had signature verification error. For this code: Only ASN.1-decoding error – Giymose Jun 22 '15 at 05:13
  • @KrishanthyMohanachandran I have RSA key with SHA1 algorithm and all my searches led to `Signature.getInstance("SHA1WithRSA);` – Giymose Jun 22 '15 at 05:16
  • Surround you code in a `try-catch` block and find out exactly where the problem created. The signature generation part seems ok. – mazhar islam Jun 22 '15 at 07:29

1 Answers1

1

No, just generating a PKCS#1 signature is not enough.

PKCS#7 specifies the Cryptographic Message Syntax (CMS). This is a container format, not just a signature. You need an implementation of CMS to create such a signature. A well known library that contains an implementation of CMS is Bouncy Castle:

Generators/Processors for S/MIME and CMS (PKCS7/RFC 3852).

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263