如何在加密文件中集成解密程序?

时间:2011-03-27 05:52:56

标签: c# encryption

我需要将解密集成到加密文件中,当您运行加密文件以要求输入密码时,文件将被解密。

我从codeproject

获取的源代码

我可以在加密程序中添加密码请求和解密程序吗? 加密程序:

/// <summary>
/// This takes an input file and encrypts it into the output file
/// </summary>
/// <param name="inFile">the file to encrypt</param>
/// <param name="outFile">the file to write the encrypted data to</param>
/// <param name="password">the password for use as the key</param>
/// <param name="callback">the method to call to notify of progress</param>
public static void EncryptFile(string inFile, string outFile, string password, CryptoProgressCallBack callback)
{
    using(FileStream fin = File.OpenRead(inFile),
                fout = File.OpenWrite(outFile))
    {
        long lSize = fin.Length; // the size of the input file for storing
        int size = (int)lSize;  // the size of the input file for progress
        byte[] bytes = new byte[BUFFER_SIZE]; // the buffer
        int read = -1; // the amount of bytes read from the input file
        int value = 0; // the amount overall read from the input file for progress

        // generate IV and Salt
        byte[] IV = GenerateRandomBytes(16);
        byte[] salt = GenerateRandomBytes(16);

        // create the crypting object
        SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt);
        sma.IV = IV;            

        // write the IV and salt to the beginning of the file
        fout.Write(IV,0,IV.Length);
        fout.Write(salt,0,salt.Length);

        // create the hashing and crypto streams
        HashAlgorithm hasher = SHA256.Create();
        using(CryptoStream cout = new CryptoStream(fout,sma.CreateEncryptor(),CryptoStreamMode.Write),
                    chash = new CryptoStream(Stream.Null,hasher,CryptoStreamMode.Write))
        {
            // write the size of the file to the output file
            BinaryWriter bw = new BinaryWriter(cout);
            bw.Write(lSize);

            // write the file cryptor tag to the file
            bw.Write(FC_TAG);

            // read and the write the bytes to the crypto stream in BUFFER_SIZEd chunks
            while( (read = fin.Read(bytes,0,bytes.Length)) != 0 )
            {
                cout.Write(bytes,0,read);
                chash.Write(bytes,0,read);  
                value += read;
                callback(0,size,value);
            }
            // flush and close the hashing object
            chash.Flush();
            chash.Close();

            // read the hash
            byte[] hash = hasher.Hash;

            // write the hash to the end of the file
            cout.Write(hash,0,hash.Length);

            // flush and close the cryptostream
            cout.Flush();
            cout.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果我理解,您想要打开文件,提示输入密码,如果密码正确,请运行真实程序吗?

如果是这样,只需制作一个快速的md5哈希检查器(带有盐),看看:HOWTO: Encode a password using MD5 in C#,然后获取嵌入代码:How to embed and access resources by using Visual C#,然后运行它:How to programmatically compile code using C# compiler

除了基本的md5检查(对反射器无效)之外,您可以使用对称加密(使用C# Symmetric Encryption之类的东西)对嵌入数据进行加密,然后使用输入的密码对其进行解密。

希望有助于您入门