无法使用RSA验证SHA1签名

时间:2017-03-09 20:09:33

标签: java c# x509certificate sha1

我正在尝试在Java中验证证书签名,但它失败了。我想SHA1和SHA1withRSA之间存在差异,但我该如何解决?

这是使用C#登录的代码(证书是mycer.p12):

static string Sign(string token, string cer_name, string passw)
        {
            try
            {
                if (!File.Exists(cer_name))
                {
                    MessageBox.Show("File not found " + cer_name);
                    return "";
                }
                X509Certificate2 cert = new X509Certificate2(cer_name, passw, X509KeyStorageFlags.Exportable);
                RSACryptoServiceProvider csp = null;
                csp = (RSACryptoServiceProvider)cert.PrivateKey;
                if (csp == null)
                {
                    MessageBox.Show("No valid cert was found");
                    return "";
                }
                Encoding encoding = Encoding.GetEncoding("UTF-8");
                byte[] data = encoding.GetBytes(token);
                RSACryptoServiceProvider rsaClear = new RSACryptoServiceProvider();
                rsaClear.ImportParameters(csp.ExportParameters(true));
                byte[] signature = rsaClear.SignData(data, CryptoConfig.MapNameToOID("SHA1"));
                bool isValid = csp.VerifyData(data, "SHA1", signature);
                if (isValid) return Convert.ToBase64String(signature).Trim(new char[] { '\0', '\n' }).Replace("\n", "");
                else
                {
                    MessageBox.Show("Siganture verification = FALSE");
                    return "";
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return "";
            }
        }

我使用此命令提取公钥

  

openssl pkcs12 -in mycer.p12 -clcerts -nokeys -out publicKEY.pem

现在用Java验证这段代码,但它返回false:

 public static boolean verify(String plainText, String signature, String publicKeyFile) {
            try {
CertificateFactory f = CertificateFactory.getInstance("X.509");
        X509Certificate certificate = (X509Certificate)f.generateCertificate(new FileInputStream(publicKeyFile));
                Signature publicSignature = Signature.getInstance("SHA1withRSA");
                publicSignature.initVerify(publicKey);
                publicSignature.update(plainText.getBytes("UTF-8"));
                byte[] signatureBytes = Base64.decode(signature);
                return publicSignature.verify(signatureBytes);
            } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | UnsupportedEncodingException e) {
                e.printStackTrace();
                return false;
            }
        }

1 个答案:

答案 0 :(得分:0)

这篇文章帮助了我https://stackoverflow.com/a/16980246/1444413

KeyStore p12 = KeyStore.getInstance("pkcs12");
p12.load(new FileInputStream("mycer.p12"), "password".toCharArray());
String alias = (String) p12.aliases().nextElement();
X509Certificate c = (X509Certificate) p12.getCertificate(alias);
.... c.getPublicKey() ....

然后方法verify()..返回true。