public static void HybridIntergityCheckEncrypDecrypt() { const string original = "Very secret and important information that can not into the hacker."; var hybrid = new HybridEncryptionIntegirtyCheck(); var rsaParams = new RSAWithRSAParameterKey(); rsaParams.AssignNewKey(); Console.WriteLine("Hybrid Encryption with Integrity Check Demonstration in .NET"); Console.WriteLine("------------------------------------------------------------"); Console.WriteLine(); try { var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams); var decrypted = hybrid.DecryptData(encryptedBlock, rsaParams); Console.WriteLine("Original Message = " + original); Console.WriteLine(); Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrypted)); } catch (CryptographicException ex) { Console.WriteLine("Error : " + ex.Message); } }
public byte[] DecryptData(EncryptedPacket encryptedpacket, RSAWithRSAParameterKey rsaParams) { // Decrypt AES Key with RSA var decryptedSessionKey = rsaParams.DecryptData(encryptedpacket.EncryptedSessionKey); // Decrypt our data with AES using the decrypted session key var decryptedData = _aes.Decrypt(encryptedpacket.EncryptedData, decryptedSessionKey, encryptedpacket.Iv); return(decryptedData); }
public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams) { // Generate our session key. var sessionKey = _aes.GenerateRandomNumber(32); // Create the encrypted packet and generate the IV var encryptedPacket = new EncryptedPacket { Iv = _aes.GenerateRandomNumber(16) }; // Encrypt our data with AES encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv); // Encrypt the session key with RSA encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey); return(encryptedPacket); }
public static void HyrbidEncrypDecrypt() { const string original = "Very secret and important information that can not into the hacker."; var rsaParams = new RSAWithRSAParameterKey(); rsaParams.AssignNewKey(); var hybrid = new HybridEncryption(); var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams); var decrypted = hybrid.DecryptData(encryptedBlock, rsaParams); Console.WriteLine("Hybrid Encryption Demonstration in .NET"); Console.WriteLine("---------------------------------------"); Console.WriteLine(); Console.WriteLine("Original Message = " + original); Console.WriteLine(); Console.WriteLine("Message After Decruption = " + Encoding.UTF8.GetString(decrypted)); }
public byte[] DecryptData(EncryptedPacketIntegrityCheck encryptedpacket, RSAWithRSAParameterKey rsaParams) { // Decrypt AES Key with RSA var decryptedSessionKey = rsaParams.DecryptData(encryptedpacket.EncryptedSessionKey); using (var hmac = new HMACSHA256(decryptedSessionKey)) { var hmacToCheck = hmac.ComputeHash(encryptedpacket.EncryptedData); if (!Comparer(encryptedpacket.Hmac, hmacToCheck)) { throw new CryptographicException("HMAC for decryption does not match"); } } // Decrypt our data with AES using the decrypted session key var decryptedData = _aes.Decrypt(encryptedpacket.EncryptedData, decryptedSessionKey, encryptedpacket.Iv); return(decryptedData); }
public static void EncryptDecryptWithRSAWithRSAParameterKey() { var rsaParams = new RSAWithRSAParameterKey(); const string original = "Text to encrypt"; rsaParams.AssignNewKey(); var encryptedRsaParams = rsaParams.EncryptData(Encoding.UTF8.GetBytes(original)); var decryptedRsaParams = rsaParams.DecryptData(encryptedRsaParams); Console.WriteLine("RSA Encryption Demonstration in .NET"); Console.WriteLine("------------------------------------"); Console.WriteLine(); Console.WriteLine("In Memory Key"); Console.WriteLine(); Console.WriteLine("Original Text = " + original); Console.WriteLine(); Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(encryptedRsaParams)); Console.WriteLine(); Console.WriteLine("Decrypted Text = " + Encoding.Default.GetString(decryptedRsaParams)); Console.WriteLine(); Console.WriteLine(); }
public EncryptedPacketIntegrityCheck EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams) { // Generate our session key. var sessionKey = _aes.GenerateRandomNumber(32); // Create the encrypted packet and generate the IV var encryptedPacket = new EncryptedPacketIntegrityCheck { Iv = _aes.GenerateRandomNumber(16) }; // Encrypt our data with AES encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv); // Encrypt the session key with RSA encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey); using (var hmac = new HMACSHA256(sessionKey)) { encryptedPacket.Hmac = hmac.ComputeHash(encryptedPacket.EncryptedData); } return(encryptedPacket); }