将java方法转换为C#.NET(verifysign) [英] Translate java method to C#.NET (verifysign)
问题描述
你好
我有一个应用程序用文件.cer和.key签名,但当我尝试用证书文件验证标志时
这是Java中的代码
public static String verifySign(String cerPath,String toVerify,String sign){
字符串resultado = null;
布尔值blnResultado = false;
try(InputStream cer = new FileInputStream(new File(cerPath))){
CertificateFactory cf = CertificateFactory.getInstance(X.509);
证书证书=(X509Certificate)cf.generateCertificates(cer).iterator()。next();
final签名签名= Signature.getInstance(SHA256withRSA);
signature.initVerify(cert.getPublicKey());
signature.update(toVerify.getBytes(UTF-8));
blnResultado = signature.verify(Base64.decodeBase64(sign。 getBytes(UTF-8)));
} catch(异常) e){
e.printStackTrace();
}
if(blnResultado.equals(布尔。 FALSE)){
resultado = new String(Firma Incorrecta);
} else {
resultado = new String(Firma Correcta );
}
返回结果;
}
我尝试过:
我尝试在C#中做同样的事情,我试图找到其他方法来验证我的应用程序生成的签名。
Hello
I have an application to sign with file .cer and .key, but when I try to validate the sign with only certificate file
This is the code in Java
public static String verifySign(String cerPath, String toVerify, String sign) {
String resultado = null;
Boolean blnResultado = false;
try (InputStream cer = new FileInputStream(new File(cerPath))) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = (X509Certificate) cf.generateCertificates(cer).iterator().next();
final Signature signature = Signature.getInstance("SHA256withRSA");
signature.initVerify(cert.getPublicKey());
signature.update(toVerify.getBytes("UTF-8"));
blnResultado = signature.verify(Base64.decodeBase64(sign.getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
if(blnResultado.equals(Boolean.FALSE)) {
resultado = new String("Firma Incorrecta");
} else {
resultado = new String("Firma Correcta");
}
return resultado;
}
What I have tried:
I try to do the same thing in C# and I try to find other ways to verify signature generated by my application.
推荐答案
System.Security.Cryptography.X509Certificates命名空间| Microsoft Docs [ ^ ]
这篇关于将java方法转换为C#.NET(verifysign)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!