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. Jdk5.0新特性

    增强for循环:foreach语句,foreach简化了迭代器. 格式:// 增强for循环括号里写两个参数,第一个是声明一个变量,第二个就是需要迭代的容器 for( 元素类型 变量名 : Colle ...

  2. mac上的键盘生活——快捷键列表

      主界面 command + tab 切换程序 command + `   在程序内切换界面 command + w     关闭界面 command + q     关闭程序   文本编辑 Com ...

  3. 为xampp 安装pear db (database) 模块

    pear channel-update pear.php.netpear install db

  4. Java Executor 框架学习总结

    大多数并发都是通过任务执行的方式来实现的.一般有两种方式执行任务:串行和并行. class SingleThreadWebServer { public static void main(String ...

  5. Centos 下安装MongoDB

    Centos 下安装MongoDB 一.安装方法 方法(一) 1  配置包管理系统 创建/etc/yum.repos.d/mongodb.repo 文件,当然我们使用的是64位系统,32位的情况不再考 ...

  6. [RxJS] Combination operator: withLatestFrom

    Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...

  7. leetcode-1 Two Sum 找到数组中两数字和为指定和

     问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值.如果数组中肯定存在至少一组满足要求. <剑指Offer>P214(有序数组) <编程之美& ...

  8. Apache让一台虚拟主机接受多域名解析(转)

    之前写了一篇文章关于linux下apache虚拟主机配置,配置那是相当简单: <VirtualHost *:80> ServerAdmin admin@example.com Docume ...

  9. LINUX 内核调试基础+编程基础

    http://blog.chinaunix.net/uid-20564848-id-73208.html 内核文档:[root@localhost Documentation]# pwd /usr/s ...

  10. iOS--inputView和inputAccessoryView

    iOS–inputView和inputAccessoryView 什么是inputView和inputAccessoryView? 如果是UITextField和UITextView,下面是声明文件源 ...