• C# 文件夹加密


    可以加密文件内容,也可以对文件夹本身进行加密,本文对文件夹加密。

    一、指定或生成一个密钥

      1)指定的密钥

     /// <summary>
     /// 密钥,这个密码可以随便指定
     /// </summary>
     public static string sSecretKey = "?a??64(?";

     2) 也可以生成密钥

    /// <summary>
    /// 生成一个64位的密钥
    /// </summary>
    /// <returns>string</returns>
    public static string GenerateKey()
    {
      //创建对称算法的一个实例。自动生成的密钥和IV。
      DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
    
        // 使用自动生成的密钥进行加密。
        return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
     }

    二、调用ZeroMemory 函数从内存中删除Key

    ///<summary>
    /// 调用该函数从内存中删除的Key后使用
    /// </summary>
    [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
    public static extern bool ZeroMemory(IntPtr Destination, int Length);
    三、加密文件
    /// <summary>
            /// 加密文件
            /// </summary>
            /// <param name="sInputFilename">待加密的文件的完整路径</param>
            /// <param name="sOutputFilename">加密后的文件的完整路径</param>
            public static void EncryptFile(string sInputFilename, string sOutputFilename)
            {
                FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
    
                FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
                ICryptoTransform desencrypt = DES.CreateEncryptor();
                CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
    
                byte[] bytearrayinput = new byte[fsInput.Length];
                fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
    
                cryptostream.Flush();
                fsInput.Flush();
                fsEncrypted.Flush();
                cryptostream.Close();
                fsInput.Close();
                fsEncrypted.Close();
            }
    四、解密文件
    /// <summary>
            /// 解密文件
            /// </summary>
            /// <param name="sInputFilename">待解密的文件的完整路径</param>
            /// <param name="sOutputFilename">解密后的文件的完整路径</param>
            public static void DecryptFile(string sInputFilename, string sOutputFilename)
            {
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
    
                FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
                ICryptoTransform desdecrypt = DES.CreateDecryptor();
                CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
                StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
                fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
                fsDecrypted.Flush();
                fsDecrypted.Close();
            }
    五、完整代码
    /// <summary>
        /// 文件加密
        /// </summary>
        public class FileSecretHelper
        {
            /// <summary>
            /// 密钥,这个密码可以随便指定
            /// </summary>
            public static string sSecretKey = "?a??64(?";
    
            /// <summary>
            /// 调用该函数从内存中删除的Key后使用
            /// </summary>
            [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
            public static extern bool ZeroMemory(IntPtr Destination, int Length);
    
    
            /// <summary>
            /// 生成一个64位的密钥
            /// </summary>
            /// <returns>string</returns>
            public static string GenerateKey()
            {
                //创建对称算法的一个实例。自动生成的密钥和IV。
                DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
    
                // 使用自动生成的密钥进行加密。
                return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
            }
    
            /// <summary>
            /// 加密文件
            /// </summary>
            /// <param name="sInputFilename">待加密的文件的完整路径</param>
            /// <param name="sOutputFilename">加密后的文件的完整路径</param>
            public static void EncryptFile(string sInputFilename, string sOutputFilename)
            {
                FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
    
                FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
                ICryptoTransform desencrypt = DES.CreateEncryptor();
                CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
    
                byte[] bytearrayinput = new byte[fsInput.Length];
                fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
    
                cryptostream.Flush();
                fsInput.Flush();
                fsEncrypted.Flush();
                cryptostream.Close();
                fsInput.Close();
                fsEncrypted.Close();
            }
    
            /// <summary>
            /// 解密文件
            /// </summary>
            /// <param name="sInputFilename">待解密的文件的完整路径</param>
            /// <param name="sOutputFilename">解密后的文件的完整路径</param>
            public static void DecryptFile(string sInputFilename, string sOutputFilename)
            {
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey);
    
                FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
                ICryptoTransform desdecrypt = DES.CreateDecryptor();
                CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
                StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
                fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
                fsDecrypted.Flush();
                fsDecrypted.Close();
            }
        }
  • 相关阅读:
    未处理的异常 stack overflow
    Largest Rectangle in a Histogram [POJ2559] [单调栈]
    Blocks [POJ3734] [矩阵快速幂]
    Rectangular Covering [POJ2836] [状压DP]
    Arrange the Bulls [POJ2441] [状压DP]
    The Water Bowls [POJ3185] [开关问题]
    Bound Found [POJ2566] [尺取法]
    4 Values whose Sum is 0 [POJ2785] [折半搜索]
    Physics Experiment 弹性碰撞 [POJ3684]
    Fliptile [POJ3279] [开关问题]
  • 原文地址:https://www.cnblogs.com/bohetang/p/4025836.html
Copyright © 2020-2023  润新知