zoukankan      html  css  js  c++  java
  • 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;
          }
        }
      }
    }
  • 相关阅读:
    mysql通过data目录恢复数据库
    CentOS安装TortoiseSVN svn 客户端
    CentOS上安装Node.js
    PHP--进行模块化设计
    PHP开发绝对不能违背的安全铁则
    达内培训:php在线端口扫描器
    使用 PHP 限制下载速度
    使用无限生命期Session的方法
    使用php作linux自动执行脚本
    腾讯星座运势api
  • 原文地址:https://www.cnblogs.com/allenfly/p/11492729.html
Copyright © 2011-2022 走看看