using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApplication
{
public class EncryptKit
{
/// <summary>
/// Call this function to remove the key from memory after use for security
/// </summary>
/// <param name="Destination"></param>
/// <param name="Length"></param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);
/// <summary>
/// Function to Generate a 64 bits Key.
/// </summary>
/// <returns>返回生成的密钥</returns>
public static string GenerateKey()
{
// Create an instance of Symetric Algorithm. Key and IV is generated automatically.
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
// Use the Automatically generated key for Encryption.
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}
/// <summary>
/// 加密文件
/// </summary>
/// <param name="sInputFilename">要加密的文件</param>
/// <param name="sOutputFilename">加密后保存的文件</param>
/// <param name="sKey">密钥</param>
public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
{
using (FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
{
byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
fsInput.Close();
FileStream fsEncrypted = new FileStream(sOutputFilename,
FileMode.OpenOrCreate,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsEncrypted.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="sInputFilename">要解密的文件</param>
/// <param name="sOutputFilename">解决后保存的文件</param>
/// <param name="sKey">密钥</param>
public static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//A 64 bit key and IV is required for this provider.
//Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
//Create a file stream to read the encrypted file back.
using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
{
//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
//Create crypto stream set to read and do a
//DES decryption transform on incoming bytes.
CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
//Print the contents of the decrypted file.
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="sInputFilename">要解密的文件路径</param>
/// <param name="sKey">密钥</param>
/// <returns>返回内容</returns>
public static string DecryptFile(string sInputFilename, string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//A 64 bit key and IV is required for this provider.
//Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
//Create a file stream to read the encrypted file back.
using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
{
byte[] byt = new byte[fsread.Length];
fsread.Read(byt, 0, byt.Length);
fsread.Flush();
fsread.Close();
//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
MemoryStream ms = new MemoryStream();
CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt, CryptoStreamMode.Write);
cryptostreamDecr.Write(byt, 0, byt.Length);
cryptostreamDecr.FlushFinalBlock();
cryptostreamDecr.Close();
return Encoding.UTF8.GetString(ms.ToArray()).Trim();
}
}
}
}
调用如下
static void Main(string[] args)
{
sSecretKey = EncryptKit.GenerateKey();
GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);
EncryptKit.EncryptFile(@"config.dat", @"config.dat", sSecretKey);
EncryptKit.DecryptFile(@"config.dat", @"Decrypted.dat", sSecretKey);
string str = EncryptKit.DecryptFile("config.dat", sSecretKey);
Console.WriteLine(sSecretKey);
EncryptKit.ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
gch.Free();
Console.ReadKey();
}
分享到:
相关推荐
c#加密解密c#加密解密c#加密解密c#加密解密c#加密解密c#加密解密c#加密解密c#加密解密
用c#做的加密解密的小软件,可以实现对文本的加密解密。
带加密字符的 加密 解密方法 static string encryptKey = "加密... 自定义加密字符(加密解密必须一致才能解密) /// /// 加密字符串 /// /// name="str">要加密的字符串 /// 加密后的字符串
C#做的RSA加密解密, 带winform界面, 测试通过, 代码完整。
c#加密解密加密解密加密解密加密解密加密解密c#加密解密加密解密加密解密加密解密加密解密
java c# 加密解密
C#加密解密,包括DES、MD5、SHA256
c#加密解密类源码 全部代码 送上 .net c#版本
实现C#加密各种文件 和解密加密后的文件 完整工程 直接可以编译运行
C# 文件加密 实例源码(加密解密)
C#编写的加密解密小工具,可以对DES、RSA、Base64、SHA、MD5算法,轻松实现数据加密解密需求。 使用方法可浏览博文《C#集成数据加密算法,包含DES、RSA、Base64、SHA、MD5算法,轻松实现数据加密解密需求》
C#加密 解密对接java 的加密AES/CBC/PKCS7Padding 转16进制 C#加密解密中的详细整理
C# .net MD5加密解密工具及加密解密类。使用方便,可直接下载加密解密代码在项目中使用。
解密文件 128位MD5算法加密字符串 128位MD5算法加密Byte数组 32位MD5加密 Base64加密 Base64解密 DES加密/解密类。 加密 加密数据 解密 解密数据 得到随机安全码(哈希加密)。 得到随机哈希加密字符串 哈希加密一...
C#加密和解密C#加密和解密C#加密和解密C#加密和解密
c# 使用DidiSoft.Pgp来实现对文件的GPG 加密与解密 。GPG非对称加密需要公钥和私钥 :你的公钥的作用 :别人用来给你发加密的信息&别人验证你的签名,即加密&验证(别人来做) 你的私钥的作用 :你用来创建签名&...
c# ras 加密解密 代码例子
RSA加密解密C#实现调用实例 public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) { try { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; System.Security....
说到加密,可能大家最熟悉的就是MD5了,一般保存密码、查看文档是否更新都是用到MD5加密,MD5加密是不可逆的,不能解密出明文。...C#里的MD5、DES、AES、RSA加密解密详细方法已封装,大家安心下载使用。
c# 和 c++ 通用加密解密 。有c#和c++的例子代码。亲测可用 c# 和 c++ 通用加密解密 。有c#和c++的例子代码。亲测可用 c# 和 c++ 通用加密解密 。有c#和c++的例子代码。亲测可用 c# 和 c++ 通用加密解密 。有c#和c++...