RSA 属不对称加密,使用一个公钥一个私钥,公钥可以公开用以加密,私钥严格保密用于解密,RSA 适合于数据量不大的加密,比如加密对称加密的密钥。
RSACryptoServiceProvider 的名称空间是:
System.Security.Cryptography
RSACryptoServiceProvider rsaSend = new RSACryptoServiceProvider(); string plaintext = "明文"; //明文 byte[] ciphertext = rsaSend.Encrypt(System.Text.Encoding.UTF8.GetBytes(plaintext), false); //加密后 lbl.Text = Convert.ToBase64String(ciphertext); //显示加密后的,为了显示不可见字符,使用的是 Base64 编码。
使用 RSACryptoServiceProvider() 创建 RSACryptoServiceProvider 实例时,自动产生密钥。
重要
RSA 在使用公钥加密时,即使每次明文一样、公钥一样,密文都是不一样的。
私钥签名则不存在上述问题。