123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace Ragdoll
- {
- public class EnUtil
- {
- public static string En(string source)
- {
- const string key = "M6MoLC88bDjDE0qtoi4hlXNHfZ2Aa6bS";
- const string IV = "UjLbdUwyqdhp5pGf";
- return AesEncryptedString(source, key, IV);
- }
- public static string AesEncryptedString(string encryptStr, string key, string vector)
- {
- byte[] aesBytes = Encoding.UTF8.GetBytes(encryptStr);
- byte[] aesKey = new byte[32];
- Array.Copy(Encoding.UTF8.GetBytes(key), aesKey, aesKey.Length);
- byte[] aesVector = new byte[16];
- Array.Copy(Encoding.UTF8.GetBytes(vector), aesVector, aesVector.Length);
- Rijndael Aes = Rijndael.Create();
- //或者采用下方生成Aes
- //RijndaelManaged Aes = new();
- // 开辟一块内存流
- MemoryStream memoryStream = new MemoryStream();
- // 把内存流对象包装成加密流对象
- CryptoStream cryptoStream = new CryptoStream(memoryStream, Aes.CreateEncryptor(aesKey, aesVector),
- CryptoStreamMode.Write);
- // 明文数据写入加密流
- cryptoStream.Write(aesBytes, 0, aesBytes.Length);
- cryptoStream.FlushFinalBlock();
- string result = Convert.ToBase64String(memoryStream.ToArray());
- return result;
- }
- public static string Sha256Encrypt(string palinData, string salt = "cxhy")
- {
- if (string.IsNullOrWhiteSpace(palinData)) return null;
- using (SHA256 sha256 = new SHA256CryptoServiceProvider())
- {
- byte[] bytes = Encoding.UTF8.GetBytes(palinData + salt);
- byte[] sha256Bytes = sha256.ComputeHash(bytes);
- return Convert.ToBase64String(sha256Bytes);
- }
- }
- public static string AesAndSha256(string content)
- {
- return Sha256Encrypt(En(content));
- }
- }
- }
|