C#字节[]加密 [英] C# Byte[] Encryption

查看:103
本文介绍了C#字节[]加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个byte []字段那就是我需要加密文件的内容。没有什么特别的或幻想,只是足以确保旁边的人谁得到它不会是能够把它没有一些努力,轻松解码。我会用自带的.Net框架4.0的加密,但我绝对不需要做​​出任何文件大于它。

I have a Byte[] field that is a file contents that I need to encrypt. Nothing special or fancy, just enough to make sure the next person who gets it won't be able to easily decode it without some effort. I would use the encryption that comes with .Net Framework 4.0 but I definitely do not need to make the file any bigger than it is.

我想过只是简单地扭转阵列或添加几个字节到底......?

I thought about just simply reversing the array or adding a few bytes to the end...?

如果我能避免使阵列大得多,这将是巨大的。

If I can avoid making the array to much bigger that would be great.

有什么建议?

谢谢!

推荐答案

是否增加了1-16字节伤害?

Does the addition of 1-16 bytes hurt? AES will pad by default using the below method:

    private static void EncryptThenDecrypt()
    {
        byte[] message; // fill with your bytes
        byte[] encMessage; // the encrypted bytes
        byte[] decMessage; // the decrypted bytes - s/b same as message
        byte[] key;
        byte[] iv;

        using (var rijndael = new RijndaelManaged())
        {
            rijndael.GenerateKey();
            rijndael.GenerateIV();
            key = rijndael.Key;
            iv = rijndael.IV;
            encMessage = EncryptBytes(rijndael, message);
        }

        using (var rijndael = new RijndaelManaged())
        {
            rijndael.Key = key;
            rijndael.IV = iv;
            decMessage = DecryptBytes(rijndael, encMessage);
        }
    }

    private static byte[] EncryptBytes(
        SymmetricAlgorithm alg,
        byte[] message)
    {
        if ((message == null) || (message.Length == 0))
        {
            return message;
        }

        if (alg == null)
        {
            throw new ArgumentNullException("alg");
        }

        using (var stream = new MemoryStream())
        using (var encryptor = alg.CreateEncryptor())
        using (var encrypt = new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
        {
            encrypt.Write(message, 0, message.Length);
            encrypt.FlushFinalBlock();
            return stream.ToArray();
        }
    }

    private static byte[] DecryptBytes(
        SymmetricAlgorithm alg,
        byte[] message)
    {
        if ((message == null) || (message.Length == 0))
        {
            return message;
        }

        if (alg == null)
        {
            throw new ArgumentNullException("alg");
        }

        using (var stream = new MemoryStream())
        using (var decryptor = alg.CreateDecryptor())
        using (var encrypt = new CryptoStream(stream, decryptor, CryptoStreamMode.Write))
        {
            encrypt.Write(message, 0, message.Length);
            encrypt.FlushFinalBlock();
            return stream.ToArray();
        }
    }

这篇关于C#字节[]加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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