C# RSA
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的更多相关文章
- “不给力啊,老湿!”:RSA加密与破解
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 加密和解密是自古就有技术了.经常看到侦探电影的桥段,勇敢又机智的主角,拿着一长串毫 ...
- .NET 对接JAVA 使用Modulus,Exponent RSA 加密
最近有一个工作是需要把数据用RSA发送给Java 虽然一开始标准公钥 net和Java RSA填充的一些算法不一样 但是后来这个坑也补的差不多了 具体可以参考 http://www.cnblogs. ...
- [C#] 简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Wen. ...
- PHP的学习--RSA加密解密
PHP服务端与客户端交互或者提供开放API时,通常需要对敏感的数据进行加密,这时候rsa非对称加密就能派上用处了. 举个通俗易懂的例子,假设我们再登录一个网站,发送账号和密码,请求被拦截了. 密码没加 ...
- RSA非对称加密,使用OpenSSL生成证书,iOS加密,java解密
最近换了一份工作,工作了大概一个多月了吧.差不多得有两个月没有更新博客了吧.在新公司自己写了一个iOS的比较通用的可以架构一个中型应用的不算是框架的一个结构,并已经投入使用.哈哈 说说文章标题的相关的 ...
- RSA算法
RSA.h #ifndef _RSA_H #define _RSA_H #include<stdio.h> #include<iostream> #include<mat ...
- 信息安全-5:RSA算法详解(已编程实现)[原创]
转发注明出处:http://www.cnblogs.com/0zcl/p/6120389.html 背景介绍 1976年以前,所有的加密方法都是同一种模式: (1)甲方选择某一种加密规则,对信息进行加 ...
- .net(c#)版RSA加密算法,拿走不谢
今天有同学对接一个支付平台,涉及到RSA的签名和验签.由于对方是java的sdk,翻成c#语言时,搞了半天也没搞定.网上搜的东西都是各种copy还不解决问题. 碰巧,我之前对接过连连银通的网银支付和代 ...
- 4、DES和RSA简介
DES是分组加密算法,速度快,使用单一密钥,加密解密都使用同一个密钥,一般用于大量数据加密,目前处于半淘汰状态. RSA算法是流式加密算法,速度慢,但是使用成对的密钥,加密解密使用不同的密钥,有利于保 ...
- Android数据加密之Rsa加密
前言: 最近无意中和同事交流数据安全传输的问题,想起自己曾经使用过的Rsa非对称加密算法,闲下来总结一下. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes加密 ...
随机推荐
- Android protectionLevel
Android protectionLevel分4个级别: "normal" "dangerous" "signature" "s ...
- Linux学习笔记10——文件I/O之一
UNIX系统中的大多数文件I/O只需要用到5个函数:open,read,write,lseek以及close 文件描述符 文件描述符是一个非负整数,所有打开的文件都通过文件描述符引用 文件描述符的变化 ...
- C++引用(Reference)
引用(Reference)是C++语言相对于C语言的又一个扩充,类似于指针,只是在声明的时候用&取代了*.引用可以看做是被引用对象的一个别名,在声明引用时,必须同时对其进行初始化.引用的声明方 ...
- 用Objective-C的Category特性添加类的属性
http://www.cnblogs.com/wupher/archive/2013/01/05/2845338.html Category是Objective-C中常用的语法特性,通过它可以很方便的 ...
- Using Apache2 with JBoss AS7 on Ubuntu
大体思路同<Using Apache Web Server with Jboss AS 7>一致,但在Ubuntu上的操作与之前有些区别. 这里仍然演示mod_proxy的配置. 首先加载 ...
- 【转】SVN linux命令及 windows相关操作(二)
转自这里:http://www.uml.org.cn/pzgl/200904246.asp 1 安装及下载client 端 2 什么是SVN(Subversion)? 3 为甚么要用SVN? 4 怎么 ...
- HDU 1502 Regular Words DP+高精度
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1502 题目大意:找出总的满足条件的字符串数,num(a)=num(b)=num(c)且任何前缀均满足n ...
- tomacat 配置ssl协议
1.首先用jdk自带的工具keytool生成一个"服务器证书" a.命令行进入$JAVA_HOME/bin目录($JAVA_HOME为jdk的安装目录) b.输入:keytool ...
- winfrom 截屏、抓屏 分类: WinForm 2014-08-01 13:02 198人阅读 评论(0) 收藏
截取全屏代码: try { this.Hide(); Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty)); Bitma ...
- java中经常使用的日期格式化(全)
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...