public string EncryptionOracle()
        {
            PlainText message = new PlainText(RandomizeMessageLength(_plainText));
            string    randKey = GetRandomKey(16).ToASCIIString();

            Random random = new Random();

            Mode = (random.Next(2) == 0) ? CipherMode.ECB : CipherMode.CBC;

            AESCipher aes = new AESCipher(Mode, PaddingMode.PKCS7);

            return(aes.Encrypt(message, randKey, CipherTextFormat.HEXADECIMAL));
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            if (!ParseInput(args))
            {
                return;
            }

            if (Params.verbose)
            {
                System.Console.WriteLine(Params.decrypt ? "Running in decryption mode." : "Running in endryption mode.");
                System.Console.WriteLine(Params.useCBC ? "Using CBC." : "Using ECB.");
                System.Console.WriteLine(Params.usePadding ? "Using PKCS5 padding." : "Not using padding. (Will fail if text length is not mod 16 byte.)");

                System.Console.WriteLine("Key: ({0})", Params.key);
                System.Console.WriteLine(BytesToFormatedString(Encoding.UTF8.GetBytes(Params.key)));
                System.Console.WriteLine();

                System.Console.WriteLine("Text: '{0}'", Params.text);
                System.Console.WriteLine(BytesToFormatedString(Encoding.UTF8.GetBytes(Params.text)));
                System.Console.WriteLine();
            }

            var cipher = new AESCipher(
                new AESParameters(
                    Encoding.UTF8.GetBytes(Params.key),
                    Params.keySize,
                    Params.useCBC,
                    Params.usePadding,
                    Params.useKeyPadding,
                    Encoding.UTF8.GetBytes(Params.initVector),
                    Params.verbose
                    )
                );

            byte[] result;

            if (Params.decrypt)
            {
                result = cipher.Decrypt(Convert.FromBase64String(Params.text));
            }
            else
            {
                result = cipher.Encrypt(Encoding.UTF8.GetBytes(Params.text));
            }

            if (Params.hex)
            {
                if (Params.useLabels || Params.verbose)
                {
                    System.Console.WriteLine("Result (Hex):");
                }
                System.Console.WriteLine(BytesToFormatedString(result));
            }

            if (Params.base64)
            {
                if (Params.useLabels || Params.verbose)
                {
                    System.Console.WriteLine("Result (base64):");
                }
                System.Console.WriteLine(Convert.ToBase64String(result));
            }

            if (Params.decrypt)
            {
                if (Params.useLabels || Params.verbose)
                {
                    System.Console.WriteLine("Result (utf-8):");
                }
                System.Console.WriteLine(Encoding.UTF8.GetString(result));
            }
        }