java签名转C#

yimi_love 2011-08-09 04:17:02
public static final String SIGN_ALGORITHMS = "SHA1WithRSA";
private static final int DEFAULT_BUFFER_SIZE = 4096;

/**
* <pre>
* 对原始数据进行签名
* </pre>
* @param content 签名内容
* @param privateKey 私钥
* @param charset 字符集
* @return
* @throws SignatureException
*/
public String doSign(String content, String privateKey, String charset)
throws SignatureException {
try {
PrivateKey priKey = this.getPrivateKeyFromPKCS8("RSA", new ByteArrayInputStream(
privateKey.getBytes()));

java.security.Signature signature = java.security.Signature
.getInstance(SIGN_ALGORITHMS);

signature.initSign(priKey);
signature.update(this.getContentBytes(content, charset));
byte[] signed = signature.sign();
return new String(Base64.encodeBase64(signed));
} catch (Exception e) {
throw new SignatureException("RSA签名[content = " + content + "; charset = " + charset
+ "]发生异常!", e);
}
}

/**
* <pre>
* 校验签名
* </pre>
* @param content 校验签名内容
* @param sign 签名字符串
* @param publicKey 公钥
* @param charset 字符集
* @return
* @throws SignatureException
*/
public boolean doCheck(String content, String sign, String publicKey, String charset)
throws SignatureException {
try {
PublicKey pubKey = this.getPublicKeyFromX509("RSA", new ByteArrayInputStream(publicKey
.getBytes()));

java.security.Signature signature = java.security.Signature
.getInstance(SIGN_ALGORITHMS);

signature.initVerify(pubKey);
signature.update(getContentBytes(content, charset));

return signature.verify(Base64.decodeBase64(sign.getBytes()));
} catch (Exception e) {
throw new SignatureException("RSA验证签名[content = " + content + "; charset = " + charset
+ "; signature = " + sign + "]发生异常!", e);
}
}

/** 将输入流中的字节码转换成私钥对象
* @param algorithm 秘钥生成算法
* @param ins 输入流
* @return 私钥对象
* @throws NoSuchAlgorithmException 非法生成算法异常
*/
private PrivateKey getPrivateKeyFromPKCS8(String algorithm, InputStream ins)
throws NoSuchAlgorithmException {
if (ins == null || StringUtils.isBlank(algorithm)) {
return null;
}

try {
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);

byte[] encodedKey = this.readText(ins).getBytes();
// 先base64解码
encodedKey = Base64.decodeBase64(encodedKey);
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
} catch (IOException ex) {
// TODO logger.error("获取私钥时发生异常:", ex);
} catch (InvalidKeySpecException ex) {
// TODO logger.error("获取私钥时发生异常:", ex);
}

return null;
}

/** 将输入流中的字节码转换成公钥对象
* @param algorithm 秘钥生成算法
* @param ins 输入流
* @return 公钥对象
* @throws NoSuchAlgorithmException 非法生成算法异常
*/
private PublicKey getPublicKeyFromX509(String algorithm, InputStream ins)
throws NoSuchAlgorithmException {
try {
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);

StringWriter writer = new StringWriter();
io(new InputStreamReader(ins), writer, -1);

byte[] encodedKey = writer.toString().getBytes();

// 先base64解码
encodedKey = Base64.decodeBase64(encodedKey);

return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
} catch (IOException ex) {
// TODO logger.error("获取公钥时发生异常:", ex);
} catch (InvalidKeySpecException ex) {
// TODO logger.error("获取公钥时发生异常:", ex);
}

return null;
}
...全文
232 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
muscle090620118 2011-08-09
  • 打赏
  • 举报
回复
不如帮我看个问题吧http://topic.csdn.net/u/20110809/14/e451ddc6-7cf6-4d86-bd41-74f4742a4970.html调用的接口java写的,需要装java虚拟机才能用
yimi_love 2011-08-09
  • 打赏
  • 举报
回复
我靠 又发了个死贴 没人回复
孟子E章 2011-08-09
  • 打赏
  • 举报
回复
.net有自己现成的类
http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.aspx
yimi_love 2011-08-09
  • 打赏
  • 举报
回复
有会的没?

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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