Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

AES Encryption\Decryption

Description
This script encrypts and decrypts text using the AES256 algorithm. For security, it doesn't use the clipboard at all.
Language
C#.net
Minimum Version
Created By
jackz98
Contributors
-
Date Created
Oct 8, 2019
Date Last Modified
Feb 18, 2020

Macro Code

using System;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// C# AES256 by jackz98
/// </summary>
public static class ClipboardFusionHelper
{
    private static readonly int Iterations = 10025;

    private static byte[] CompressBytes(byte[] bytes)
    {
        using (MemoryStream compressStream = new MemoryStream())
        {
            using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress))
                zipStream.Write(bytes, 0, bytes.Length);
            return compressStream.ToArray();
        }
    }

    private static byte[] DecompressBytes(byte[] bytes)
    {
        using (var compressStream = new MemoryStream(bytes))
        {
            using (var zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
            {
                using (var resultStream = new MemoryStream())
                {
                    zipStream.CopyTo(resultStream);
                    return resultStream.ToArray();
                }
            }
        }
    }

    public static byte[] D(byte[] input, string password, bool enc)
    {
        using (var aes = new AesManaged()
        {
            KeySize = 256,
            BlockSize = 128,
            Mode = CipherMode.CBC,
            Padding = PaddingMode.PKCS7
        })
        {
            byte[] salt = new byte[16];
            if(enc)
                new RNGCryptoServiceProvider().GetBytes(salt);
            else
                Array.Copy(input, 0, salt, 0, salt.Length);
            using (var generator = new Rfc2898DeriveBytes(password, salt, Iterations))
            {
                aes.IV = generator.GetBytes(16);
                aes.Key = generator.GetBytes(32);
                
                ICryptoTransform eng;
                if (enc)
                    eng = aes.CreateEncryptor();
                else
                    eng = aes.CreateDecryptor();
    

                using (var outStream = new MemoryStream())
                {
                    if(enc)
                        outStream.Write(salt, 0, salt.Length);
                    using (var cryptoStream = new CryptoStream(outStream, eng, CryptoStreamMode.Write))
                    using (var binaryWriter = new BinaryWriter(cryptoStream))
                    {
                        if(enc)
                            binaryWriter.Write(CompressBytes(input));
                        else
                            binaryWriter.Write(input, salt.Length, input.Length - salt.Length);
                    }
                    if(enc)
                        return outStream.ToArray();
                    else
                        return DecompressBytes(outStream.ToArray());
                }
            }
        }
    }

    private static string AddNewLine(string s)
    {
        var sb = new StringBuilder(s);
        for (int i = (s.Length / 64) * 64; i >= 64; i -= 64)
            sb.Insert(i, Environment.NewLine);
        return sb.ToString();
    }

    public static string ProcessText(string text)
    {
        // your code goes here
        text = BFS.Clipboard.CopyText();

        BFS.ClipboardFusion.PauseClipboardListener();

        if (text != null && text.Length >= 0)
        {
            string res;
            string a = BFS.Dialog.GetUserInputListViewWithFilter("AES Encryption/Decryption", new string[2] { "Encryption", "Decryption" });
            if (a == "Encryption")
            {
                string pass = BFS.Dialog.GetUserInput("Encryption: Input your password here!", "");
                if (pass != "")
                {
                    res = AddNewLine(System.Convert.ToBase64String(D(System.Text.Encoding.UTF8.GetBytes(text), pass, true)));
                    BFS.Clipboard.PasteText(res);
                }
            }
            else if (a != "")
            {
                string pass = BFS.Dialog.GetUserInput("Decryption: Input your password here!", "");
                if (pass != "")
                {
                    try
                    {
                        res = Encoding.UTF8.GetString(D(System.Convert.FromBase64String(text), pass, false));
                        BFS.Clipboard.PasteText(res);
                    }
                    catch (Exception)
                    {
                        BFS.Dialog.ShowMessageError("Decryption Failed: malformated text, corrupted message or wrong password!");
                    }
                }
            }
        }

        BFS.Clipboard.Clear();

        BFS.ClipboardFusion.ResumeClipboardListener();

        return null;
    }
}