cdk
2025-03-24 9694541db429678cbfcf8b769184856e6a6ab603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
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)
    {
        this._Key = key;
        this._IV = iv;
    }
 
    public string CryptorName { get { return "Rijndael"; } }
 
    public string Key { get { return _Key; } }
 
    public string IV { get { return _IV; } }
 
    public string Encrypt(string content)
    {
        return CryptoHelperService.RijndaelEncrypt(content, _Key, _IV);
    }
 
    public string Decrypt(string content)
    {
        return CryptoHelperService.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)
    {
        this._Key = publicKey;
    }
 
    public string CryptorName { get { return "RSA"; } }
 
    public string Key { get { return _Key; } }
 
    public string IV { get { return _IV; } }
 
    public string Encrypt(string content)
    {
        return CryptoHelperService.RSAEncrypt(content, _Key);
    }
 
    public string Decrypt(string content)
    {
        return CryptoHelperService.RSADecrypt(content, _Key);
    }
}