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加密 ...
随机推荐
- 低压差稳压器AMS1585
(1)高效线性稳压. (2)输出高达4.6A,最高输入电压15V,推荐最低压差1.5V(最低1.35V),最大压差12V. (3)两种封装:TO220(直插式),TO230(贴片). 典型电路如下图所 ...
- IronPython fail to add reference to WebDriver.dll
在使用Ironpython引用WebDriver程序集做web自动化时碰到这个问题,出问题的代码很简单,如下: import sys import clr clr.AddReferenceToFile ...
- 基础排序算法之快速排序(Quick Sort)
快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...
- 关键字 final
package com.zyw.reusableClass; import java.util.Random; /** * Created by zyw on 2016/3/26. * from th ...
- poj 3465 Corn Fields 状态压缩
题目链接:http://poj.org/problem?id=3254 #include <cstdio> #include <cstring> #include <io ...
- asterisk 能打电话的配置
若使用的是chan_sip.so模块,则在sip.conf里添加: [10]type=friend;context=phonescontext=publichost=dynamicsecret=123 ...
- JVM内存管理和JVM垃圾回收机制
JVM内存管理和JVM垃圾回收机制(1) 这里向大家描述一下JVM学习笔记之JVM内存管理和JVM垃圾回收的概念,JVM内存结构由堆.栈.本地方法栈.方法区等部分组成,另外JVM分别对新生代和旧生代采 ...
- 【设计模式 - 15】之解释器模式(Interpreter)
1 模式简介 解释器模式允许我们自定义一种语言,并定义一个这种语言的解释器,这个解释器用来解释语言中的句子.由于这种模式主要用于编译器的编写,因此在日常应用中不是很常用. 如果一种特定类型的 ...
- springframework hibernate Transaction not successfully started
先贴出错误:org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transac ...
- [Reactive Programming] Async requests and responses in RxJS
We will learn how to perform network requests to a backend using RxJS Observables. A example of basi ...