开发者

How to integrate the decryption procedure in the encrypted file?

I need to integrate the decryption in the encrypted file, and when you run an encrypted file to require input the password, after which the file will be decrypted.

The source code I took from codeproject

Can I add a password request, and decryption procedure into the encryption procedure? The encryption procedure:

/// <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>
publ开发者_开发百科ic 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();
        }
    }
}


if I understand, you want a file to open, prompt a password, and if the password is correct, run the real program?

If so, just make a quick md5 hash checker (with a salt), look at this: HOWTO: Encode a password using MD5 in C#, then get the embedded code: How to embed and access resources by using Visual C#, then run it: How to programmatically compile code using C# compiler.

Alternatively to the basic md5 check (which will do nothing against reflector), you would encrypt the embedded data with symmetric encryption (using something like C# Symmetric Encryption), then decrypt it with the entered password.

Hope that helps get you started

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜