C # Byte [] Encryption

I have a Byte [] field, which is the contents of the file that I need to encrypt. Nothing special or unusual is enough to make sure that the next person who receives it cannot easily decode it without much effort. I would use the encryption that comes with .Net Framework 4.0, but I definitely do not need to make the file bigger than it is.

I thought just to simply flip the array or add a few bytes to the end ...?

If I can avoid increasing the size of the array, that would be great.

Any suggestions?

Thank!

+3
source share
2 answers

Does adding 1-16 bytes strengthen? AES will use the method below by default:

    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();
        }
    }
+10

(, Security by Obfuscation), , .

+2

Source: https://habr.com/ru/post/1781480/


All Articles