zoukankan      html  css  js  c++  java
  • 用C#实现RSA加密的一个小例子

    整个算法的流程是:
    接收方先同时生成公钥和私钥, 再把公钥传递给发送方, 发送方收到公钥后, 用此公钥将自己的明文加密, 然后将加密后的密文传递给接收方, 接收方用自己的私钥解密得到明文. 以下是演示这个过程的示例代码:
     

               

     //待加密的明文
                string originText="Hello";
                
    //公钥
                string publicKey;

                System.Security.Cryptography.RSACryptoServiceProvider rsaReceive 
    = 
                    
    new System.Security.Cryptography.RSACryptoServiceProvider();
                System.Security.Cryptography.RSACryptoServiceProvider rsaSend 
    =
                    
    new System.Security.Cryptography.RSACryptoServiceProvider();

                
    //接收方先生成公钥, 并将此公钥公开
                
    //参数false 表示只生成公钥, 如果为true, 则同时生成公钥和私钥.
                publicKey = rsaReceive.ToXmlString(false);
                
    //发送方接收公钥, 并用此公钥加密数据
                rsaSend.FromXmlString(publicKey);

                
    //发送方执行加密.
                
    //第二个参数指示是否使用OAEP, 如果使用, 则程序必须运行在Windows XP 及以上版本的
                
    //系统中. 无论true 或false, 解密时必须跟加密时的选择相同. 
                byte[] cryp = rsaSend.Encrypt(System.Text.Encoding.UTF8.GetBytes(originText),false);
                
    //接收方用自己的私钥解密
                byte[] b_OriginText = rsaReceive.Decrypt(cryp, false);
  • 相关阅读:
    python 连接ubuntu xampp mysql
    [解决] win7能上网,ubuntu14.04不行
    ubuntu14.04 安装 pyv8
    QT_QMAKE_EXECUTABLE reported QT_INSTALL_LIBS as /usr/lib/i386-linux-gnu but ...
    网站运营思想
    织梦直接往数据库写入数据
    [xunsearch] 在thinkphp中使用xunsearch
    [xampp] phpmyadmin 设置登录密码
    [centos6.5] 把xampp的htdocs改为其他目录
    [ubuntu] service apache2 restart [fail]
  • 原文地址:https://www.cnblogs.com/Moosdau/p/908852.html
Copyright © 2011-2022 走看看