zoukankan      html  css  js  c++  java
  • Nido.Common.Utilities.MD5类

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace Nido.Common.Utilities.MD5
    {
        public class Crypto
        {
            static readonly string PasswordHash = "P@@Sw0rd";
            static readonly string SaltKey = "S@LT&KEY";
            static readonly string VIKey = "@1B2c3D4e5F6g7H8";
    
            public static string Encrypt(string plainText)
            {
                try
                {
                    byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
    
                    byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
                    var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
                    var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
    
                    byte[] cipherTextBytes;
    
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            cipherTextBytes = memoryStream.ToArray();
                            cryptoStream.Close();
                        }
                        memoryStream.Close();
                    }
                    return Convert.ToBase64String(cipherTextBytes);
                }
                catch
                {
                    return plainText;
                }
            }
    
            public static string Decrypt(string encryptedText)
            {
                try
                {
                    byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
                    byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
                    var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
    
                    var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
                    var memoryStream = new MemoryStream(cipherTextBytes);
                    var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
                    byte[] plainTextBytes = new byte[cipherTextBytes.Length];
    
                    int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                    memoryStream.Close();
                    cryptoStream.Close();
                    return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("".ToCharArray());
                }
                catch
                {
                    return encryptedText;
                }
            }
        }
    }
  • 相关阅读:
    梯度下降_机器学习-李宏毅
    LeTex算法伪代码环境
    数据结构之线性表
    Java中的初始化块、构造器、静态初始化块的执行顺序
    Java中的内省(introspector)
    JSP (Java Server Page)
    eclipse的web工程默认部署到了哪里
    Persistence机制(永久保存/序列化Serialize)
    VC++中使用正则表达式RegExp
    Java中解析和生成xml
  • 原文地址:https://www.cnblogs.com/jimcsharp/p/5225249.html
Copyright © 2011-2022 走看看