Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            byte[] key256 = new byte[32];
            for (int i = 0; i < 32; i++)
            {
                key256[i] = Convert.ToByte(i % 256);
            }

            string message  = "Hello World";
            string password = "******";

            byte[] nonSecretOrg = Encoding.UTF8.GetBytes("Pay Bob Zero Dollars");
            byte[] nonSecretMod = Encoding.UTF8.GetBytes("Pay Bob $ 1,000,000.");

            // Encrypt with associated data
            //string encrypted = AESGCM.SimpleEncrypt(message, key256, nonSecretOrg);
            string encrypted = AESThenHMAC.SimpleEncryptWithPassword(message, password, nonSecretOrg);

            Console.WriteLine("AESThenHMAC Encrypted: {0}", encrypted);

            // Decrypt with original associated data
            //string decrypted = AESGCM.SimpleDecrypt(encrypted, key256, nonSecretOrg.Length);
            string decrypted = AESThenHMAC.SimpleDecryptWithPassword(encrypted, password, nonSecretOrg.Length);

            Console.WriteLine("AESThenHMAC Decrypted: {0}", decrypted);
            //Console.WriteLine("Auth cleartext: {0}", Encoding.UTF8.GetString(nonSecretOrg));

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();


            var secret = AESCBC.EncryptString(password, message);

            //Console.WriteLine("AESCBC Encrypted: {0}", BitConverter.ToString(secret));
            Console.WriteLine("AESCBC Encrypted: {0}", Convert.ToBase64String(secret));

            var recovered = AESCBC.DecryptString(password, secret);

            Console.WriteLine("AESCBC Decrypted: {0}", recovered);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            Rijndael.Inputkey = password;

            encrypted = Rijndael.EncryptRijndael(message, "12345678");
            //Console.WriteLine("AESCBC Encrypted: {0}", BitConverter.ToString(secret));
            Console.WriteLine("Rijndael Encrypted: {0}", encrypted);

            decrypted = Rijndael.DecryptRijndael(encrypted, "12345678");
            Console.WriteLine("Rijndael Decrypted: {0}", decrypted);


            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();


            string passPhrase         = "Pas5pr@se";        // can be any string
            string saltValue          = "12345678";         //"s@1tValue";        // can be any string
            string hashAlgorithm      = "SHA1";             // can be "MD5"
            int    passwordIterations = 10000;              // can be any number
            string initVector         = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
            int    keySize            = 128;                // 256;                // can be 192 or 128

            string cipherText = RijndaelSimple.Encrypt
                                (
                message,
                passPhrase,
                saltValue,
                hashAlgorithm,
                passwordIterations,
                initVector,
                keySize
                                );

            Console.WriteLine(String.Format("RijndaelSimple Encrypted : {0}", cipherText));

            message = RijndaelSimple.Decrypt
                      (
                cipherText,
                passPhrase,
                saltValue,
                hashAlgorithm,
                passwordIterations,
                initVector,
                keySize
                      );

            Console.WriteLine(String.Format("RijndaelSimple Decrypted : {0}", message));

            Console.ReadLine();
        }