文件加密与Rijndael算法

问题描述 投票:-1回答:1

我正在做一个程序加密Files.I希望有我的方法在dll.But文件与Rijndael算法进行加密,当我把它添加到DLL命令

Application.DoEvents();

确实没有work.Is有没有做与Rijndael的加密,而无需使用Application.DoEvents另一方法();

c# encryption
1个回答
1
投票

加密与Rijndael算法不使用Application.DoEvents()

我不明白为什么你需要这个,虽然,但,是的,你可以做到以下几点:

using (System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create()) {

    rijndael.GenerateKey();
    // Set your Key 
    // rijndael.Key = key;
    rijndael.GenerateIV();
    // Set your IV
    // rijndael.IV = iv;
    rijndael.BlockSize = 256; // this is what makes it different from AES
    using (System.Security.Cryptography.ICryptoTransform transform = rijndael.CreateEncryptor()) {
        var fileToBeEncrypted = System.IO.File.ReadAllBytes("Path");
        transform.TransformFinalBlock(fileToBeEncrypted, 0, fileToBeEncrypted.Length);
    }
}

如果你的文件很大,那么这种做法就不会是非常有效的。在这种情况下,你最好使用类似于下面的方法流:

using (System.IO.FileStream inputFs = new System.IO.FileStream("inputPath", System.IO.FileMode.Open))
{
    using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(inputFs, rijndael.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Read)) {
        using (System.IO.FileStream outputFs = new System.IO.FileStream("outputPath", System.IO.FileMode.CreateNew)) {
            cs.CopyTo(outputFs);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.