视频加密使用aes [英] video encryption using aes

查看:230
本文介绍了视频加密使用aes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码适用于较小的文件,即使mp3和大约50mb的视频文件,但是当它尝试加密c#win应用程序中的较大视频文件(> 1gb)时,会给我错误的内存。

AES算法:

  public byte [] AES_Encrypt(byte [] bytesToBeEncrypted,byte [] passwordBytes)
{
byte [] encryptedBytes = null;
byte [] saltBytes = new byte [] {1,2,3,4,5,6,7,8};
using(MemoryStream ms = new MemoryStream())
{
using(RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;

var key = new Rfc2898DeriveBytes(passwordBytes,saltBytes,1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);

AES.Mode = CipherMode.CBC;

使用(var cs = new CryptoStream(ms,AES.CreateEncryptor(),CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted,0,bytesToBeEncrypted.Length) ;
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}

返回encryptedBytes;
}

我在这里做错什么?

解决方案

您的问题很可能是以x86运行,并将该内存可用于该进程。这样做将架构更改为x64,所以您唯一的限制是系统中的RAM数量:


  1. 点击Build

  2. 选择配置管理器

  3. 在平台下单击新建

  4. 选择x64作为新平台

  5. 保留默认的复制设置(任何CPU)

  6. 单击确定

  7. 为解决方案中的每个项目选择x64

如果问题仍然发生,那么您将不得不将文件拆分成更小的数组,并分开处理。



代码示例,以演示如何拆分文件。警告:这是一个例子,并没有完全优化:

  FileInfo info = new FileInfo(@D:\SomeMovie .AVI); 
int bytesToRead = 128 * 1024 * 1024; // 128MB

byte [] buffer = new byte [bytesToRead]; //创建将使用加密的数组
long fileOffset = 0;
int read = 0;
bool allRead = false;

while(!allRead)
{
using(FileStream fs = new FileStream(info.FullName,FileMode.Open,FileAccess.Read))
{
fs.Seek(fileOffset,SeekOrigin.Begin); //继续从我们的位置读取...
read = fs.Read(buffer,0,bytesToRead); //读取下一个块
}

if(read == 0)
allRead = true;
else
fileOffset + = read;

//加密的东西,做你需要的...
}


This code works for smaller files even mp3 and around 50mb video files but it gives me error saying out of memory when it try to encrypt larger video files(>1gb) in c# win application

AES algorithm:

public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
    {
        byte[] encryptedBytes = null;
        byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        using (MemoryStream ms = new MemoryStream())
        {
            using (RijndaelManaged AES = new RijndaelManaged())
            {
                AES.KeySize = 256;
                AES.BlockSize = 128;

                var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                AES.Key = key.GetBytes(AES.KeySize / 8);
                AES.IV = key.GetBytes(AES.BlockSize / 8);

                AES.Mode = CipherMode.CBC;

                using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                    cs.Close();
                }
                encryptedBytes = ms.ToArray();
            }
        }

        return encryptedBytes;
    }

What am I doing wrong here?

解决方案

Your problem is most likely that you are running this as x86 and fulling the RAM available for the process. Do this to change the architecture to x64 so your only limit is the amount of RAM in your system:

  1. Click on Build
  2. Select "Configuration Manager"
  3. Under Platform, click on New
  4. Select x64 as new platform
  5. Leave the default "copy settings from" (Any CPU)
  6. Click ok
  7. Choose x64 for every project in your solution

If the problem still happens, then you will have to split the file into smaller arrays and work with them separately.

Code example, to demonstrate how you could split the file. WARNING: This is an example, and it's not completely optimized:

FileInfo info = new FileInfo(@"D:\SomeMovie.avi");
int bytesToRead = 128 * 1024 * 1024; // 128MB 

byte[] buffer = new byte[bytesToRead]; // create the array that will be used encrypted
long fileOffset = 0;
int read = 0;
bool allRead = false;

while (!allRead)
{
    using (FileStream fs = new FileStream(info.FullName, FileMode.Open, FileAccess.Read))
    {
        fs.Seek(fileOffset, SeekOrigin.Begin); // continue reading from where we were...
        read = fs.Read(buffer, 0, bytesToRead); // read the next chunk
    }

    if (read == 0)
        allRead = true;
    else
        fileOffset += read;

    // encrypt the stuff, do what you need...
}

这篇关于视频加密使用aes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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