• 大小: 21KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: C#
  • 标签: 实例  

资源简介

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


评论

共有 条评论