zoukankan      html  css  js  c++  java
  • 数据操作DES加密和解密

     #region 数据操作DES加密和解密
        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString"> 待加??密??的??字??符??串?? </param>
        /// <param name="encryptKey"> 加??密??密??钥?,要?a求??为a8位? </param>
        /// <returns> 加??密??成??功|返???回?加??密??后??的??字??符??串??,??失???败???返???回?源??串?? </returns>
        public static string EncryptDES( string encryptString, string encryptKey)
        {
            byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byte[] rgbKey = Encoding .UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding .UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                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
            {
                return encryptString;
            }
        }
     
     
     
        /// <summary>
        /// DES解a密??字??符??串??
        /// </summary>
        /// <param name="decryptString"> 待?y解a密??的??字??符??串?? </param>
        /// <param name="decryptKey"> 解a密??密??钥?,要?a求??为a8位?,和??加??密??密??钥?相??同?? </param>
        /// <returns> 解a密??成??功|返???回?解a密??后??的??字??符??串??,??失???败???返???源??串?? </returns>
        public static string DecryptDES( string decryptString, string decryptKey)
        {
            byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
     
            try
            {
                byte[] rgbKey = Encoding .UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert .FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                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
            {
                return decryptString;
            }
        }
     
        #endregion
     
     
     
      /// <summary>
        /// DES解a密??字??符??串??
        /// </summary>
        /// <param name="decryptString"> 待解密的字符串 </param>
        /// <param name="decryptKey"> 解密密钥要求为8位和加密密钥相同</param>
        /// <returns> 解a密成功|返回?解a密??后??的??字??符??串??,??失???败???返???源??串?? </returns>
        public static string DecryptDES( string decryptString, string decryptKey)
        {
            byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
     
            try
            {
                byte[] rgbKey = Encoding .UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert .FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                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
            {
                return decryptString;
            }
        }
     
        #endregion
  • 相关阅读:
    Mysql的ONLY_FULL_GROUP_BY
    Redis报错“ OOM command not allowed when used memory > 'maxmemory' ”
    redis发布订阅客户端报错
    使用IDEA远程调试SpringBoot程序
    kafk学习笔记(一)
    Ubuntu中卸载node和npm并重装
    一些常用的类型转换
    一台电脑配置多个GigHub账号
    百度网盘文件直接下载方法(跳过每次需要启动百度网盘下载的方式)
    如何激活IDEA工具,亲测有效
  • 原文地址:https://www.cnblogs.com/BungeeJumping/p/3397300.html
Copyright © 2011-2022 走看看