字符串加密写入文本文件(02)

该方法没有经过测试。

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

namespace TBQDLKW
{
    /// <summary>
    /// 字符串加密
    /// </summary>
    public class DesCrypto
    {
        private static readonly byte[] defaultIV;
        static DesCrypto()
        {
            DesCrypto.defaultIV = new byte[8]{
                                                (byte) 18,
                                                (byte) 52,
                                                (byte) 86,
                                                (byte) 120,
                                                (byte) 144,
                                                (byte) 171,
                                                (byte) 205,
                                                (byte) 239
                                             };
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="encryptString">加密字符串</param>
        /// <returns></returns>
        public static string EncryptDES(string encryptString)
        {
            return EncryptDES(encryptString, "lncykangji");
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="decryptString">解密字符串</param>
        /// <returns></returns>
        public static string DecryptDES(string decryptString)
        {
            return DecryptDES(decryptString, "lncykangji");
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="encryptString">加密字符串</param>
        /// <param name="encryptKey">密钥</param>
        /// <returns></returns>
        private static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] bytes1 = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = DesCrypto.defaultIV;
                byte[] bytes2 = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider cryptoServiceProvider = new DESCryptoServiceProvider();
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, cryptoServiceProvider.CreateEncryptor(bytes1, rgbIV), CryptoStreamMode.Write);
                cryptoStream.Write(bytes2, 0, bytes2.Length);
                cryptoStream.FlushFinalBlock();
                return Convert.ToBase64String(memoryStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="decryptString">加密字串</param>
        /// <param name="decryptKey">密钥</param>
        /// <returns></returns>
        private static string DecryptDES(string decryptString, string decryptKey)
        {
            if (decryptString == "")
                return "";
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = DesCrypto.defaultIV;
                byte[] buffer = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider cryptoServiceProvider = new DESCryptoServiceProvider();
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, cryptoServiceProvider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write);
                cryptoStream.Write(buffer, 0, buffer.Length);
                cryptoStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/tbqdlkw/p/11807130.html