Пример #1
0
        public static byte[] Encrypt(string publicKey, byte[] data)
        {
            using (var aes = new AesCryptoServiceProvider())
            {
                aes.GenerateKey();
                aes.GenerateIV();

                using (var encryptor = aes.CreateEncryptor())
                    using (var ms = new MemoryStream())
                    {
                        ms.Write(Rsa.Encrypt(publicKey, aes.IV), 0, 128);
                        ms.Write(Rsa.Encrypt(publicKey, aes.Key), 0, 128);

                        using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                        {
                            cs.Write(data, 0, data.Length);
                        }
                        return(ms.ToArray());
                    }
            }
        }
Пример #2
0
        public static byte[] Decrypt(string privateKey, byte[] data)
        {
            using (var aes = new AesCryptoServiceProvider())
            {
                var iv  = new byte[128];
                var key = new byte[128];

                Buffer.BlockCopy(data, 0, iv, 0, 128);
                Buffer.BlockCopy(data, 128, key, 0, 128);

                using (var encryptor = aes.CreateDecryptor(Rsa.Decrypt(privateKey, key), Rsa.Decrypt(privateKey, iv)))
                    using (var src = new MemoryStream(data, writable: false))
                        using (var dst = new MemoryStream())
                        {
                            src.Seek(256, SeekOrigin.Begin);
                            using (var cs = new CryptoStream(src, encryptor, CryptoStreamMode.Read))
                            {
                                cs.CopyTo(dst);
                            }
                            return(dst.ToArray());
                        }
            }
        }
Пример #3
0
        public void rsa_encrypt_decrypt()
        {
            string publicKey, privateKey;

            Rsa.CreateKeys(out publicKey, out privateKey);

            Console.WriteLine();
            Console.WriteLine("public key:");
            Console.WriteLine(publicKey);

            Console.WriteLine();
            Console.WriteLine("private key:");
            Console.WriteLine(privateKey);

            for (var i = 0; i < 2; i++)
            {
                var encrypted = Rsa.Encrypt(publicKey, "hello world");

                Console.WriteLine();
                Console.WriteLine("encrypted:");
                Console.WriteLine(encrypted);

                Assert.Equal("hello world", Rsa.Decrypt(privateKey, encrypted));
            }

            for (var i = 0; i < 2; i++)
            {
                var longText  = "Nam illud iudico no. Est et oratio labore feugiat. Ius paulo dolores gloriatur ad. Eam senserit intellegebat cu, in error nusquam ponderum duo, paulo simul omnes cu his. Posse admodum ea duo.";
                var encrypted = RsaAes.Encrypt(publicKey, longText);

                Console.WriteLine();
                Console.WriteLine("encrypted:");
                Console.WriteLine(encrypted);

                Assert.Equal(longText, RsaAes.Decrypt(privateKey, encrypted));
            }
        }
Пример #4
0
 public static void CreateKeys(out string publicKey, out string privateKey)
 {
     Rsa.CreateKeys(out publicKey, out privateKey);
 }