其他分享
首页 > 其他分享> > TripleDESCryptoServiceProvider CryptoStream

TripleDESCryptoServiceProvider CryptoStream

作者:互联网

using System.Security.Cryptography;

 static void Main(string[] args)
        {
            string rawString = "Make every second count.";
            string encryptedString = Encrypt3DES(rawString);
            string decryptedString = Decrypt3DES(encryptedString);
            Console.WriteLine($"Raw:{rawString}\nencrypted:{encryptedString}\ndecrypted:{decryptedString}");
            Console.ReadLine();
        }

        public static string Encrypt3DES(string rawString, string encryptKey = "serenityserenityserenityserenity", string encryptIv = "04D4DA042DE=")
        {
            using (SymmetricAlgorithm tripleDesProvider = new TripleDESCryptoServiceProvider())
            {
                try
                {
                    tripleDesProvider.Key = Convert.FromBase64String(encryptKey);
                    tripleDesProvider.IV = Convert.FromBase64String(encryptIv);
                    tripleDesProvider.Mode = System.Security.Cryptography.CipherMode.CBC;
                    tripleDesProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                    using (ICryptoTransform ct = tripleDesProvider.CreateEncryptor(tripleDesProvider.Key, tripleDesProvider.IV))
                    {
                        byte[] bytes = Encoding.UTF8.GetBytes(rawString);
                        using (MemoryStream ms = new MemoryStream())
                        {
                            using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
                            {
                                cs.Write(bytes, 0, bytes.Length);
                                cs.FlushFinalBlock();
                                cs.Close();
                                string result = Convert.ToBase64String(ms.ToArray());
                                return result;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        public static string Decrypt3DES(string rawString, string encryptKey = "serenityserenityserenityserenity", string encryptIv = "04D4DA042DE=")
        {
            using (SymmetricAlgorithm tripleDesProvider = new TripleDESCryptoServiceProvider())
            {
                try
                {
                    tripleDesProvider.Key = Convert.FromBase64String(encryptKey);
                    tripleDesProvider.IV = Convert.FromBase64String(encryptIv);
                    tripleDesProvider.Mode = System.Security.Cryptography.CipherMode.CBC;
                    tripleDesProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                    using (ICryptoTransform ct = tripleDesProvider.CreateDecryptor(tripleDesProvider.Key, tripleDesProvider.IV))
                    {
                        byte[] bytes = Convert.FromBase64String(rawString);
                        using (MemoryStream ms = new MemoryStream())
                        {
                            using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
                            {
                                cs.Write(bytes, 0, bytes.Length);
                                cs.FlushFinalBlock();
                                cs.Close(); 
                                string result = Encoding.UTF8.GetString(ms.ToArray());
                                return result;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

 

标签:rawString,string,tripleDesProvider,new,using,cs,TripleDESCryptoServiceProvider,C
来源: https://www.cnblogs.com/Fred1987/p/12522676.html