Example #1
0
        private void RunEncryptionUsingTripleDES()
        {
            Console.WriteLine("Encryption Using Triple DES started");
            Console.WriteLine();

            CryptographyExample cryptographyExample = new CryptographyExample(8);

            // we are using 24 bytes to make use of the tripple key variants of triple DES
            // 24 bytes represents three 64 bit keys or three 8 byte keys
            // under the covers our data will be encrypted with key1, then with key2, then with key3
            byte[] key = cryptographyExample.GenerateRandomNumber(24);


            // we could just send in 16 bytes and it will still work fine, however this means that
            // under the covers our data will get encrypted using the first key, then encrypted again using the 2nd key, then once more
            // using the first key again.
            //byte[] key = cryptographyExample.GenerateRandomNumber(16);


            byte[]       initializationVector = cryptographyExample.GenerateRandomNumber(8);
            const string originalMessage      = "Text To Encrypt";

            Console.WriteLine(String.Format("Message before encryption: {0}", originalMessage));

            byte[] encryptedMessage = cryptographyExample.EncryptUsingTripleDES(Encoding.UTF8.GetBytes(originalMessage), key, initializationVector);
            Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedMessage)));

            byte[] decryptedMessage = cryptographyExample.DecryptUsingTripleDES(encryptedMessage, key, initializationVector);
            Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedMessage)));

            Console.WriteLine();
            Console.WriteLine("Encryption Using Triple DES ended");
        }
Example #2
0
        private void RunEncryptionUsingAES()
        {
            Console.WriteLine("Encryption Using AES started");
            Console.WriteLine();

            CryptographyExample cryptographyExample = new CryptographyExample();

            // here we are creating a key that is 32 bytes in length or 256 bits, which is the max size of an AES key
            byte[] key = cryptographyExample.GenerateRandomNumber(32);

            // AES using a 16 byte Initialization Vector
            byte[]       initializationVector = cryptographyExample.GenerateRandomNumber(16);
            const string originalMessage      = "Text To Encrypt";

            Console.WriteLine(String.Format("Message before encryption: {0}", originalMessage));

            byte[] encryptedMessage = cryptographyExample.EncryptUsingAES(Encoding.UTF8.GetBytes(originalMessage), key, initializationVector);
            Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedMessage)));

            byte[] decryptedMessage = cryptographyExample.DecryptUsingAES(encryptedMessage, key, initializationVector);
            Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedMessage)));

            Console.WriteLine();
            Console.WriteLine("Encryption Using AES ended");
        }
Example #3
0
        private void RunEncryptionUsingDES()
        {
            Console.WriteLine("Encryption Using DES started");
            Console.WriteLine();

            CryptographyExample cryptographyExample = new CryptographyExample(8);

            byte[]       key = cryptographyExample.GenerateRandomNumber();
            byte[]       initializationVector = cryptographyExample.GenerateRandomNumber();
            const string originalMessage      = "Text To Encrypt";

            Console.WriteLine(String.Format("Message before encryption: {0}", originalMessage));

            byte[] encryptedMessage = cryptographyExample.EncryptUsingDES(Encoding.UTF8.GetBytes(originalMessage), key, initializationVector);
            Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedMessage)));

            byte[] decryptedMessage = cryptographyExample.DecryptUsingDES(encryptedMessage, key, initializationVector);
            Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedMessage)));

            Console.WriteLine();
            Console.WriteLine("Encryption Using DES ended");
        }
Example #4
0
        public EncryptedPacket EncryptData(byte[] originalMessage, RsaWithRsaParameterKey rsaParams)
        {
            // Sender generates AES session key
            byte[] sessionKey = _cryptographyExample.GenerateRandomNumber(32);

            // Sender generates Initialization Vector
            byte[] initializationVector = _cryptographyExample.GenerateRandomNumber(16);

            // Sender stores that IV in the packet object
            EncryptedPacket EP = new EncryptedPacket
            {
                IV = initializationVector
            };

            // Sender encrypts data using AES
            EP.EncryptedData = _cryptographyExample.EncryptUsingAES(originalMessage, sessionKey, EP.IV);

            //Sender encrypts the session key with RSA
            EP.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);

            return(EP);
        }
Example #5
0
        private void RunRNGCryptoServiceRandomNumbersExample()
        {
            Console.WriteLine("RNGCryptoService Simple Random Numbers started");
            Console.WriteLine();

            CryptographyExample CE = new CryptographyExample(32);

            for (int index = 1; index < 11; index++)
            {
                Console.WriteLine(String.Format("Random Number {0}: {1}", index, Convert.ToBase64String(CE.GenerateRandomNumber())));
                //check the console readout for this comment:
                //The final '==' sequence indicates that the last group contained only one byte, and '=' indicates that it contained two bytes.
            }

            Console.WriteLine();
            Console.WriteLine("RNGCryptoService Simple Random Numbers complete");
        }