(C#基础)各种加密学习
之前,一直想对这个做一个了解,但是总是坚持不下去,很急躁。最近看了几遍文章,很有感触,于是又来重新开始学习,从最最基础的开始——正所谓“慢就是快”。心态变了,继续吧!上代码!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography; namespace dazilianxi.wenjian
{
public class RSACryptoHelper
{
//加密
public static string Encrypt(string publicKeyXml, string plainText)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(publicKeyXml); //使用公匙初始化对象
byte[] plainData = Encoding.Default.GetBytes(plainText);
byte[] encryptedData = provider.Encrypt(plainData, true);
return Convert.ToBase64String(encryptedData);
} //解密
public static string Decrypt(string privateKeyXml, string encryptedText)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKeyXml);
byte[] encryptedData = Convert.FromBase64String(encryptedText);
byte[] plainData = provider.Decrypt(encryptedData, true);
string plainText = Encoding.Default.GetString(plainData);
return plainText;
}
public static KeyValuePair<string, string> CreateRSAKey()
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
string privateKey = RSA.ToXmlString(true);
string publicKey = RSA.ToXmlString(false); return new KeyValuePair<string, string>(publicKey, privateKey);
} public static string SignData(string plainText, string privateKeyXml)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKeyXml); byte[] plainData = Encoding.Default.GetBytes(plainText);
//设置获取摘要的算法
HashAlgorithm sha1 = HashAlgorithm.Create("SHA1");
//获取签名过的摘要,是使用私匙加密过的摘要
byte[] signedDigest = provider.SignData(plainData, sha1);
return Convert.ToBase64String(signedDigest);
} public static bool VerifyData(string plainText, string signature, string publicKeyXml)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(publicKeyXml); byte[] plainData = Encoding.Default.GetBytes(plainText);
byte[] signedDigest = Convert.FromBase64String(signature); HashAlgorithm sha1 = HashAlgorithm.Create("SHA1");
bool isDataIntact = provider.VerifyData(plainData, sha1, signedDigest);
return isDataIntact;
} //使用SingnHash
public static string SignData2(string plainText, string privateKeyXml)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKeyXml);
byte[] plainData = Encoding.Default.GetBytes(plainText); //设置获取摘要的算法
HashAlgorithm sha1 = HashAlgorithm.Create("SHA1");
//获得原始摘要
byte[] digestData = sha1.ComputeHash(plainData);
//对元素摘要进行签名
byte[] signedDigest = provider.SignHash(digestData, "SHA1");
return Convert.ToBase64String(signedDigest);
} //使用VerifyHash
public static bool VerifyData2(string plainText, string signedDigest, string publicKeyXml)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(publicKeyXml); byte[] plainData = Encoding.Default.GetBytes(plainText);
byte[] signedDigestData = Convert.FromBase64String(signedDigest); //获得本地摘要
HashAlgorithm sha1 = HashAlgorithm.Create("SHA1");
byte[] digest = sha1.ComputeHash(plainData); //解密签名
bool isDataIntact = provider.VerifyHash(digest, "SHA1", signedDigestData);
return isDataIntact;
}
}
}
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
string publicPrivate = provider.ToXmlString(true);//获得公/私匙对
// string publicOnly = provider.ToXmlString(false); //只获得公匙
// string key = "secret key";
string str = "hello world";
// string str2 = "hello world";
//加密
string encryptedText = RSACryptoHelper.Encrypt(publicPrivate, str);
Console.WriteLine(encryptedText);
//学到了重构,this 在构造函数中的灵活运用。不能直接满足条件,就要想方设法创造合适的。
//解密
string clearText = RSACryptoHelper.Decrypt(publicPrivate, encryptedText);
Console.WriteLine(clearText); string originalData = "文章不错,这是我的签名:奥巴马!";
Console.WriteLine("签名数为:{0}", originalData); KeyValuePair<string, string> keyPair =RSACryptoHelper.CreateRSAKey();
string privateKey = keyPair.Value;
string publicKey = keyPair.Key;
//1、生成签名,通过摘要算法
string signedData = RSACryptoHelper.SignData(originalData, privateKey);
Console.WriteLine("数字签名1:{0}", signedData); //2、验证签名
bool verify = RSACryptoHelper.VerifyData(originalData, signedData, publicKey);
Console.WriteLine("签名验证结果:{0}", verify); string signedData2 = RSACryptoHelper.SignData2(originalData, privateKey);
Console.WriteLine("数字签名2:{0}", signedData2); //2、验证签名
bool verify2 = RSACryptoHelper.VerifyData2(originalData, signedData2, publicKey);
Console.WriteLine("签名验证结果2:{0}", verify2);
参考,延伸学习:
数据加密:http://www.cnblogs.com/yank/p/3528548.html
数字签名:http://www.cnblogs.com/yank/p/3533998.html
证书:http://www.cnblogs.com/Microshaoft/archive/2009/05/19/1460641.html
加密:http://www.cnblogs.com/Microshaoft/archive/2008/07/21/1247584.html
加密:http://www.cnblogs.com/darrenji/p/3677458.html
(C#基础)各种加密学习的更多相关文章
- 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解
salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解 建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schem ...
- 零基础如何系统学习Java Web
零基础如何系统学习Java Web? 我来给你说一说 你要下决心,我要转行做开发,这样你才能学成. 你要会打字,我公司原来有一个程序员,打字都是两个手一指禅,身为程序员你一指禅怎么写出的代码,半个 ...
- ASP.NET基础之HttpHandler学习
ASP.NET基础之HttpHandler学习 经过前两篇[ASP.NET基础之HttpModule学习]和[ASP.NET基础之HttpContext学习]文章的学习我们对ASP.NET的基础内容有 ...
- C#区块链零基础入门,学习路线图 转
C#区块链零基础入门,学习路线图 一.1分钟短视频<区块链100问>了解区块链基本概念 http://tech.sina.com.cn/zt_d/blockchain_100/ 二.C#区 ...
- (转)ASP.NET基础之HttpHandler学习
原文地址:http://www.cnblogs.com/wujy/archive/2013/08/18/3266009.html 经过前两篇[ASP.NET基础之HttpModule学习]和[ASP. ...
- (转) HTTP & HTTPS网络协议重点总结(基于SSL/TLS的握手、TCP/IP协议基础、加密学)
HTTP & HTTPS网络协议重点总结(基于SSL/TLS的握手.TCP/IP协议基础.加密学) 原文:http://blog.csdn.net/itermeng/article/detai ...
- (转)零基础入门深度学习(6) - 长短时记忆网络(LSTM)
无论即将到来的是大数据时代还是人工智能时代,亦或是传统行业使用人工智能在云上处理大数据的时代,作为一个有理想有追求的程序员,不懂深度学习(Deep Learning)这个超热的技术,会不会感觉马上就o ...
- 【转载】salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建 VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的 ...
- 【转载】salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable
salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable 本篇知识参考:https://developer.salesforce.com/trailhead/for ...
随机推荐
- poj Meteor Shower - 搜索
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16313 Accepted: 4291 Description Bess ...
- linux下命令行工具gcp显示拷贝进度条
1.环境: ubuntu16.04 Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017 x86_64 x ...
- 1-20 RHEL7的启动原理和服务控制
大纲: RHEL7启动原理 RHEL7服务启动配置 网络概述 发布内网服务器 ############################################################ ...
- 第五章 消息摘要算法--MAC
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第6章“验证数据完整性--消息摘要算法” 5.1.mac(又称为Hmac) 原理:在md与sha系列算法的基础上加入了密钥,是 ...
- 使用PDFminer3k解析pdf为文字遇到:WARING:root:GBK-EUC-H
最近需要把PDF解析为文字,查了查python的模块,发现PDFminer3k能满足需求.我使用的是 windows平台下的python3.6,python2的则下载pdfminer. 首先下载:直接 ...
- 使用Docfx生成项目文档
使用docfx.console生成本项目的文档 使用docfx.console生成其他项目的文档 直接使用docfx.exe生成项目文档 指定配置文档模板 文档地址:http://gitlab.l ...
- python 函数返回函数
def hi(name="yasoob"): def greet(): return "now you are in the greet() function" ...
- Java线程池队列吃的太饱,撑着了咋整?java 队列过大导致内存溢出
Java的Executors框架提供的定长线程池内部默认使用LinkedBlockingQueue作为任务的容器,这个队列是没有限定大小的,可以无限向里面submit任务. 当线程池处理的太慢的时候, ...
- Python 运算符重载
https://www.cnblogs.com/hotbaby/p/4913363.html
- 源代码方式调试Mycat
如果是第一次刚接触MyCat建议下载源码在本地通过eclipse等工具进行配置和运行,便于深入了解和调试程序运行逻辑. 1)源代码方式调试与配置 由于MyCat源代码目前主要托管在github上,大家 ...