资源简介
C#加密(DES) 实例C#加密(DES) 实例
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
// 对称加密帮助方法
public class CryptoHelper {
// 对称加密算法提供器
private ICryptoTransform encryptor; // 加密器对象
private ICryptoTransform decryptor; // 解密器对象
private const int BufferSize = 1024;
public CryptoHelper(string algorithmName string key) {
SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithmName);
provider.Key = Encoding.UTF8.GetBytes(key);
provider.IV = new byte[] { 0x12 0x34 0x56 0x78 0x90 0xAB 0xCD 0xEF };
encryptor = provider.CreateEncryptor();
decryptor = provider.CreateDecryptor();
}
public CryptoHelper(string key) : this(“TripleDES“ key) { }
// 加密算法
public string Encrypt(string clearText) {
// 创建明文流
byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText);
MemoryStream clearStream = new MemoryStream(clearBuffer);
// 创建空的密文流
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptoStream =
new CryptoStream(encryptedStream encryptor CryptoStreamMode.Write);
// 将明文流写入到buffer中
// 将buffer中的数据写入到cryptoStream中
int bytesRead = 0;
byte[] buffer = new byte[BufferSize];
do {
bytesRead = clearStream.Read(buffer 0 BufferSize);
cryptoStream.Write(buffer 0 bytesRead);
} while (bytesRead > 0);
cryptoStream.FlushFinalBlock();
// 获取加密后的文本
buffer = encryptedStream.ToArray();
string encryptedText = Convert.Tobase64String(buffer);
return encryptedText;
}
// 解密算法
public string Decrypt(string encryptedText) {
byte[] encryptedBuffer = Convert.Frombase64String(encryptedText);
Stream encryptedStream = new MemoryStream(encryptedBuffer);
MemoryStream clearStream = new MemoryStream();
CryptoStream cryptoStream =
new CryptoStream(encryptedStream decryptor CryptoStreamMode.Read);
int bytesRead = 0;
byte[] buffer = new byte[BufferSize];
do {
bytesRead = cryptoStream.Read(buffer 0 BufferSize);
clearStream.Write(buffer 0 bytesRead);
} while (bytesRead > 0);
buffer = clearStream.GetBuffer();
string clearText =
Encoding.UTF8.GetString(buffer 0 (int)clearStream.Length);
return clearText;
}
public static string Encrypt(string clearText string key) {
CryptoHelper helper = new CryptoHelper(key);
return helper.Encrypt(clearText);
}
public static string Decrypt(string encryptedText string key) {
CryptoHelper helper = new CryptoHelper(key);
return helper.Decrypt(encryptedText);
}
}
namespace ConsoleApp {
class Program {
static void Main(string[] args) {
string key = “ABCDEFGHIJKLMNOP“;
string clearText = “欢迎访问www.tracefact.net“;
CryptoHelper helper = new CryptoHelper(key);
string encryptedText = helper.Encrypt(clearText);
Console.WriteLine(encryptedText);
clearText = helper.Decrypt(encryptedText);
Console.WriteLine(clearText);
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6144 2008-10-22 23:41 ConsoleApp\bin\Debug\ConsoleApp.exe
文件 15872 2008-10-22 23:41 ConsoleApp\bin\Debug\ConsoleApp.pdb
文件 14328 2008-10-22 16:50 ConsoleApp\bin\Debug\ConsoleApp.vshost.exe
文件 2114 2008-10-22 23:41 ConsoleApp\ConsoleApp.csproj
文件 168 2008-10-22 16:24 ConsoleApp\ConsoleApp.csproj.user
文件 331 2008-10-22 20:46 ConsoleApp\obj\Debug\ConsoleApp.csproj.FileListAbsolute.txt
文件 6144 2008-10-22 23:41 ConsoleApp\obj\Debug\ConsoleApp.exe
文件 15872 2008-10-22 23:41 ConsoleApp\obj\Debug\ConsoleApp.pdb
文件 3144 2008-10-22 23:41 ConsoleApp\Program.cs
文件 1352 2008-10-09 11:19 ConsoleApp\Properties\AssemblyInfo.cs
文件 920 2008-10-09 11:19 Cryptography.sln
目录 0 2008-10-09 12:19 ConsoleApp\obj\Debug\Refactor
目录 0 2008-10-09 11:19 ConsoleApp\obj\Debug\TempPE
目录 0 2008-10-22 16:27 ConsoleApp\bin\Debug
目录 0 2008-10-22 23:41 ConsoleApp\obj\Debug
目录 0 2008-10-22 16:19 ConsoleApp\bin
目录 0 2008-10-09 11:19 ConsoleApp\obj
目录 0 2008-10-09 11:19 ConsoleApp\Properties
目录 0 2008-10-22 23:41 ConsoleApp
文件 329 2008-10-22 23:43 ReadMe.txt
----------- --------- ---------- ----- ----
66718 20
相关资源
- C# 绘图 矩形 椭圆 直线 曲线 带控制点
- ArcGIS+Engine+C#
- 三层架构——通讯录
- 用c#语言实现写字板的界面及写字板涉
- C#开发实例大全(基础卷/1.69G/光盘)
- asp.net access 注册 登陆
- Wpf_背景音乐
- C# Socket通讯框架,封装了Client和Serv
- 模拟退火算法C#源码+
- C# 新手:在窗体上显示图片和文字
- ASP.net 读取MySql简单,包含数据库读取
- C#防盗链
- C#串口通信RS232SerialPort
- AE C# 空间查询基于点,线,面
- Quartz.NET demo
- C#中读取Excel表格数据
- C#多线程编程 线程与窗体交互源码
- C#开发大全(基础卷+提高卷)源码
- CefSharp.WinForms 支持mp4
- C# WPF+MVVM+EF+多语言
- 《ArcGIS Engine+C#开发教程》
- C# FTP客户端实例42418
- C#三层架构
- 佳博条码打印机C#
- c# TTS语音 附带 DotNetSpeech.dll语音类库
- XSS攻击1
- C#写入与读取Cookie C#.Net代码
- C#开发实战1200例第2卷源码
- C#开发实战1200例第1卷源码
-
xm
l文件操作 C#源码
评论
共有 条评论