Search code examples
androidweb-servicesx509certificate2

How to display x509certifcate in android app


I use spongycastle. I downloaded byte array certificate from webservice in android app (I think is base64 byte array) and want to display it on screen. I wrote below code but It does not work and c variable is null. in addition I don't know how to display it on screen.

  SoapObject result = (SoapObject)envelope.bodyIn;           

                   if(result != null)
                    {
                        Object cert= result.getProperty(0);   
                        InputStream is = new ByteArrayInputStream(cert.toString().getBytes());
                        CertificateFactory cf = CertificateFactory.getInstance("X.509","BC");
                        java.security.cert.Certificate c = cf.generateCertificate(is);
                        X509Certificate t = (X509Certificate) c;
                        System.out.println("ca=" + t.getSubjectDN());


                   }

Solution

  • Probably cert.toString().getBytes() is the problem. A certificate is encoded in binary format. To build it you need a byte array, not a string converted to bytes.

    Ensure in which format are you receiving the certificate in order to transform it into binary. If you think that the certificate is encoded in base64, decode it in this way

     byte[] data = Base64.decode(base64, Base64.DEFAULT);
    

    Note also the Java Security API Provider name for SpongyCastle is SC rather than BC

    To display a text you need for example to set the contento of a TextView inside an Activity or Fragment. I suggest you to get a tutorial first