• c# EncryptDES DecryptDES


    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace ClassEncrypt
    {
      internal class Class1
      {
        private static byte[] Keys = new byte[8]
        {
          (byte) 18,
          (byte) 52,
          (byte) 86,
          (byte) 120,
          (byte) 144,
          (byte) 171,
          (byte) 205,
          (byte) 239
        };
    
        public static string EncryptDES(string encryptString, string encryptKey)
        {
          try
          {
            byte[] bytes1 = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] rgbIV = Class1.Keys;
            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;
          }
        }
    
        public static string DecryptDES(string decryptString, string decryptKey)
        {
          try
          {
            byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
            byte[] rgbIV = Class1.Keys;
            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
          {
            return decryptString;
          }
        }
      }
    }
  • 相关阅读:
    中文分词资源
    软工第二次作业——数独生成器
    软工第一次作业?再次给自己一次定位!
    大一下学期的自我目标,上学期的小总结
    在Mac OS X上安装JavaHL
    在MONO下实现WCF所遇到的问题
    linux下允许root用户远程登录
    CentOS 6.0 安装 MONO 2.10.8
    Linux Vi命令
    CentOS 6.0 编译安装 MySQL 5.5.17
  • 原文地址:https://www.cnblogs.com/allenfly/p/11492729.html
Copyright © 2020-2023  润新知