You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

59 lines
2.0 KiB

/*******************************************************************************
* Copyright © 2017-2020 Znyc.Cloudcar.Admin.Framework 版权所有
* Author: Znyc
* Description: Znyc快速开发平台
* Website:http://www.Znyc.Cloudcar.Admin.com
*********************************************************************************/
using System;
using System.Security.Cryptography;
using System.Text;
namespace Znyc.Cloudcar.Admin.Commons
{
/// <summary>
/// 用于webapi 生成票据使用,公开
/// </summary>
public sealed class Cryptography
{
/// <summary>
/// </summary>
/// <param name="tmpStr"></param>
/// <param name="encodingAESKey"></param>
/// <returns></returns>
public static string AES_encrypt(string tmpStr, string encodingAESKey)
{
string str = tmpStr + GetMD5_32(encodingAESKey);
return GetMD5_32(SHA256(str)) + GetMD5_32(tmpStr);
}
/// <summary>
/// SHA256函数
/// </summary>
/// <param name="str">原始字符串</param>
/// <returns>SHA256结果(返回长度为44字节的字符串)</returns>
public static string SHA256(string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
/// <summary>
/// 获得32位的MD5加密
/// </summary>
public static string GetMD5_32(string input)
{
MD5 md5 = MD5.Create();
byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.Append(data[i].ToString("x2"));
}
return sb.ToString();
}
}
}