描述以下代码如何工作 [英] describe how the following code will work

查看:70
本文介绍了描述以下代码如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
{
    Encrypt(Server.MapPath("~/Files/Test.PDF"), Server.MapPath("~/Files/Test2.PDF"));
    Decrypt(Server.MapPath("~/Files/Test2.PDF"), Server.MapPath("~/Files/Test3.PDF"));
}
private void Encrypt(string inputFilePath, string outputfilePath)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (FileStream fs = new FileStream(outputfilePath, FileMode.Create))
        {
            using (CryptoStream cs = new CryptoStream(fs, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                FileStream fsIn = new FileStream(inputFilePath, FileMode.Open);
                int data;
                while ((data = fsIn.ReadByte()) != -1)
                {
                    cs.WriteByte((byte)data);
                }
            }
 
        }
    }
 
}
 
private void Decrypt(string inputFilePath, string outputfilePath)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (FileStream fs = new FileStream(inputFilePath, FileMode.Open))
        {
            using (CryptoStream cs = new CryptoStream(fs, encryptor.CreateDecryptor(), CryptoStreamMode.Read))
            {
                using (FileStream fsOut = new FileStream(outputfilePath, FileMode.Create))
                {
                    int data;
                    while ((data = cs.ReadByte()) != -1)
                    {
                        fsOut.WriteByte((byte)data);
                    }
                }
            }
        }
    }
}

推荐答案

上面的代码更像是用于保护数据的加密和解密技术,因为没有其他人可以按原样读取它。



第一个块使用加密密钥,并使用该密钥加密数据,一旦完成,然后将数据保存在文件中。要加密的数据来自流 - 请使用块查看第一个,并将数据保存在另一个文件中 outputfilePath



第二个块执行相反的操作,它读取加密数据,使用相同的密钥解密(您可以看到方法中使用的第一个字符串)然后写入它到另一个。我很确定这两个函数的名称都会解释代码的整个功能。



无论如何,你可以在 Aes 上阅读更多关于MSDN密码学文档的其他课程这里 [ ^ ]。该文档包含此处使用的所有类和对象,并提供了使用它们的示例代码。
The above code is more like an encryption and decryption technique used to secure the data as no other human can read it as it stands.

The first block uses an Encryption key, and encrypts the data using that key, once done it then saves the data in a File. The data to encrypt comes from a stream - see the first using block, and the data is saved in another file outputfilePath.

The second block does the opposite, it reads the encrypted data, decrypts using the same key (you can see the first string being used in the method) and then writes it to another one. I am pretty sure the names of both the functions would have explained the entire function of code.

Anyways, you can read more on Aes and other classes on MSDN's Cryptography documentation here[^]. The document contains all of the classes and objects used here, and has sample code for using them too.


这篇关于描述以下代码如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆