C# RSA加密

发布时间 2023-07-31 18:33:23作者: 以至千里

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

byte[] cipherbytes;

RsaKeyParameters publickeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(spk));//spk为公钥

string XML = string.Format("<RSAKeyValue><Modulus>{0]</Modulus><Exponent>{1}</Exponent><RSAKeyValue>",

Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),

Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));

rsa.FromXmlString(XML);

cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(userid),false);//userid为加密内容

string data = Convert.ToBase64String(cipherbytes);//加密后参数