zoukankan      html  css  js  c++  java
  • MD5 加密 解密

    加密:

    View Code
     1 using System;
     2  using System.Collections.Generic;
     3  using System.Linq;
     4  using System.Text;
     5  using System.Security.Cryptography;
     6  using System.IO;
     7  
     8 namespace ENPOT.Manufacture.Security.DL.DataLogic
     9  {
    10      public class CryptUtil
    11      {
    12          static string encryptionKey = "12345678";
    13          static byte[] rgbKey = new byte[0];
    14          static byte[] rgbIV = new byte[] { 10, 20, 30, 40, 50, 60, 70, 80 };
    15  
    16       //编码
    17  
    18         public static string Encrypt(string textToEncrypt)
    19          {
    20              rgbKey = Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
    21              DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
    22              byte[] bytes = Encoding.UTF8.GetBytes(textToEncrypt);
    23              MemoryStream stream = new MemoryStream();
    24              CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
    25              stream2.Write(bytes, 0, bytes.Length);
    26              stream2.FlushFinalBlock();
    27              return Convert.ToBase64String(stream.ToArray());
    28          }

    解码:

    View Code
     1 public static string Decrypt(string textToDecrypt)
     2          {
     3              byte[] buffer = new byte[textToDecrypt.Length];
     4              rgbKey = Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
     5              DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
     6              buffer = Convert.FromBase64String(textToDecrypt);
     7              MemoryStream stream = new MemoryStream();
     8              CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
     9              stream2.Write(buffer, 0, buffer.Length);
    10              stream2.FlushFinalBlock();
    11              return Encoding.UTF8.GetString(stream.ToArray());
    12          }
    13      }
    14  
    15 }
  • 相关阅读:
    一致性哈希算法
    Tcp 3次握手 4次挥手
    计算机字符编码编年史
    虚拟机字节码指令表 JVM
    计算机是如何计算的、运行时栈帧分析(神奇i++续)
    神奇的i++
    记一次 springboot 参数解析 bug调试 HandlerMethodArgumentResolver
    String+、intern()、字符串常量池
    签名和加密的区别(详细)
    java之设计模式汇总
  • 原文地址:https://www.cnblogs.com/zxbzl/p/2961259.html
Copyright © 2011-2022 走看看