简体   繁体   English

使用AES / Rijndael将文件加密为Base64格式

[英]Encrypting a file into Base64 format using AES/Rijndael

In DevGlan, at https://www.devglan.com/online-tools/aes-encryption-decryption , there is an online tool where you can upload a file and then encrypt it into Base64 format by using AES encryption and also choosing a cipher mode, secret key, and initialization vector. 在DevGlan中, 网址https://www.devglan.com/online-tools/aes-encryption-decryption ,该工具具有一个在线工具,您可以在其中上传文件,然后使用AES加密将其加密为Base64格式,还可以选择密码模式,秘密密钥和初始化向量。 I want to achieve the same thing in my C# web app. 我想在我的C#Web应用程序中实现相同的目的。 Here is my code: 这是我的代码:

FileUpload3.SaveAs(Server.MapPath(FileUpload3.FileName));
string inputFile = Server.MapPath(FileUpload3.FileName);
byte[] bytesToEncrypt = File.ReadAllBytes(inputFile);
byte[] encryptedBytes = EncryptAESfile(bytesToEncrypt, CipherMode.CBC, keyArray, IV);
string encryptedFileBase64 = Convert.ToBase64String(encryptedBytes);
string encryptedFileHex = BitConverter.ToString(encryptedBytes).Replace("-", "");

    public byte[] EncryptAESfile(byte[] data, CipherMode mode, byte[] key, byte[] iv)
    {
        byte[] encryptedData = null;
        if (data == null)
            throw new ArgumentNullException("data");

        if (data == key)
            throw new ArgumentNullException("key");

        if (data == iv)
            throw new ArgumentNullException("iv");

        using (RijndaelManaged aesAlg = new RijndaelManaged())
        {
            aesAlg.Key = key;
            aesAlg.IV = iv;
            aesAlg.Mode = mode;
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
            encryptedData = encryptor.TransformFinalBlock(data, 0, data.Length);
        }

        return encryptedData;
    }

The code does return a Base64 string of the file, as in the variable encryptedFileBase64 , but not the correct one in reference to DevGlan. 该代码不返回该文件的Base64编码字符串,如可变encryptedFileBase64 ,但在参考DevGlan不正确的。 My code only returns a Base64 string of length 24 whereas DevGlan returns a string of nearly 100,000 characters. 我的代码仅返回长度为24的Base64字符串,而DevGlan返回近100,000个字符的字符串。 Also, when I test to see if bytes are being read, the following code returns 0, so the problem could be in my first few lines: 另外,当我测试以查看是否正在读取字节时,以下代码返回0,因此问题可能出在我的前几行:

lblBytes.Text += "Bytes read: " + bytesToEncrypt.Length;

I've also seen many examples of encrypting files - whether in AES or some other symmetric encryption algorithm - but not ones that return a Base64 string. 我也看到过许多加密文件的示例(无论是使用AES还是其他对称加密算法),但没有返回Base64字符串的文件。 Most end with lines like this before the CryptoStream is closed: 在关闭CryptoStream之前,大多数以这样的行结尾:

byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);   // The input FileStream
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

Reference: Encrypting any file using AES 参考: 使用AES加密任何文件

Is there a way to read the byte array of the CryptoStream and then convert it to Base64, because I don't see bytearrayinput left alone as storing the correct information. 有没有一种方法可以读取CryptoStream的字节数组,然后将其转换为Base64,因为我不认为bytearrayinput可以存储正确的信息。 Help would be much appreciated. 帮助将不胜感激。 Thanks! 谢谢!

First: How is key created? 第一:如何创建密钥? Have you double-checked that your salt is the same? 您是否仔细检查过盐是否相同?

Also I haven't checked, but this code should work: 另外我还没有检查,但是这段代码应该可以工作:

AesManaged cipher = new AesManaged();
cipher.Mode = MODE;
ICryptoTransform encryptor = cipher.CreateEncryptor(KEY, IV);
MemoryStream to = new MemoryStream();
CryptoStream writer = new CryptoStream(to, encryptor, CryptoStreamMode.Write);

writer.Write(input, 0, input.Length);
writer.FlushFinalBlock();
byte[] encrypted = to.ToArray();
return Convert.ToBase64String(encrypted);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM