110,567
社区成员
发帖
与我相关
我的任务
分享
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
/// <summary>
/// RSA 私钥加密
/// </summary>
/// <param name="data">数据</param>
/// <param name="key">私钥</param>
/// <returns></returns>
public byte[] RSAEncryptByPrivate(byte[] data, byte[] key)
{
try
{
//java: PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
//java: KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//java: PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
AsymmetricKeyParameter Privatekey = PrivateKeyFactory.CreateKey(key);
//java: Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
IBufferedCipher c = CipherUtilities.GetCipher("RSA");
//java: cipher.init(Cipher.ENCRYPT_MODE, privateKey);
c.Init(true, Privatekey);
//java: return cipher.doFinal(data);
return c.DoFinal(data);
}
catch
{
return null;
}
}
public static byte[] encryptByPrivateKey(byte data[], byte key[])
throws Exception
{
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
java.security.PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(1, privateKey);
return cipher.doFinal(data);
}