zoukankan
html css js c++ java
简单的使用.NET加密
对称加密算法:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
///
<summary>
/// 使用对称加密的例子
///
</summary>
class Class1
{
static
void
Main(
string[] args)
{
Class1 c
=
new Class1();
c.StartDemo();
}
public
void StartDemo()
{
//
establish symmetric algorithm
SymmetricAlgorithm sa
= Rijndael.Create();
//
key and iv
sa.GenerateKey();
//产生随机的 (32*8) 位的密钥
//
sa.GenerateIV();
//
初始向量,在ECB模式里面可以不用IV
sa.Mode
=
CipherMode.ECB;
//
块处理模式
sa.Padding
=
PaddingMode.Zeros;
//
末尾数据块的填充模式
Console.WriteLine("
密钥是:
"
);
for
(
int
i
=
0
; i
<
sa.Key.Length; i
++
)
{
Console.Write(
"
{0:X2}
"
,sa.Key[i]);
}
Console.WriteLine(
"
\n
"
);
//
establish crypto stream
MemoryStream ms
=
new MemoryStream();
CryptoStream cs
=
new CryptoStream(ms,sa.CreateEncryptor(),CryptoStreamMode.Write);
string
plaintext;
//
原始文本
byte
[] cipherbytes;
//
加密后的数据
byte
[] finalbytes;
//
解密后的数据
plaintext=
"
How are you? 这是一行文字。
";
byte
[] plainbytes
= Encoding.UTF8.GetBytes(plaintext);
Console.WriteLine(
"
原始文本是:\n{0}\n
",plaintext);
//
display plaint text byte array in hex format
Console.WriteLine(
"
原始数据是:
"
);
for
(
int
i
=
0
; i
<
plainbytes.Length; i
++
)
{
Console.Write(
"
{0:X2}
"
,plainbytes[i]);
}
Console.WriteLine(
"
\n
"
);
//
加密过程
cs.Write(plainbytes,
0, plainbytes.Length);
cs.Close();
cipherbytes
= ms.ToArray();
ms.Close();
//
display ciphertext byte array in hex format
Console.WriteLine(
"
加密后的数据是:
"
);
for
(
int
i
=
0
; i
<
cipherbytes.Length; i
++
)
{
Console.Write(
"
{0:X2}
"
,cipherbytes[i]);
}
Console.WriteLine(
"
\n
"
);
//
下面的为加密过程
ms
=
new MemoryStream(cipherbytes);
cs
=
new CryptoStream(ms,sa.CreateDecryptor(),CryptoStreamMode.Read);
finalbytes
=
new
byte[plainbytes.Length];
cs.Read(finalbytes,
0,plainbytes.Length);
Console.WriteLine(
"
解密后的数据是:
"
);
for
(
int
i
=
0
; i
<
finalbytes.Length; i
++
)
{
Console.Write(
"
{0:X2}
"
,finalbytes[i]);
}
Console.WriteLine(
"
\n
"
);
string
finaltext
=Encoding.UTF8.GetString(finalbytes);
Console.WriteLine(
"
解密后的文本是:\n{0}\n\n
",finaltext );
Console.WriteLine(
"
按任意键继续
");
Console.ReadLine();
}
}
非对称加密算法:
using
System;
using
System.IO;
using
System.Text;
using
System.Security.Cryptography;
/**/
///
<summary>
///
一个简单的使用.NET非对称加密算法的例子
///
本例的程序很简单,仅用于说明如何在.NET里面使用非对称(RSA)算法。
///
Kwanhong 2005.9
///
</summary>
class
Class1
{
public
static
void
Main(
string
[] args)
{
Class1 c
=
new
Class1();
c.StartDemo();
}
public
void
StartDemo()
{
//
RSA的加解密过程:
//
有 rsa1 和 rsa2 两个RSA对象。
//
现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2
//
rsa2 获取得公钥之后,用来加密要发送的数据内容。
//
rsa1 获取加密后的内容后,用自己的私钥解密,得出原始的数据内容。
RSACryptoServiceProvider rsa1
=
new
RSACryptoServiceProvider();
RSACryptoServiceProvider rsa2
=
new
RSACryptoServiceProvider();
string
publickey;
publickey
=
rsa1.ToXmlString(
false
);
//
导出 rsa1 的公钥
string
plaintext;
plaintext
=
"
你好吗?这是用于测试的字符串。
"
;
//
原始数据
Console.WriteLine(
"
原始数据是:\n{0}\n
"
,plaintext);
rsa2.FromXmlString(publickey);
//
rsa2 导入 rsa1 的公钥,用于加密信息
//
rsa2开始加密
byte
[] cipherbytes;
cipherbytes
=
rsa2.Encrypt(
Encoding.UTF8.GetBytes(plaintext),
false
);
/**/
/*
//////////////////////////////////////////////
*/
Console.WriteLine(
"
加密后的数据是:
"
);
for
(
int
i
=
0
; i
<
cipherbytes.Length; i
++
)
{
Console.Write(
"
{0:X2}
"
,cipherbytes[i]);
}
Console.WriteLine(
"
\n
"
);
/**/
/*
//////////////////////////////////////////////
*/
//
rsa1开始解密
byte
[] plaintbytes;
plaintbytes
=
rsa1.Decrypt(cipherbytes,
false
);
Console.WriteLine(
"
解密后的数据是:
"
);
Console.WriteLine(Encoding.UTF8.GetString(plaintbytes));
Console.ReadLine();
}
}
查看全文
相关阅读:
Week4_1Neural Networks Representation
吴恩达逻辑回归题目详解
丢弃正则化
python随笔 join 字典,列表的清空 set集合 以及深浅拷贝(重点..难点)
python全栈开发 随笔 'is' 和 == 的比较知识与区别 编码和解码的内容及转换
python全栈 字典数据类型相关知识及操作
Python全栈开发 列表, 元组 数据类型知识运用及操作 range知识
python全栈 字符串,整数,bool 数据类型运用
python全栈 流程控制;while 循环 格式化输出 运算符 及编码
python全栈开发 什么是python python命名及循环
原文地址:https://www.cnblogs.com/studio313/p/298953.html
最新文章
java方法
选择结构switch
跳转语句
DDD实践-学习大纲
MySQL基础知识汇总
架构学习资料精选
Spring源码阅读之事务管理
架构师之路系列文章
QueryDSL+SpringDataJPA提升数据访问编程效率
架构师之路2016年精选50篇
热门文章
阿里支付宝架构师:谈谈我眼中的高并发架构
golang学习资料汇总
关于CQRS的知识汇总
Matplotlib中plt.rcParams用法(设置图像细节)
机器学习中的梯度消失、爆炸原因及其解决方法
Numpy 中的矩阵向量乘法(三个)
机器学习中的范数正则化以及L2范数推导
深度学习中的batch、epoch、iteration的含义
吴恩达机器学习作业Python实现(七):K-means和PCA主成分分析
吴恩达第七章垃圾邮件分类
Copyright © 2011-2022 走看看