You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.4 KiB
C#
133 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Common
|
|
{
|
|
public enum EncryptionType
|
|
{
|
|
MD5,
|
|
NONE
|
|
}
|
|
|
|
public class Encryption
|
|
{
|
|
/// <summary>
|
|
/// AES加密
|
|
/// </summary>
|
|
/// <param name="text">明文</param>
|
|
/// <param name="password">密钥</param>
|
|
/// <param name="iv">向量</param>
|
|
/// <returns></returns>
|
|
public static string AESEncrypt(string text, string password)
|
|
{
|
|
|
|
//, string iv
|
|
RijndaelManaged rijndaelManaged = new RijndaelManaged();
|
|
rijndaelManaged.Mode = CipherMode.ECB;
|
|
rijndaelManaged.Padding = PaddingMode.PKCS7;
|
|
rijndaelManaged.KeySize = 128;
|
|
rijndaelManaged.BlockSize = 128;
|
|
|
|
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
|
|
byte[] keyBytes = new byte[16];
|
|
int len = pwdBytes.Length;
|
|
if (len > keyBytes.Length)
|
|
{
|
|
len = keyBytes.Length;
|
|
}
|
|
System.Array.Copy(pwdBytes, keyBytes, len);
|
|
rijndaelManaged.Key = keyBytes;
|
|
|
|
//byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
|
|
//rijndaelManaged.IV = ivBytes;
|
|
|
|
ICryptoTransform iCryptoTransform = rijndaelManaged.CreateEncryptor();
|
|
byte[] textBytes = Encoding.UTF8.GetBytes(text);
|
|
byte[] targetBytes = iCryptoTransform.TransformFinalBlock(textBytes, 0, textBytes.Length);
|
|
return Convert.ToBase64String(targetBytes);
|
|
|
|
}
|
|
/// <summary>
|
|
/// AES解密
|
|
/// </summary>
|
|
/// <param name="text">密文</param>
|
|
/// <param name="password">密钥</param>
|
|
/// <param name="iv">向量</param>
|
|
/// <returns></returns>
|
|
public static string AESDecrypt(string text, string password)
|
|
{
|
|
//, string iv
|
|
RijndaelManaged rijndaelManaged = new RijndaelManaged();
|
|
rijndaelManaged.Mode = CipherMode.ECB;
|
|
rijndaelManaged.Padding = PaddingMode.PKCS7;
|
|
rijndaelManaged.KeySize = 128;
|
|
rijndaelManaged.BlockSize = 128;
|
|
|
|
byte[] encryptedData = Convert.FromBase64String(text);
|
|
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
|
|
byte[] keyBytes = new byte[16];
|
|
int len = pwdBytes.Length;
|
|
if (len > keyBytes.Length)
|
|
{
|
|
len = keyBytes.Length;
|
|
}
|
|
System.Array.Copy(pwdBytes, keyBytes, len);
|
|
rijndaelManaged.Key = keyBytes;
|
|
|
|
//byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
|
|
//rijndaelManaged.IV = ivBytes;
|
|
|
|
ICryptoTransform iCryptoTransform = rijndaelManaged.CreateDecryptor();
|
|
byte[] targetBytes = iCryptoTransform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
|
|
return Encoding.UTF8.GetString(targetBytes);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// MD5加密
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static string MD5Encrypt(string str)
|
|
{
|
|
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
|
|
string returnStr = "";
|
|
byte[] strBytes = Encoding.UTF8.GetBytes(str);
|
|
byte[] binfile_md5_bytes = md5.ComputeHash(strBytes);
|
|
if (binfile_md5_bytes != null)
|
|
{
|
|
for (int i = 0; i < binfile_md5_bytes.Length; i++)
|
|
{
|
|
returnStr += binfile_md5_bytes[i].ToString("X2");
|
|
}
|
|
}
|
|
return returnStr;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// MD5加密
|
|
/// </summary>
|
|
/// <param name="b"></param>
|
|
/// <returns></returns>
|
|
public static string MD5Encrypt(byte[] b)
|
|
{
|
|
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
|
|
string returnStr = "";
|
|
byte[] binfile_md5_bytes = md5.ComputeHash(b);
|
|
if (binfile_md5_bytes != null)
|
|
{
|
|
for (int i = 0; i < binfile_md5_bytes.Length; i++)
|
|
{
|
|
returnStr += binfile_md5_bytes[i].ToString("X2");
|
|
}
|
|
}
|
|
|
|
return returnStr;
|
|
}
|
|
}
|
|
}
|