using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{

static void Main()
    {
       
try
       
{
           
string str_Plain_Text = "How are you?How are you?How are you?How
are you?=-popopolA";
           
Console.WriteLine("明文:" + str_Plain_Text);
           
Console.WriteLine("长度:" +
str_Plain_Text.Length.ToString());
           
Console.WriteLine();

RSACryptoServiceProvider RSA = new
RSACryptoServiceProvider();

string str_Public_Key;
           
string str_Private_Key;
           
string str_Cypher_Text = RSA_Encrypt(str_Plain_Text, out
str_Public_Key,out str_Private_Key);
           
Console.WriteLine("密文:" + str_Cypher_Text);
           
Console.WriteLine("公钥:" + str_Public_Key);
           
Console.WriteLine("私钥:" + str_Private_Key);

string str_Plain_Text2 = RSA_Decrypt(str_Cypher_Text,
str_Private_Key);
           
Console.WriteLine("解密:" + str_Plain_Text2);

Console.WriteLine();
       
}
       
catch (ArgumentNullException)
       
{
           
Console.WriteLine("Encryption failed.");
       
}
    }

//RSA加密,随机生成公私钥对并作为出参返回
    static
public string RSA_Encrypt(string str_Plain_Text, out string
str_Public_Key, out string str_Private_Key)
    {
       
str_Public_Key = "";
       
str_Private_Key = "";
       
UnicodeEncoding ByteConverter = new UnicodeEncoding();
       
byte[] DataToEncrypt =
ByteConverter.GetBytes(str_Plain_Text);
       
try
       
{
           
RSACryptoServiceProvider RSA = new
RSACryptoServiceProvider();
           
str_Public_Key =
Convert.ToBase64String(RSA.ExportCspBlob(false));
           
str_Private_Key =
Convert.ToBase64String(RSA.ExportCspBlob(true));

//OAEP padding is only available on Microsoft Windows XP or
later. 
           
byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
           
str_Public_Key =
Convert.ToBase64String(RSA.ExportCspBlob(false));
           
str_Private_Key =
Convert.ToBase64String(RSA.ExportCspBlob(true));
           
string str_Cypher_Text =
Convert.ToBase64String(bytes_Cypher_Text);
           
return str_Cypher_Text;
       
}
       
catch (CryptographicException e)
       
{
           
Console.WriteLine(e.Message);
           
return null;
       
}
    }

//RSA解密
    static
public string RSA_Decrypt(string str_Cypher_Text, string
str_Private_Key)
    {
       
byte[] DataToDecrypt =
Convert.FromBase64String(str_Cypher_Text);
       
try
       
{
           
RSACryptoServiceProvider RSA = new
RSACryptoServiceProvider();
           
//RSA.ImportParameters(RSAKeyInfo);
           
byte[] bytes_Public_Key =
Convert.FromBase64String(str_Private_Key);
           
RSA.ImportCspBlob(bytes_Public_Key);

//OAEP padding is only available on Microsoft Windows XP or
later. 
           
byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
           
UnicodeEncoding ByteConverter = new UnicodeEncoding();
           
string str_Plain_Text =
ByteConverter.GetString(bytes_Plain_Text);
           
return str_Plain_Text;
       
}
       
catch (CryptographicException e)
       
{
           
Console.WriteLine(e.ToString());
           
return null;
       
}
    }
}

C# RSA的更多相关文章

  1. “不给力啊,老湿!”:RSA加密与破解

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 加密和解密是自古就有技术了.经常看到侦探电影的桥段,勇敢又机智的主角,拿着一长串毫 ...

  2. .NET 对接JAVA 使用Modulus,Exponent RSA 加密

    最近有一个工作是需要把数据用RSA发送给Java 虽然一开始标准公钥 net和Java  RSA填充的一些算法不一样 但是后来这个坑也补的差不多了 具体可以参考 http://www.cnblogs. ...

  3. [C#] 简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Wen. ...

  4. PHP的学习--RSA加密解密

    PHP服务端与客户端交互或者提供开放API时,通常需要对敏感的数据进行加密,这时候rsa非对称加密就能派上用处了. 举个通俗易懂的例子,假设我们再登录一个网站,发送账号和密码,请求被拦截了. 密码没加 ...

  5. RSA非对称加密,使用OpenSSL生成证书,iOS加密,java解密

    最近换了一份工作,工作了大概一个多月了吧.差不多得有两个月没有更新博客了吧.在新公司自己写了一个iOS的比较通用的可以架构一个中型应用的不算是框架的一个结构,并已经投入使用.哈哈 说说文章标题的相关的 ...

  6. RSA算法

    RSA.h #ifndef _RSA_H #define _RSA_H #include<stdio.h> #include<iostream> #include<mat ...

  7. 信息安全-5:RSA算法详解(已编程实现)[原创]

    转发注明出处:http://www.cnblogs.com/0zcl/p/6120389.html 背景介绍 1976年以前,所有的加密方法都是同一种模式: (1)甲方选择某一种加密规则,对信息进行加 ...

  8. .net(c#)版RSA加密算法,拿走不谢

    今天有同学对接一个支付平台,涉及到RSA的签名和验签.由于对方是java的sdk,翻成c#语言时,搞了半天也没搞定.网上搜的东西都是各种copy还不解决问题. 碰巧,我之前对接过连连银通的网银支付和代 ...

  9. 4、DES和RSA简介

    DES是分组加密算法,速度快,使用单一密钥,加密解密都使用同一个密钥,一般用于大量数据加密,目前处于半淘汰状态. RSA算法是流式加密算法,速度慢,但是使用成对的密钥,加密解密使用不同的密钥,有利于保 ...

  10. Android数据加密之Rsa加密

    前言: 最近无意中和同事交流数据安全传输的问题,想起自己曾经使用过的Rsa非对称加密算法,闲下来总结一下. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes加密 ...

随机推荐

  1. Windows玩转Docker(二):运行whalesay image

    docker官网site:http://www.docker.com/ 参照site:https://docs.docker.com/windows/step_three/ docker安装参照: h ...

  2. Top 10 Java Debugging Tips with Eclipse

    In this tutorial we will see about debugging java applications using Eclipse. Debugging helps us to ...

  3. python 2 处理HTTP 请求的包

    httplib httplib: https://docs.python.org/2/library/httplib.html python 的官方文档这样说明: This module define ...

  4. Redis教程01——命令

    APPEND key value追加一个值到key上 AUTH password验证服务器 BGREWRITEAOF 异步重写追加文件 BGSAVE 异步保存数据集到磁盘上 BITCOUNT key ...

  5. poj 3169 Layout

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8610   Accepted: 4147 Descriptio ...

  6. 用java写随机出题

    import java.io.*; //输入函数包 public class hello{ public static void main(String args[]){ String s=" ...

  7. 【设计模式 - 17】之中介者模式(Mediator)

    1      模式简介 中介者模式的定义: 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显式地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 中介者模式中的组成部分: 1. ...

  8. C#将数据以XML格式写入Excel

    本文转载:http://www.cnblogs.com/eflylab/archive/2008/09/21/1295580.html c#将数据导入Excel另类方法 今天公司突然给个Excel模版 ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...

  10. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤 系列目录 步骤设计很重要,特别是规则的选择. 我这里分为几个规则 1.按自行 ...