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.
164 lines
5.6 KiB
164 lines
5.6 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.Encrypt
|
|
{
|
|
/// <summary>
|
|
/// Encrypt 的加密/解密。
|
|
/// </summary>
|
|
public class DEncrypt
|
|
{
|
|
#region 使用 缺省密钥字符串 加密/解密string
|
|
|
|
/// <summary>
|
|
/// 使用缺省密钥字符串加密string
|
|
/// </summary>
|
|
/// <param name="original">明文</param>
|
|
/// <returns>密文</returns>
|
|
public static string Encrypt(string original)
|
|
{
|
|
return Encrypt(original, "ZnycSOFT");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用缺省密钥字符串解密string
|
|
/// </summary>
|
|
/// <param name="original">密文</param>
|
|
/// <returns>明文</returns>
|
|
public static string Decrypt(string original)
|
|
{
|
|
return Decrypt(original, "ZnycSOFT", Encoding.Default);
|
|
}
|
|
|
|
#endregion 使用 缺省密钥字符串 加密/解密string
|
|
|
|
#region 使用 给定密钥字符串 加密/解密string
|
|
|
|
/// <summary>
|
|
/// 使用给定密钥字符串加密string
|
|
/// </summary>
|
|
/// <param name="original">原始文字</param>
|
|
/// <param name="key">密钥</param>
|
|
/// <returns>密文</returns>
|
|
public static string Encrypt(string original, string key)
|
|
{
|
|
byte[] buff = Encoding.Default.GetBytes(original);
|
|
byte[] kb = Encoding.Default.GetBytes(key);
|
|
return Convert.ToBase64String(Encrypt(buff, kb));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用给定密钥字符串解密string
|
|
/// </summary>
|
|
/// <param name="original">密文</param>
|
|
/// <param name="key">密钥</param>
|
|
/// <returns>明文</returns>
|
|
public static string Decrypt(string original, string key)
|
|
{
|
|
return Decrypt(original, key, Encoding.Default);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用给定密钥字符串解密string,返回指定编码方式明文
|
|
/// </summary>
|
|
/// <param name="encrypted">密文</param>
|
|
/// <param name="key">密钥</param>
|
|
/// <param name="encoding">字符编码方案</param>
|
|
/// <returns>明文</returns>
|
|
public static string Decrypt(string encrypted, string key, Encoding encoding)
|
|
{
|
|
byte[] buff = Convert.FromBase64String(encrypted);
|
|
byte[] kb = Encoding.Default.GetBytes(key);
|
|
return encoding.GetString(Decrypt(buff, kb));
|
|
}
|
|
|
|
#endregion 使用 给定密钥字符串 加密/解密string
|
|
|
|
#region 使用 缺省密钥字符串 加密/解密/byte[]
|
|
|
|
/// <summary>
|
|
/// 使用缺省密钥字符串解密byte[]
|
|
/// </summary>
|
|
/// <param name="encrypted">密文</param>
|
|
/// <param name="key">密钥</param>
|
|
/// <returns>明文</returns>
|
|
public static byte[] Decrypt(byte[] encrypted)
|
|
{
|
|
byte[] key = Encoding.Default.GetBytes("ZnycSOFT");
|
|
return Decrypt(encrypted, key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用缺省密钥字符串加密
|
|
/// </summary>
|
|
/// <param name="original">原始数据</param>
|
|
/// <param name="key">密钥</param>
|
|
/// <returns>密文</returns>
|
|
public static byte[] Encrypt(byte[] original)
|
|
{
|
|
byte[] key = Encoding.Default.GetBytes("ZnycSOFT");
|
|
return Encrypt(original, key);
|
|
}
|
|
|
|
#endregion 使用 缺省密钥字符串 加密/解密/byte[]
|
|
|
|
#region 使用 给定密钥 加密/解密/byte[]
|
|
|
|
/// <summary>
|
|
/// 生成MD5摘要
|
|
/// </summary>
|
|
/// <param name="original">数据源</param>
|
|
/// <returns>摘要</returns>
|
|
public static byte[] MakeMD5(byte[] original)
|
|
{
|
|
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
|
|
byte[] keyhash = hashmd5.ComputeHash(original);
|
|
hashmd5 = null;
|
|
return keyhash;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用给定密钥加密
|
|
/// </summary>
|
|
/// <param name="original">明文</param>
|
|
/// <param name="key">密钥</param>
|
|
/// <returns>密文</returns>
|
|
public static byte[] Encrypt(byte[] original, byte[] key)
|
|
{
|
|
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider
|
|
{
|
|
Key = MakeMD5(key),
|
|
Mode = CipherMode.ECB
|
|
};
|
|
|
|
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用给定密钥解密数据
|
|
/// </summary>
|
|
/// <param name="encrypted">密文</param>
|
|
/// <param name="key">密钥</param>
|
|
/// <returns>明文</returns>
|
|
public static byte[] Decrypt(byte[] encrypted, byte[] key)
|
|
{
|
|
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider
|
|
{
|
|
Key = MakeMD5(key),
|
|
Mode = CipherMode.ECB
|
|
};
|
|
|
|
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
|
|
}
|
|
|
|
#endregion 使用 给定密钥 加密/解密/byte[]
|
|
}
|
|
}
|