73 lines
2.3 KiB
C#
73 lines
2.3 KiB
C#
using MathNet.Numerics.Random;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace New_College.Common
|
|
{
|
|
/// <summary>
|
|
/// aes解密
|
|
/// </summary>
|
|
public static class RSAHelper
|
|
{
|
|
/// <summary>
|
|
///aes解密
|
|
/// </summary>
|
|
/// <param name="text"></param>
|
|
/// <param name="AesKey"></param>
|
|
/// <param name="AesIV"></param>
|
|
/// <returns></returns>
|
|
public static string AESDecrypt(string text, string AesKey, string AesIV)
|
|
{
|
|
try
|
|
{
|
|
|
|
//判断是否是16位 如果不够补0
|
|
//text = tests(text);
|
|
//16进制数据转换成byte
|
|
byte[] encryptedData = Convert.FromBase64String(text); // strToToHexByte(text);
|
|
RijndaelManaged rijndaelCipher = new RijndaelManaged();
|
|
rijndaelCipher.Key = Convert.FromBase64String(AesKey); // Encoding.UTF8.GetBytes(AesKey);
|
|
rijndaelCipher.IV = Convert.FromBase64String(AesIV);// Encoding.UTF8.GetBytes(AesIV);
|
|
rijndaelCipher.Mode = CipherMode.CBC;
|
|
rijndaelCipher.Padding = PaddingMode.PKCS7;
|
|
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
|
|
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
|
|
string result = Encoding.Default.GetString(plainText);
|
|
//int index = result.LastIndexOf('>');
|
|
//result = result.Remove(index + 1);
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return null;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
///获取随机数
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetRandomStr()
|
|
{
|
|
const string baseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
Random random = new Random();
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < 16; i++)
|
|
{
|
|
int index = random.Next(baseChars.Length);
|
|
sb.Append(baseChars[index]);
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
}
|
|
}
|