public static void ReceiveMessage(AsymmetricKeyParameter rsaKey) { string transfer = File.ReadAllText("../../TransferData.json"); TransferData transferData = JsonConvert.DeserializeObject <TransferData>(transfer); byte[] encryptedSecret = transferData.RsaEncryptedSecret.HexToByteArray(); byte[] decryptedSecret = EncryptionUtils.RsaProcessMessage( false, encryptedSecret, rsaKey); Console.WriteLine("Transfer data received. Decrypted secret (RSA):\r\n" + Utils.ToString(decryptedSecret)); byte[] encryptedMessage = transferData.AesEncryptedMessage.HexToByteArray(); byte[] iv = transferData.AesIV.HexToByteArray(); byte[] decryptedMessage = EncryptionUtils.AesCtrDecrypt( encryptedMessage, iv, decryptedSecret); Console.WriteLine("Decrytped message (AES):\r\n" + Utils.ToUTF8String(decryptedMessage)); byte[] hmacKey = transferData.HmacKey.HexToByteArray(); byte[] messageHmac = HashUtils.ComputeHmac("HMACSHA256", decryptedMessage, hmacKey); string decryptedMessageHmac = Utils.ToString(messageHmac); bool validMessage = decryptedMessageHmac == transferData.MessageHmac; Console.WriteLine( "Decrytped message integrity check:\r\n" + (validMessage ? "Passed" : "Failed")); }
public static void SendMessage(string message, AsymmetricKeyParameter rsaKey) { Console.WriteLine("Message before sending:\r\n" + message); byte[] messageBytes = Utils.GetBytes(message); // used as the key for AES encryption byte[] secret = EncryptionUtils.GetRandomBytes(32); Console.WriteLine("AES secret before sending:\r\n" + Utils.ToString(secret)); // encrypt the shared secret using RSA byte[] encryptedSecret = EncryptionUtils.RsaProcessMessage( true, secret, rsaKey); // encrypt the message with the shared secret as key using AES byte[] iv = EncryptionUtils.GetRandomBytes(16); byte[] encryptedMessage = EncryptionUtils.AesCtrEncrypt(messageBytes, iv, secret); byte[] hmacKey = EncryptionUtils.GetRandomBytes(32); byte[] messageHmac = HashUtils.ComputeHmac(messageBytes, hmacKey, 256); TransferData transferData = new TransferData { AesEncryptedMessage = Utils.ToString(encryptedMessage), AesIV = Utils.ToString(iv), HmacKey = Utils.ToString(hmacKey), MessageHmac = Utils.ToString(messageHmac), RsaEncryptedSecret = Utils.ToString(encryptedSecret) }; string output = JsonUtils.Serialize(transferData); File.WriteAllText("../../../TransferData.json", output); }
private static void Main(string[] args) { Console.WriteLine( "HMAC-SHA-512{0}{1}", Environment.NewLine, Utils.ToString(HashUtils.ComputeHmac( Utils.GetBytes("blockchain"), Utils.GetBytes("devcamp"), 512))); }
private static void Main(string[] args) { byte[] passphrase = Utils.GetBytes("p@$$w0rd~3"); byte[] message = Utils.GetBytes("exercise-cryptography"); //byte[] scryptSalt = EncryptionUtils.GetRandomBytes(256 / 8); byte[] scryptSalt = "7b07a2977a473e84fc30d463a2333bcfea6cb3400b16bec4e17fe981c925ba4f".HexToByteArray(); int scryptCost = 16384; int scryptBlockSize = 16; int scryptParallelization = 1; int scryptDesiredKeyBitLength = 512; byte[] scryptKey = EncryptionUtils.GenerateSCryptKey( passphrase, scryptSalt, scryptCost, scryptBlockSize, scryptParallelization, scryptDesiredKeyBitLength); Console.WriteLine("Scrypt key: " + Utils.ToString(scryptKey)); byte[] encryptionKey = new byte[scryptKey.Length / 2]; Array.Copy(scryptKey, encryptionKey, encryptionKey.Length); Console.WriteLine("Encryption key: " + Utils.ToString(encryptionKey)); byte[] hmacKey = new byte[scryptKey.Length / 2]; Array.Copy(scryptKey, hmacKey.Length, hmacKey, 0, hmacKey.Length); Console.WriteLine("HMAC-SHA256 key: " + Utils.ToString(hmacKey)); BufferedBlockCipher twofishCipher = EncryptionUtils.GetTwofishCipher(); int cipherBlockSize = twofishCipher.GetBlockSize(); //byte[] iv = EncryptionUtils.GetRandomBytes(cipherBlockSize); byte[] iv = "433e0d8557a800a40c1d3b54f6636ff5".HexToByteArray(); byte[] twofishEncryptedMessage = EncryptionUtils.BlockCipherProcessMessage( twofishCipher, true, message, encryptionKey, iv); string hmacHashedMessage = HashUtils.ComputeHmac(message, hmacKey, 256).ToHex(); string outputJson = GetEncryptionResultJson( scryptSalt, scryptCost, scryptBlockSize, scryptParallelization, scryptDesiredKeyBitLength, iv, twofishEncryptedMessage, hmacHashedMessage); File.WriteAllText("../../../Output.json", outputJson); Console.WriteLine(outputJson); byte[] twofishDecryptedMessage = EncryptionUtils.BlockCipherProcessMessage( twofishCipher, false, twofishEncryptedMessage, encryptionKey, iv); Console.WriteLine("Decrypted message: {0}", Utils.ToUTF8String(twofishDecryptedMessage)); }