mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 10:37:55 +08:00
全面修改Id为Guid类型,为2.0版做准备
This commit is contained in:
59
Infrastructure/Encryption.cs
Normal file
59
Infrastructure/Encryption.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Infrastructure
|
||||
{
|
||||
public class Encryption
|
||||
{
|
||||
private static string encryptKey = "4h!@w$rng,i#$@x1%)5^3(7*5P31/Ee0";
|
||||
|
||||
//默认密钥向量
|
||||
private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
|
||||
/// <summary>
|
||||
/// 加密
|
||||
/// </summary>
|
||||
/// <param name="encryptString"></param>
|
||||
/// <returns></returns>
|
||||
public static string Encrypt(string encryptString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(encryptString))
|
||||
return string.Empty;
|
||||
RijndaelManaged rijndaelProvider = new RijndaelManaged();
|
||||
rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
|
||||
rijndaelProvider.IV = Keys;
|
||||
ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
|
||||
|
||||
byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
|
||||
byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
|
||||
|
||||
return Convert.ToBase64String(encryptedData);
|
||||
}
|
||||
/// <summary>
|
||||
/// 解密
|
||||
/// </summary>
|
||||
/// <param name="decryptString"></param>
|
||||
/// <returns></returns>
|
||||
public static string Decrypt(string decryptString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(decryptString))
|
||||
return string.Empty;
|
||||
try
|
||||
{
|
||||
RijndaelManaged rijndaelProvider = new RijndaelManaged();
|
||||
rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
|
||||
rijndaelProvider.IV = Keys;
|
||||
ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
|
||||
|
||||
byte[] inputData = Convert.FromBase64String(decryptString);
|
||||
byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
|
||||
|
||||
return Encoding.UTF8.GetString(decryptedData);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user