1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
127
128
129
130
131
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);
        }
    }
}