資源簡介
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
評論
共有 條評論