RSA公钥pem格式转XML格式并加密
格式转换
private static string RSAPublicKey(string publicKey)
{
RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
string XML = string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
return XML;
}
加密
public string PassWordEncrypt(string passWord, string strPublicKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(strPublicKey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(passWord), false);
return Convert.ToBase64String(cipherbytes);
}