Search code examples
androidx509certificatebouncycastle

How to create pkcs#7 certificate with SpongyCastle on Android?


I am trying to generate on my Android app pkcs7 file to communicate between client and server (confidential data are transmitted between them).

I actually try SpongyCastle which seems fine for the job.

Actually, I tried with the example given : AttrCertExample. My certificate seems generated by the end (a v3 one) and verified, all is OK.

Then I try to make a der version of it like this :

String strResult = Base64.encode(cert.getTBSCertificate(), Base64.NO_WRAP).toString();

But the result of this operation gives me a very little Base 64 encoded String (where I wanted to have something like this :

MIICyzCCAbOgAwI [ ... ] 6Shws= (960 chars in base 64)

Do you actually know how to get a PKCS#7 version of the certificate generated by SpongyCastle ?

Thanks a lot in advance, I appreciate the help. I can give some more code if needed.


Solution

  • I got the answer quickly. This is how you do this :

    // returns a byte array containing the Certificate
    byte[] buf = clientCert.getEncoded(); 
    
    // Returns Base64 encoded certificate
    String strCertificateCrypted_b64 = new String(Base64.encode(buf, Base64.NO_WRAP)); 
    

    Then you obtain a certificate like this :

    MIIDHjCCAgagAwIBAgIEOH9W+ [...] czP+BohBw==
    

    By saving this with DER extension, you will be able to see certificate contents easily.