C# 简单的加密解密方法

C# 简单的加密解密方法
两个方法如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace databackupandfilebackup
{
    public class DESEncryptHelper
    {
        /// <summary>
        /// Encrypt string
        /// </summary>
        /// <param name="value">The string which to be encrypted.</param>
        /// <param name="encryptKey">The encrypt Key.</param>
        /// <returns>The value after being encrypted.</returns>
        public static string Encrypt(string value, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] inputByteArray = Encoding.UTF8.GetBytes(value);
                using (DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider())
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();
                        return Convert.ToBase64String(mStream.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                //throw new Exception("Error when encrypting data", ex);
                throw new ArgumentException(ex.Message, ex);
            }
        }
         
        /// <summary>
        /// Decrypt string
        /// </summary>
        /// <param name="value">The string which to be decrypted.</param>
        /// <param name="encryptKey">The encrypt Key.</param>
        /// <returns>The value after being decrypted.</returns>
        public static string Decrypt(string value, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] inputByteArray = Convert.FromBase64String(value);
                using (DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider())
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();
                        return Encoding.UTF8.GetString(mStream.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                //throw new Exception("Error when decrypting data", ex);
                throw new ArgumentException(ex.Message, ex);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u010833391/article/details/85097973