XML签名以在Android中使用.cer(公钥)进行验证/验证

时间:2020-07-29 11:48:30

标签: android certificate x509certificate public-key xml-signature

我需要一些帮助来验证对.cer(公共密钥)的XML签名。

基本上,此过程是针对政府签发的身份证进行离线Ekyc验证的。

有关XML和.cert文件的示例可在下面的链接中找到, 1。离线Ekyc示例数据部分。此外,他们还提供了.net语言的代码。

并且我尝试在android上应用相同的功能,但它始终会提供错误的验证。您能帮我如何将示例代码转换为android吗?

参考链接: https://www.uidai.gov.in/ecosystem/authentication-devices-documents/developer-section/915-developer-section/tutorial-section.html

我已在android中应用了以下代码

public boolean verifySampleData(String SignatureValue,InputStream publicKeyCertificate){
    X509Certificate cert = createCert(publicKeyCertificate);
    java.security.Signature signat;
    boolean ret=false;
    try {
        signat = java.security.Signature.getInstance("SHA256withRSA");
        signat.initVerify(cert.getPublicKey());

        byte[] s= readSignature(SignatureValue);
        ret = signat.verify(s,0,s.length);
        return ret;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ret;
}
public static X509Certificate createCert (InputStream certStream) {
    X509Certificate certret = null;
    CertificateFactory cf = null;
    try {
        cf = CertificateFactory.getInstance("X.509");
        certret = (X509Certificate) cf.generateCertificate(certStream);
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return certret;
}

byte[] readSignature(String input) throws Exception {
    byte[] data = input.getBytes("UTF-8");
    return Base64.encode(data, Base64.DEFAULT);
}

0 个答案:

没有答案