问题描述
- C#中怎么使用.cer证书中的公钥进行AES加密?
-
最近要对接中行的支付接口, 那边给出的接口是用pfx证书中的私钥进行签名以后,再使用cer证书中的公钥加密;
这个加密的秘钥要怎么读取出来?
我现在这样写:
X509Certificate2 pubcrt = new X509Certificate2(cer);
RSACryptoServiceProvider pubkey = (RSACryptoServiceProvider)pubcrt.PublicKey.Key;
Rijndael rd = Rijndael.Create();
rd.Key = Encoding.UTF8.GetBytes(pubkey.ToXmlString(false));报出的错误是:指定键的大小对于此算法无效。 求救啊 急急急!!!!
解决方案
http://blog.csdn.net/zj510/article/details/39964533
解决方案二:
这个是RSA加密吧? 我需要使用证书里面的公钥 ,进行**AES**加密
解决方案三:
这个是反编译出来的java示例
public Document encrypt(Document node, boolean keyInfoFlag, byte[] key)
throws Exception
{
if (this.mode != 1)
throw new IllegalStateException("call a XMLEDTool instance not for encrypt.");
Key skey;
if (key != null) {
skey = new SecretKeySpec(key, this.algorithm); //这里的 this.algorithm 是AES
}
else
{
Key skey;
if (keyInfoFlag) {
KeyGenerator keyGenerator = KeyGenerator.getInstance(this.algorithm);
skey = keyGenerator.generateKey();
}
else {
throw new IllegalArgumentException("Key is null.");
}
}
Key skey;
XMLCipher xmlCipher = XMLCipher.getInstance(this.xmlAlgorithm);
xmlCipher.init(1, skey);
if (keyInfoFlag) {
XMLCipher keyCipher = XMLCipher.getInstance(this.xmlWrapAlgorithm);
keyCipher.init(3, this.wrapKey);
EncryptedKey encryptedKey = keyCipher.encryptKey(node, skey);
EncryptedData encryptedData = xmlCipher.getEncryptedData();
KeyInfo keyInfo = new KeyInfo(node);
keyInfo.add(encryptedKey);
encryptedData.setKeyInfo(keyInfo);
}
return xmlCipher.doFinal(node, node.getDocumentElement(), true);
}
怎么把这个改写成C#???
时间: 2024-09-26 11:36:49