using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Siia.Veima.Host.Utils
{
public class CommonFunction
{
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt32(string password)
{
//string cl = password;
//string pwd = "";
//MD5 md5 = MD5.Create(); //实例化一个md5对像
// // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
//byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
//// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
//for (int i = 0; i < s.Length; i++)
//{
// // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
// pwd = pwd + s[i].ToString("X");
//}
//return pwd;
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(password)));
t2 = t2.Replace("-", "");
return t2;
}
/// <summary>
/// 加密
/// </summary>
/// <param name="characters">要加密的明文</param>
/// <returns>加密后的密文</returns>
public static string EncryptStr(string characters)
{
string key = "330ead0b-4951-46a7-8db8-0e2569472fd1";
//位移加密
byte[] bStr = (new UnicodeEncoding()).GetBytes(characters);
for (int i = 0; i < bStr.Length; i++)
{
byte b = (byte)(bStr[i] + 255);
bStr[i] = b;
}
byte[] bKey = (new UnicodeEncoding()).GetBytes(key);//加密锁
//异或加密
for (int i = 0; i < bStr.Length; i += 2)
{
for (int j = 0; j < bKey.Length; j += 3)
{
bStr[i] = Convert.ToByte(bStr[i] ^ bKey[j]);
}
}
return (new UnicodeEncoding()).GetString(bStr).TrimEnd('\0');
}
/// <summary>
/// 解密
/// </summary>
/// <param name="ciphertext">要解密的密文</param>
/// <returns>解密后的明文</returns>
public static string DecryptStr(string ciphertext)
{
string key = "330ead0b-4951-46a7-8db8-0e2569472fd1";
byte[] bStr = (new UnicodeEncoding()).GetBytes(ciphertext);
byte[] bKey = (new UnicodeEncoding()).GetBytes(key);//加密锁
//异或解密
for (int i = 0; i < bStr.Length; i += 2)
{
for (int j = 0; j < bKey.Length; j += 3)
{
bStr[i] = Convert.ToByte(bStr[i] ^ bKey[j]);
}
}
//位移解密
for (int i = 0; i < bStr.Length; i++)
{
byte b = (byte)(bStr[i] - 255);
bStr[i] = b;
}
return (new UnicodeEncoding()).GetString(bStr).TrimEnd('\0');
}
//加密
public static string Encryption(string express)
{
CspParameters param = new CspParameters();
param.KeyContainerName = "siia";//密匙容器的名称,保持加密解密一致才能解密成功
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
byte[] plaindata = Encoding.Default.GetBytes(express);//将要加密的字符串转换为字节数组
byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据转换为新的加密字节数组
return Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串
}
}
//解密
public static string Decrypt(string ciphertext)
{
CspParameters param = new CspParameters();
param.KeyContainerName = "330ead0b-4951-46a7-8db8-0e2569472fd1";
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
byte[] encryptdata = Convert.FromBase64String(ciphertext);
byte[] decryptdata = rsa.Decrypt(encryptdata, false);
return Encoding.Default.GetString(decryptdata);
}
}
}
}