网站备案ps,领动建站,餐厅装修设计公司网站,免费素材库大全RSA加密的概念 RSA加密是一种非对称加密算法。非对称加密算法使用一对密钥#xff0c;公钥#xff08;Public Key#xff09;和私钥#xff08;Private Key#xff09;。公钥可以公开发布#xff0c;用于加密数据#xff1b;私钥则由接收者自己保存#xff0c;用于解密…RSA加密的概念 RSA加密是一种非对称加密算法。非对称加密算法使用一对密钥公钥Public Key和私钥Private Key。公钥可以公开发布用于加密数据私钥则由接收者自己保存用于解密数据。这就好比有一个特殊的盒子任何人都可以用公钥这个锁将物品锁在这个盒子里但是只有拥有私钥的人才能打开这个盒子获取物品。 RSA加密算法基于大数质因数分解的困难性原理。简单来说就是将两个大素数相乘很容易但是将它们的乘积分解回原来的两个素数却非常困难。例如给你两个很大的素数p和q计算出它们的乘积n p*q很容易但是如果只是给你n要找出p和q就很困难这个特性使得RSA加密算法具有很高的安全性。
RSA加密的用法 密钥生成首先需要生成一对公钥和私钥。这一过程涉及到选择两个大素数p和q计算它们的乘积n然后计算欧拉函数φn p - 1*q - 1。接着选择一个与φn互质的整数e作为公钥的指数部分再计算出与e同余的模逆元d作为私钥的指数部分。公钥可以表示为en私钥可以表示为dn。 加密过程发送方使用接收方的公钥en对明文m进行加密。加密公式为c m^e mod n其中c是加密后的密文。 解密过程接收方使用自己的私钥dn对接收到的密文c进行解密。解密公式为m c^d mod n这样就可以得到原始的明文m。
C#代码实现 首先需要引用相关的命名空间例如 System.Security.Cryptography它提供了用于加密和解密的类。 RSA加密解密示例代码 using System;
using System.Security.Cryptography;
using System.Text;class RSACryptoExample
{static void Main(){// 创建RSA加密服务提供者using (RSA rsa RSA.Create()){// 生成密钥对// 一般 RSA 密钥大小建议使用 2048 位或更高这里为了演示简单可能使用较小的默认值// 获取公钥string publicKey rsa.ToXmlString(false); // false表示只导出公钥// 获取私钥string privateKey rsa.ToXmlString(true); // true表示导出公钥和私钥// 明文string plainText Hello RSA Encryption!;// 加密byte[] encryptedData EncryptStringToBytes_RSA(plainText, publicKey);Console.WriteLine(Encrypted Data: Convert.ToBase64String(encryptedData));// 解密string decryptedText DecryptBytesToString_RSA(encryptedData, privateKey);Console.WriteLine(Decrypted Text: decryptedText);}}// 加密方法static byte[] EncryptStringToBytes_RSA(string plainText, string publicKey){byte[] encryptedBytes null;// 使用 RSA 从公钥字符串创建加密服务提供者using (RSA rsa RSA.Create()){try{// 导入公钥rsa.FromXmlString(publicKey);// 将明文字符串转换为字节数组byte[] dataToEncrypt Encoding.UTF8.GetBytes(plainText);// 使用公钥加密数据encryptedBytes rsa.Encrypt(dataToEncrypt, RSAEncryptionPadding.OaepSHA256);}catch (CryptographicException e){Console.WriteLine(Error encrypting: {0}, e.Message);}}return encryptedBytes;}// 解密方法static string DecryptBytesToString_RSA(byte[] encryptedData, string privateKey){string decryptedText null;// 使用 RSA 从私钥字符串创建解密服务提供者using (RSA rsa RSA.Create()){try{// 导入私钥rsa.FromXmlString(privateKey);// 使用私钥解密数据byte[] decryptedBytes rsa.Decrypt(encryptedData, RSAEncryptionPadding.OaepSHA256);// 将解密后的字节数组转换为字符串decryptedText Encoding.UTF8.GetString(decryptedBytes);}catch (CryptographicException e){Console.WriteLine(Error decrypting: {0}, e.Message);}}return decryptedText;}
} 代码详细解释 在代码中首先创建了一个RSA加密服务提供者对象 RSA rsa RSA.Create()。这个对象用于生成密钥对以及进行加密和解密操作。 通过调用 rsa.ToXmlStringfalse 和 rsa.ToXmlStringtrue 分别获取公钥和私钥的XML字符串形式。这是为了方便存储和传输密钥。例如在网络通信中可以将公钥发送给发送方而私钥由接收方自己保存。 在加密方法 EncryptStringToBytes_RSA 中使用公钥对明文进行加密。首先将明文字符串转换为字节数组然后调用 rsa.Encrypt 方法传入加密数据和加密填充模式这里使用OAEPSHA256填充模式。加密填充模式是为了防止某些攻击如选择密文攻击等。加密后的结果是一个字节数组。 在解密方法 DecryptBytesToString_RSA 中使用私钥对密文进行解密。调用 rsa.Decrypt 方法传入加密后的字节数组和解密填充模式得到解密后的字节数组然后再将其转换为字符串。 在主方法中首先生成密钥对然后对明文进行加密并将加密后的数据打印出来。接着对加密后的数据进行解密并打印解密后的明文从而验证加密解密过程的正确性。 文章转载自: http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.jstggt.cn.gov.cn.jstggt.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.ywqsk.cn.gov.cn.ywqsk.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.kgtyj.cn.gov.cn.kgtyj.cn http://www.morning.nkpml.cn.gov.cn.nkpml.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.pqkgb.cn.gov.cn.pqkgb.cn http://www.morning.zdydj.cn.gov.cn.zdydj.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.ljjph.cn.gov.cn.ljjph.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.ljcjc.cn.gov.cn.ljcjc.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.plfy.cn.gov.cn.plfy.cn http://www.morning.nmtyx.cn.gov.cn.nmtyx.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.qlry.cn.gov.cn.qlry.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.kxltf.cn.gov.cn.kxltf.cn http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.gxtbn.cn.gov.cn.gxtbn.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.kflzy.cn.gov.cn.kflzy.cn http://www.morning.mlpmf.cn.gov.cn.mlpmf.cn http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn