zhaolei
3 days ago 4a2e5b9a21940f11757be37d99f0944e240e908b
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
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
 
namespace Prow.Security.Cryptography
{
    /// <summary>
    /// 加密算法操作类
    /// </summary>
    public static class LgbCryptography
    {
        /// <summary>
        /// 生成 Salt 方法
        /// </summary>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string GenerateSalt(int length = 36)
        {
            var rng = new RNGCryptoServiceProvider();
            var saltBytes = new byte[length];
            rng.GetBytes(saltBytes);
            return Convert.ToBase64String(saltBytes);
        }
 
        /// <summary>
        /// 加密算法
        /// </summary>
        /// <param name="rgbKey">加密 Key</param>
        /// <param name="rgbIV">加密 IV</param>
        /// <param name="data">加密原始数据</param>
        /// <param name="algo">加密算法</param>
        /// <returns>密文集合</returns>
        public static byte[] Encrypt(byte[] rgbKey, byte[] rgbIV, byte[] data, SymmetricAlgorithm algo)
        {
            if (rgbKey == null) throw new ArgumentNullException(nameof(rgbKey));
            if (rgbIV == null) throw new ArgumentNullException(nameof(rgbIV));
            if (data == null) throw new ArgumentNullException(nameof(data));
            using var ms = new MemoryStream();
            using (var cs = new CryptoStream(ms, algo.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
            {
                cs.Write(data, 0, data.Length);
            }
            return ms.ToArray();
        }
 
        /// <summary>
        /// 解密算法
        /// </summary>
        /// <param name="rgbKey">解密 Key</param>
        /// <param name="rgbIV">解密 IV</param>
        /// <param name="cpText">解密原始数据</param>
        /// <param name="algo">解密算法</param>
        /// <returns>解密后数据集合</returns>
        public static byte[] Decrypt(byte[] rgbKey, byte[] rgbIV, byte[] cpText, SymmetricAlgorithm algo)
        {
            var buffer = new byte[200];
            using var reader = new MemoryStream();
            using (var ms = new MemoryStream(cpText))
            {
                using var cs = new CryptoStream(ms, algo.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Read);
                var i = 0;
                while ((i = cs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    reader.Write(buffer, 0, i);
                }
            }
            return reader.ToArray();
        }
 
        /// <summary>
        /// 计算哈希值方法 内部使用 SHA256CryptoServiceProvider 算法
        /// </summary>
        /// <param name="data">原始数据</param>
        /// <param name="salt">Salt 值</param>
        /// <returns></returns>
        public static string ComputeHash(string data, string salt) => ComputeHash(data, salt, new SHA256CryptoServiceProvider());
 
        /// <summary>
        /// 计算哈希值方法
        /// </summary>
        /// <param name="data">原始数据</param>
        /// <param name="salt">Salt 值</param>
        /// <param name="algorithm">算法实例</param>
        /// <returns></returns>
        public static string ComputeHash(string data, string salt, HashAlgorithm algorithm)
        {
            var passwordAndSaltBytes = Encoding.UTF8.GetBytes(data + salt);
            var hashBytes = algorithm.ComputeHash(passwordAndSaltBytes);
            return Convert.ToBase64String(hashBytes);
        }
    }
}