C# 檔案的加密/解密(DES)
C# 檔案的加密/解密(DES)
資料來源:http://www.dotblogs.com.tw/yc421206/archive/2012/04/18/71609.aspx
https://support.microsoft.com/zh-tw/kb/307010
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;//FILE using System.Security.Cryptography;//DESCryptoServiceProvider namespace Console_CS_Encrypt_desDecrypt { class Program { public static void desEncryptFile(string sourceFile, string encryptFile) { if (string.IsNullOrEmpty(sourceFile) || string.IsNullOrEmpty(encryptFile)) { return; } if (!File.Exists(sourceFile)) { return; } DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] key = Encoding.ASCII.GetBytes("12345678"); byte[] iv = Encoding.ASCII.GetBytes("87654321"); des.Key = key; des.IV = iv; using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)) using (FileStream encryptStream = new FileStream(encryptFile, FileMode.Create, FileAccess.Write)) { //檔E案¡Ñ加D[密ÓK byte[] dataByteArray = new byte[sourceStream.Length]; sourceStream.Read(dataByteArray, 0, dataByteArray.Length); using (CryptoStream cs = new CryptoStream(encryptStream, des.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(dataByteArray, 0, dataByteArray.Length); cs.FlushFinalBlock(); } } } public static void desDecryptFile(string encryptFile, string decryptFile) { if (string.IsNullOrEmpty(encryptFile) || string.IsNullOrEmpty(decryptFile)) { return; } if (!File.Exists(encryptFile)) { return; } DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] key = Encoding.ASCII.GetBytes("12345678"); byte[] iv = Encoding.ASCII.GetBytes("87654321"); des.Key = key; des.IV = iv; using (FileStream encryptStream = new FileStream(encryptFile, FileMode.Open, FileAccess.Read)) using (FileStream decryptStream = new FileStream(decryptFile, FileMode.Create, FileAccess.Write)) { byte[] dataByteArray = new byte[encryptStream.Length]; encryptStream.Read(dataByteArray, 0, dataByteArray.Length); using (CryptoStream cs = new CryptoStream(decryptStream, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(dataByteArray, 0, dataByteArray.Length); cs.FlushFinalBlock(); } } } static void Main(string[] args) { string sourceFile="C:\\Users\\RD\\Desktop\\06.nsi"; string encryptFile="C:\\Users\\RD\\Desktop\\06_e.nsi"; string decryptFile="C:\\Users\\RD\\Desktop\\06_d.nsi"; desEncryptFile(sourceFile,encryptFile); desDecryptFile(encryptFile, decryptFile); Pause(); } public static void Pause() { Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }
2 thoughts on “C# 檔案的加密/解密(DES)”
C# 加密和解密字串:http://fecbob.pixnet.net/blog/post/38064343-c%23-%E5%8A%A0%E5%AF%86%E5%92%8C%E8%A7%A3%E5%AF%86%E5%AD%97%E4%B8%B2-
—————————–
public class Encrypt
{
//預設金鑰向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// DES加密字串
/// </summary>
/// <param name="encryptString">待加密的字串</param>
/// <param name="encryptKey">加密金鑰,要求為8位</param>
/// <returns>加密成功返回加密後的字串,失敗返回源串</returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
}
/// <summary>
/// DES解密字串
/// </summary>
/// <param name="decryptString">待解密的字串</param>
/// <param name="decryptKey">解密金鑰,要求為8位,和加密金鑰相同</param>
/// <returns>解密成功返回解密後的字串,失敗返源串</returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
}
版主回覆:(03/29/2016 01:33:47 AM)
在NET2.0會異常
資料來源:https://dotblogs.com.tw/yc421206/archive/2012/04/18/71609.aspx
//在NET2.0正常
public static string EncryptDES(string source)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] key = Encoding.ASCII.GetBytes("12345678");
byte[] iv = Encoding.ASCII.GetBytes("87654321");
byte[] dataByteArray = Encoding.UTF8.GetBytes(source);
des.Key = key;
des.IV = iv;
string encrypt = "";
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, 0, dataByteArray.Length);
cs.FlushFinalBlock();
encrypt = Convert.ToBase64String(ms.ToArray());
}
return encrypt;
}
private string desDecryptBase64(string encrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] key = Encoding.ASCII.GetBytes("12345678");
byte[] iv = Encoding.ASCII.GetBytes("87654321");
des.Key = key;
des.IV = iv;
byte[] dataByteArray = Convert.FromBase64String(encrypt);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, 0, dataByteArray.Length);
cs.FlushFinalBlock();
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}