难道CSDN上就没人会了吗?一直没有人回答

ipsoft 2008-09-26 02:31:26
x509 v3版证书 PKCS12
做验签怎么做啊。专家门救救。

下面的代码对吗?


byte[] plainByte = System.Text.Encoding.Default.GetBytes(PlainString);
byte[] paSign = Hex2byte(EncryptedString);

string certPath = Fetch.MapPath(PathProvider.BasePath + "/pingan/V_GROUP_LPMS_PARTNER_SIGN.cer");
X509Certificate2 myCert = new X509Certificate2(certPath);
RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)myCert.PublicKey.Key;
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("SHA1");
if (RSADeformatter.VerifySignature(plainByte, paSign))
{
return "The signature was verified.";
}
else
{
return "The signature was not verified.";
}


出异常信息,异常详细信息: System.Security.Cryptography.CryptographicException: 不正确的散列算法。

对方网站是用的JAVA开发的。java签名和验签操作代码如下


private static String algorithm = "SHA1withRSA";
/**
* 签名服务
*/
public String sign(String srcStr) throws Exception {

// KeyStore store = KeyStore.getInstance("JKS");
KeyStore store = KeyStore.getInstance("PKCS12");
FileInputStream stream = new FileInputStream(sslTrustStore4SSLChannel); // jks文件名
String passwd = sslTrustPassword4SSLChannel; // jks文件密码
store.load(stream, passwd.toCharArray());
// 获取jks证书别名
Enumeration en = store.aliases();
String pName = null;

while (en.hasMoreElements()) {
String n = (String) en.nextElement();
if (store.isKeyEntry(n)) {
pName = n;
}
}

// 获取证书的私钥
PrivateKey key = (PrivateKey) store.getKey(pName, passwd.toCharArray());
// 进行签名服务
Signature signature = Signature.getInstance(algorithm);
signature.initSign(key);
signature.update(srcStr.getBytes());
byte[] signedData = signature.sign();
// 将字节数组转换成HEX字符串返回

return byte2hex(signedData);
}

/**
* 进行验签服务
*/
public boolean verify(String srcStr, String hexStr) throws Exception {
// 获取指定证书的公钥
CertificateFactory certInfo = CertificateFactory.getInstance("x.509");
X509Certificate cert = (X509Certificate) certInfo
.generateCertificate(new FileInputStream(certFilePath));
PublicKey publicKey = cert.getPublicKey();
Signature sign3 = Signature.getInstance(algorithm);
sign3.initVerify(publicKey);
sign3.update(srcStr.getBytes());
// boolean success = sign3.verify(signedData);
boolean success = sign3.verify(hex2byte(hexStr));
return success;
}

public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int i = 0; i < b.length; i++) {
stmp = Integer.toHexString(b[i] & 0xFF);
if (stmp.length() == 1) {
hs += "0" + stmp;
} else {
hs += stmp;
}
}
return hs.toUpperCase();

}

public static byte[] hex2byte(String hex) throws IllegalArgumentException {
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = "" + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}


...全文
143 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
llxchen 2008-09-26
  • 打赏
  • 举报
回复
没做过,友情UP
xiang532005274 2008-09-26
  • 打赏
  • 举报
回复
我也帮忙顶一下
xiaoyu77582 2008-09-26
  • 打赏
  • 举报
回复
鉴定为分太少
ipsoft 2008-09-26
  • 打赏
  • 举报
回复
分不够,再加分。。。
flyso 2008-09-26
  • 打赏
  • 举报
回复
帮顶一下~~~~~~
caofan520 2008-09-26
  • 打赏
  • 举报
回复
问题难度大
分太少没人回答
Jarvis-Li 2008-09-26
  • 打赏
  • 举报
回复
不是没人会是会的人没看到!你把分给的高点也许就有人给你回答了
anyan1986 2008-09-26
  • 打赏
  • 举报
回复
不懂,帮顶一下
wanjianhong 2008-09-26
  • 打赏
  • 举报
回复
你这个东东见到头有点大 你应该到JAVA区去求救
顶你的小JJ
tonyze 2008-09-26
  • 打赏
  • 举报
回复
只能帮顶了
pangbin106 2008-09-26
  • 打赏
  • 举报
回复
支持一下!

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧