示例#1
0
        private static StreamEncryptor CreateEncryptor(string algorithm, byte[] key, byte[] nonce)
        {
            switch (algorithm)
            {
            case "AES-SIV": return(StreamEncryptor.CreateAesCmacSivEncryptor(key, nonce));

            case "AES-PMAC-SIV": return(StreamEncryptor.CreateAesPmacSivEncryptor(key, nonce));

            default: throw new ArgumentException("Unknown algorithm.");
            }
        }
示例#2
0
        private static void StreamExample()
        {
            // Messages to encrypt.
            var messages = new List <string> {
                "Now that the party is jumping",
                "With the bass kicked in, the fingers are pumpin'",
                "Quick to the point, to the point no faking",
                "I'm cooking MC's like a pound of bacon"
            };

            // Create a 32-byte key.
            var key = Aead.GenerateKey256();

            // Create a 8-byte STREAM nonce (required).
            var nonce = StreamEncryptor.GenerateNonce();

            // Create STREAM encryptor and decryptor using the AES-CMAC-SIV
            // algorithm. They implement the IDisposable interface,
            // so it's best to create them inside using statement.
            using (var encryptor = StreamEncryptor.CreateAesCmacSivEncryptor(key, nonce))
                using (var decryptor = StreamDecryptor.CreateAesCmacSivDecryptor(key, nonce))
                {
                    for (int i = 0; i < messages.Count; ++i)
                    {
                        // Calculate whether the message is the last message to encrypt.
                        bool last = i == messages.Count - 1;

                        // Convert the message to byte array first.
                        var bytes = Encoding.UTF8.GetBytes(messages[i]);

                        // Encrypt the message.
                        var ciphertext = encryptor.Seal(bytes, null, last);

                        // Decrypt the message.
                        var message = decryptor.Open(ciphertext, null, last);

                        // Convert the message back to string.
                        var plaintext = Encoding.UTF8.GetString(bytes);

                        // Print the decrypted message to the standard output.
                        Console.WriteLine(plaintext);
                    }
                }
        }