public string Decrypt(Secret secret)
        {
            string plainText = null;
            using (MemoryStream msDecrypt = new MemoryStream(secret.Data))
            {
                // Create an AesManaged object with the specified key and IV.
                using (Aes aesAlg = new AesManaged())
                {
                    aesAlg.Key = _key;
                    aesAlg.IV = secret.IV;

                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor();

                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt, Encoding.Unicode))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plainText = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return plainText;
        }
        public Secret Encrypt(string plainText)
        {
            Secret result = new Secret();

            using (MemoryStream ms = new MemoryStream())
            {
                using (Aes aesAlg = new AesManaged())
                {
                    aesAlg.Key = _key;
                    result.IV = aesAlg.IV;

                    ICryptoTransform encryptor = aesAlg.CreateEncryptor();

                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        byte[] rawPlaintext = Encoding.Unicode.GetBytes(plainText);
                        cs.Write(rawPlaintext, 0, rawPlaintext.Length);
                        cs.FlushFinalBlock();

                        // get the encrypted text
                        ms.Seek(0, SeekOrigin.Begin);
                        byte[] content = new byte[ms.Length];
                        ms.Read(content, 0, content.Length);
                        result.Data = content;
                    }
                }
            }

            return result;
        }
Beispiel #3
0
        public static string Decrypt(PublicSecret secret, string key)
        {
            Secret internalSecret = new Secret() {
                IV = Convert.FromBase64String(secret.IV),
                Data = Convert.FromBase64String(secret.Data)
            };

            AESEncryption encryptor = new AESEncryption(key);
            return encryptor.Decrypt(internalSecret);
        }