Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 7403

Digital signature: sample code for verification and for extracting certification information

I use a third party tool to verify signature and to get certificate detail(like serial number, CA etc..) from signature. The problem with this utility is it is licensed and works on certain machines only.

Can i validate the signature against the data using simple java or .net code?(instead of using paid application). I dont have private key to extract certificate information from signed data.

Or if someone can suggest sample code in java or .net to extract certificate detail if i have pfx file. Of from signed data.

Data is signed with asymmetric encryption.

Upvotes: 5

Views: 11681

Answers (2)

Raj
Raj

Reputation: 1163

If you are having the PFX file, then that may contain the public key certificate which will be required to verify the signature.

Alternatively, if your signature is a PKCS#7 signature, then the signature itself will hold the data, signature and the certificate. Assuming PKCS#7 is not detached.

You need to ask your signer, how is he transferring his certificate for validation.

Upvotes: 1

noquery
noquery

Reputation: 1945

To extract detail from certificate:

  1. Make a string which keeps certificate data. Just ensure it has -----BEGIN CERTIFICATE----- in starting and -----END CERTIFICATE----- in end.
  2. Now use the following code in Java to extract certificate detail.

InputStream inStream = new ByteArrayInputStream(certString.toString().getBytes("UTF-8"));
BufferedInputStream bis = new BufferedInputStream(inStream);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(bis);
X509Certificate xCert = (X509Certificate)cert;

System.out.println("Certificate Type: "+cert.getType());
System.out.println("Public Key: \n"+cert.getPublicKey());
try{
      System.out.println("Signature Algorithm"+xCert.getSigAlgName());
      System.out.println("IssuerDN : "+xCert.getIssuerDN());
      System.out.println("Serial Number : "+xCert.getSerialNumber());
      System.out.println("SubjectDN : "+xCert.getSubjectDN());
}catch(Exception exp){
      :
}

Upvotes: 4

Related Questions