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. [Python]Pip的安装以及简单的使用

    Pip的安装 安装python以后(我的python版本是32位,版本号2.7.10),如果需要安装一些其他的库,一般有两种办法,一种是自己手动去各个库的官网下载,自己安装:另一种方法是安装pip,使 ...

  2. weekend110(Hadoop)的 第六天笔记

    (2015年1月25日) 课程目录 01-复习ha相关 02-hive的元数据库mysql方式安装配置 03-hive的使用 04-hive的常用语法 05-hql语法及自定义函数 06-hbase表 ...

  3. Ubuntu 下安装opencv 编译后执行找不到库

    在ubuntu下编译opencv程序后,执行报下面到错误:error while loading shared libraries: libopencv_core.so.2.4: cannot ope ...

  4. c++重点知识点

    - const加强 在变量前加const,说明变量是常量只读属性.假如用指针去修改const常量会用什么结果.上例子: //a 是一个只读的常量,按照理论应该不能被修改 ;//内存中为a分配地址,赋值 ...

  5. c#调用钩子

    1 概述 在c++中有钩子程序,但是在C#还没有对其进行封装,所以需要自己根据实际情况调用钩子.钩子在我的理解下是,通过初始化钩子与系统中消息映射建立某种关系,当点击鼠标或者键盘,就会通过钩子中的回调 ...

  6. Away3D ATFTexture

    之前在项目中贴图大量使用了 PNG 和 jpg 遇到了个问题.在使用BitmapTexture的时候发现 是必须MIP 不管你 是否开启或者关闭 MIP 他都会去创建.而每次MIP都会根据贴图大小去生 ...

  7. HDU 1231 (13.12.2)

    Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i < ...

  8. 【2014】【辛星】【php】【秋季】【2】第一个php程序

    <span style="font-family:KaiTi_GB2312;font-size:18px;">*******************设置server** ...

  9. linux进程调度之 FIFO 和 RR 调度策略---SYSTEMTAP

    http://blog.chinaunix.net/uid-24774106-id-3379478.html http://blog.chinaunix.net/uid-24774106-id-337 ...

  10. java.sql.SQLException: Invalid parameter object type. Expected 'java.util.Map' but found 'java.lang.String 转载

    java.sql.SQLException: Invalid parameter object type. Expected 'java.util.Map' but found 'java.lang. ...