using CSFrameworkV5.Core.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
|
namespace CSFrameworkV5.Core.Common
|
{
|
/// <summary>
|
/// 平台支持的对称加密类型
|
/// </summary>
|
public class KeyProvider
|
{
|
/// <summary>
|
/// 预设密钥,初始化系统时设置
|
/// </summary>
|
public static string DefaultKey { get; set; }
|
|
/// <summary>
|
/// 预设偏移量,初始化系统时设置
|
/// </summary>
|
public static string DefaultIV { get; set; }
|
|
/// <summary>
|
/// 预设对称加密工具
|
/// </summary>
|
public static IKeyProvider Default { get; set; }
|
}
|
|
/// <summary>
|
/// 对称加密密钥接口
|
/// </summary>
|
public interface IKeyProvider
|
{
|
/// <summary>
|
/// 密钥名称
|
/// </summary>
|
string CryptorName { get; }
|
|
string Key { get; }
|
|
string IV { get; }
|
|
string Encrypt(string content);
|
|
string Decrypt(string content);
|
}
|
|
public class KeyProviderRijndael : IKeyProvider
|
{
|
private string _Key = "";
|
private string _IV = "";
|
|
public KeyProviderRijndael(string key, string iv)
|
{
|
_Key = key;
|
_IV = iv;
|
}
|
|
public string CryptorName => "Rijndael";
|
|
public string Key => _Key;
|
|
public string IV => _IV;
|
|
public string Encrypt(string content)
|
{
|
return CryptoHelper.RijndaelEncrypt(content, _Key, _IV);
|
}
|
|
public string Decrypt(string content)
|
{
|
return CryptoHelper.RijndaelDecrypt(content, _Key, _IV);
|
}
|
}
|
|
//public class KeyProviderTripleDES : IKeyProvider
|
//{
|
// private string _Key = "";
|
// private string _IV = "";
|
|
// public KeyProviderTripleDES(string key, string iv)
|
// {
|
// this._Key = key;
|
// this._IV = iv;
|
// }
|
|
// public string CryptorName { get { return "TripleDES"; } }
|
|
// public string Key { get { return _Key; } }
|
|
// public string IV { get { return _IV; } }
|
|
// public string Encrypt(string content)
|
// {
|
// return CryptoHelper.TripleDESEncrypt(content, _Key, _IV);
|
// }
|
|
// public string Decrypt(string content)
|
// {
|
// return CryptoHelper.TripleDESDecrypt(content, _Key, _IV);
|
// }
|
//}
|
|
public class KeyProviderRSA : IKeyProvider
|
{
|
private string _Key = "";
|
private string _IV = "";
|
|
public KeyProviderRSA(string publicKey)
|
{
|
_Key = publicKey;
|
}
|
|
public string CryptorName => "RSA";
|
|
public string Key => _Key;
|
|
public string IV => _IV;
|
|
public string Encrypt(string content)
|
{
|
return CryptoHelper.RSAEncrypt(content, _Key);
|
}
|
|
public string Decrypt(string content)
|
{
|
return CryptoHelper.RSADecrypt(content, _Key);
|
}
|
}
|
}
|