C#的加密與解密
發(fā)布日期:2022-01-07 17:17 | 文章來源:源碼之家
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class FileEncrypt {
public static Byte[] ConvertStringToByteArray(String s)
{
return (new UnicodeEncoding()).GetBytes(s);
}
public static void Main()
{
//創(chuàng)建文件流
FileStream fs = new FileStream("EncryptedFile.txt",FileMode.Create,FileAccess.Write);
Console.WriteLine("輸入一些要存儲在加密文件中的文本::");
String strinput = Console.ReadLine();
Byte[] bytearrayinput=ConvertStringToByteArray(strinput);
//具有隨機密鑰的 DES 實例
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//從此實例創(chuàng)建 DES 加密器
ICryptoTransform desencrypt = des.CreateEncryptor();
//創(chuàng)建使用 des 加密轉(zhuǎn)換文件流的加密流
CryptoStream cryptostream = new CryptoStream(fs,desencrypt,CryptoStreamMode.Write);
//寫出 DES 加密文件
cryptostream.Write(bytearrayinput,0,bytearrayinput.Length);
cryptostream.Close();
//創(chuàng)建文件流以讀回加密文件
FileStream fsread = new FileStream("EncryptedFile.txt",FileMode.Open,FileAccess.Read);
//從此 des 實例創(chuàng)建 DES 解密器
ICryptoTransform desdecrypt = des.CreateDecryptor();
//創(chuàng)建加密流集合以便對傳入的字節(jié)進行讀取并執(zhí)行 des 解密轉(zhuǎn)換
CryptoStream cryptostreamDecr = new CryptoStream(fsread,desdecrypt,CryptoStreamMode.Read);
//輸出已解密文件的內(nèi)容
Console.WriteLine( (new StreamReader(cryptostreamDecr, new UnicodeEncoding())).ReadToEnd() );
Console.WriteLine ();
Console.WriteLine ("按 Enter 鍵繼續(xù)...");
Console.ReadLine();
}
}
using System.IO;
using System.Security.Cryptography;
using System.Text;
class FileEncrypt {
public static Byte[] ConvertStringToByteArray(String s)
{
return (new UnicodeEncoding()).GetBytes(s);
}
public static void Main()
{
//創(chuàng)建文件流
FileStream fs = new FileStream("EncryptedFile.txt",FileMode.Create,FileAccess.Write);
Console.WriteLine("輸入一些要存儲在加密文件中的文本::");
String strinput = Console.ReadLine();
Byte[] bytearrayinput=ConvertStringToByteArray(strinput);
//具有隨機密鑰的 DES 實例
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//從此實例創(chuàng)建 DES 加密器
ICryptoTransform desencrypt = des.CreateEncryptor();
//創(chuàng)建使用 des 加密轉(zhuǎn)換文件流的加密流
CryptoStream cryptostream = new CryptoStream(fs,desencrypt,CryptoStreamMode.Write);
//寫出 DES 加密文件
cryptostream.Write(bytearrayinput,0,bytearrayinput.Length);
cryptostream.Close();
//創(chuàng)建文件流以讀回加密文件
FileStream fsread = new FileStream("EncryptedFile.txt",FileMode.Open,FileAccess.Read);
//從此 des 實例創(chuàng)建 DES 解密器
ICryptoTransform desdecrypt = des.CreateDecryptor();
//創(chuàng)建加密流集合以便對傳入的字節(jié)進行讀取并執(zhí)行 des 解密轉(zhuǎn)換
CryptoStream cryptostreamDecr = new CryptoStream(fsread,desdecrypt,CryptoStreamMode.Read);
//輸出已解密文件的內(nèi)容
Console.WriteLine( (new StreamReader(cryptostreamDecr, new UnicodeEncoding())).ReadToEnd() );
Console.WriteLine ();
Console.WriteLine ("按 Enter 鍵繼續(xù)...");
Console.ReadLine();
}
}
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章