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;
                }
            }
        }
    }
  • 相关阅读:
    django 添加字段, migrate时候生成一个默认值
    django 开发经验
    GeoIP2-python
    一些好用的django模块
    ubuntu linux samba
    动画工作室
    以太坊dApp全栈开发教程(引言)
    社交网络去中心化已成趋势,读懂核心底层技术选择和路线
    CryptoCurrency Security Standard (CCSS)
    HARNESS THE POWER OF DISTRIBUTED STORAGE IPFS AND SWARM IN ETHEREUM BLOCKCHAIN APPLICATIONS
  • 原文地址:https://www.cnblogs.com/jimcsharp/p/5225249.html
Copyright © 2011-2022 走看看