コード例 #1
0
        internal static (string output, string iv) Encrypt(string input, string password)
        {
            byte[] encrypted;
            byte[] IV;
            byte[] Salt = GetSalt();

            using (var aesAlg = Aes.Create())
            {
                aesAlg.Key     = CreateMD5(password);
                aesAlg.Mode    = CipherMode.CBC;
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.GenerateIV();
                IV = aesAlg.IV;

                var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (var memoryStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                    {
                        using (var streamWriter = new StreamWriter(cryptoStream))
                        {
                            streamWriter.Write(input);
                        }
                        encrypted = memoryStream.ToArray();
                    }
                }
            }

            return(Convert.ToBase64String(encrypted.ToArray()), Convert.ToBase64String(IV.ToArray()));
        }