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();
}
}
查看全文
相关阅读:
字符串练习题
js
百度商桥--提供网站与用户之间交流平台
git从本地上传到码云
命名单词
swiper 点击切换,拖动切换后继续自动轮播
ionic4创建新项目
两个年月日相减,获取年数和年数及半年数
微信小程序点击跳转出现背景
列表数据进行左浮动造成页面空白一块,排版错位问题
原文地址:https://www.cnblogs.com/studio313/p/298953.html
最新文章
【Spring】Spring重要类层次图
【Spring】IoC容器
【Spring】重新认识 IoC
SPI在JDBC中的运用
JDK中的SPI机制
【Java虚拟机11】线程上下文类加载器
第四代富士X100F操作学习
vue使用技巧
javascript原型链理解
vw+vh+rem响应式布局
热门文章
Vue项目加载本地的json数据模拟请求后台数据
vue知识点
使用ajax提交form表单
【vue报错】——listen EADDRINUSE :::8080 解决方案
Vuex全解
vue项目限时秒杀倒计时
Math对象
关于日期的题
关于数组的题
localStorage 储存与读取
Copyright © 2011-2022 走看看