private void RunHMACExample() { // Hashed Message Authentication Codes Console.WriteLine("Hashed Message Authentication Codes started"); Console.WriteLine(String.Format("Message before Hashing: {0}", _originalMessage)); Console.WriteLine(string.Empty); byte[] keyToUse = CryptographyExample.GenerateKey(); byte[] MD5Hash = CryptographyExample.ComputeMd5HashWithKey(Encoding.UTF8.GetBytes(_originalMessage), keyToUse); Console.WriteLine(String.Format("Message after MD5 Hash: {0}", Convert.ToBase64String(MD5Hash))); byte[] SHA1Hash = CryptographyExample.ComputeSha1HashWithKey(Encoding.UTF8.GetBytes(_originalMessage), keyToUse); Console.WriteLine(String.Format("Message after SHA1 Hash: {0}", Convert.ToBase64String(SHA1Hash))); byte[] SHA2Hash256 = CryptographyExample.ComputeSha2Hash256WithKey(Encoding.UTF8.GetBytes(_originalMessage), keyToUse); Console.WriteLine(String.Format("Message after SHA2 (256) Hash: {0}", Convert.ToBase64String(SHA2Hash256))); byte[] SHA2Hash512 = CryptographyExample.ComputeSha2Hash512WithKey(Encoding.UTF8.GetBytes(_originalMessage), keyToUse); Console.WriteLine(String.Format("Message after SHA2 (512) Hash: {0}", Convert.ToBase64String(SHA2Hash512))); Console.WriteLine(string.Empty); Console.WriteLine("Hashed Message Authentication Codes ended"); }
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"); }
private void StoringPasswordsUsingSaltedHashes() { // using salted hashes Console.WriteLine("Encrypting Passwords started"); Console.WriteLine(string.Empty); User U = new User(); Console.WriteLine("please enter the user name"); U.UserName = Console.ReadLine().Trim(); //Console.WriteLine("please enter the password"); //U.Password = Console.ReadLine().Trim(); byte[] salt = CryptographyExample.GenerateSalt(); byte[] saltedHash = CryptographyExample.HashPasswordWithSalt(Encoding.UTF8.GetBytes(_tempPassword), salt); U.Password = Convert.ToBase64String(saltedHash); JSONDataBase JsonDB = new JSONDataBase(); JsonDB.AddUser(U); Console.WriteLine("Encrypting Passwords ended"); Console.WriteLine(string.Empty); }
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"); }
private static void RunRsaWithRsaParameterKeyFromCSP() { // CSP = Configuration Service Provider or Windows Key Container // these can be stored as either: // 1) User-level key containers (C:\Users\<user_name>\AppData\Roaming\Microsoft\Crypto\RSA). // These are only accessable by the user as they are stored in the users profile // 2) machine-level key container (C:\Users\All Users\Application Data\Microsoft\Crypto\RSA) // These are stored on a global level that all users on the machine can access Console.WriteLine("Encryption Using RSA with Parameter key from CSP started"); Console.WriteLine(); CryptographyExample cryptographyExample = new CryptographyExample(); const string originalMessage = "Some Text to Encrypt"; Console.WriteLine(String.Format("Message before encryption: {0}", originalMessage)); cryptographyExample.AssignNewRSAKeyAndStoreInCSP(); byte[] encryptedMessage = cryptographyExample.EncryptDataUsingRSAStoredinCSP(Encoding.UTF8.GetBytes(originalMessage)); Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedMessage))); byte[] decryptedMessage = cryptographyExample.DecryptDataUsingRSAStoredinCSP(encryptedMessage); Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedMessage), true)); cryptographyExample.DeleteRSPkeyStoredInCSP(); Console.WriteLine(); Console.WriteLine("Encryption Using RSA with Parameter key from CSP ended"); }
private void RunMD5Algorithm() { Console.WriteLine("MD5 Hash Algorithm started"); Console.WriteLine(String.Format("Message before Hash: {0}", _originalMessage)); byte[] hashedMessage = CryptographyExample.ComputeMd5Hash(Encoding.UTF8.GetBytes(_originalMessage)); Console.WriteLine(String.Format("Message after Hash: {0}", Convert.ToBase64String(hashedMessage))); Console.WriteLine("MD5 Hash Algorithm ended"); }
private void RunPasswordBasedKeyDerivationFunction() { Console.WriteLine("Password Based Key Derivation Functions started"); CryptographyExample.HashUsingPasswordBasedKeyDerivationFunction(_tempPassword, 100); CryptographyExample.HashUsingPasswordBasedKeyDerivationFunction(_tempPassword, 1000); CryptographyExample.HashUsingPasswordBasedKeyDerivationFunction(_tempPassword, 10000); CryptographyExample.HashUsingPasswordBasedKeyDerivationFunction(_tempPassword, 50000); CryptographyExample.HashUsingPasswordBasedKeyDerivationFunction(_tempPassword, 100000); CryptographyExample.HashUsingPasswordBasedKeyDerivationFunction(_tempPassword, 200000); CryptographyExample.HashUsingPasswordBasedKeyDerivationFunction(_tempPassword, 500000); Console.WriteLine("Password Based Key Derivation Functions ended"); }
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"); }
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"); }
private static void RunRsaWithRsaParameterKey() { Console.WriteLine("Encryption Using RSA with Parameter key started"); Console.WriteLine(); CryptographyExample cryptographyExample = new CryptographyExample(); const string originalMessage = "Some Text to Encrypt"; Console.WriteLine(String.Format("Message before encryption: {0}", originalMessage)); cryptographyExample.AssignNewRSAKey(); byte[] encryptedMessage = cryptographyExample.EncryptDataUsingRSA(Encoding.UTF8.GetBytes(originalMessage)); Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedMessage))); byte[] decryptedMessage = cryptographyExample.DecryptDataUsingRSA(encryptedMessage); Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(decryptedMessage))); Console.WriteLine(); Console.WriteLine("Encryption Using RSA with Parameter key ended"); }
public HybridEncryption() { _cryptographyExample = new CryptographyExample(); }