Revision | 7f2750fd5c26cfa43e981d435ee550b714dbd998 (tree) |
---|---|
Zeit | 2022-08-07 14:45:13 |
Autor | yoshy <yoshy.org.bitbucket@gz.j...> |
Commiter | yoshy |
[ADD] 暗号化ヘルパクラスを追加
@@ -0,0 +1,49 @@ | ||
1 | +using System.Security.Cryptography; | |
2 | +using System.Text; | |
3 | + | |
4 | +namespace CleanAuLait.Core.Converter | |
5 | +{ | |
6 | + public static class CryptoHelper | |
7 | + { | |
8 | + public static byte[] GenerateEntropy(int length) | |
9 | + { | |
10 | + return RandomNumberGenerator.GetBytes(length); | |
11 | + } | |
12 | + | |
13 | + public static byte[] ProtectData(string str, byte[] entropy) | |
14 | + { | |
15 | + if (str == null) | |
16 | + { | |
17 | + return null; | |
18 | + } | |
19 | + | |
20 | + if (entropy == null || entropy.Length == 0) | |
21 | + { | |
22 | + return null; | |
23 | + } | |
24 | + | |
25 | + byte[] data = Encoding.UTF8.GetBytes(str); | |
26 | + byte[] encrypted = ProtectedData.Protect(data, entropy, DataProtectionScope.CurrentUser); | |
27 | + | |
28 | + return encrypted; | |
29 | + } | |
30 | + | |
31 | + public static string UnprotectData(byte[] encrypted, byte[] entropy) | |
32 | + { | |
33 | + if (encrypted == null || encrypted.Length == 0) | |
34 | + { | |
35 | + return null; | |
36 | + } | |
37 | + | |
38 | + if (entropy == null || entropy.Length == 0) | |
39 | + { | |
40 | + return null; | |
41 | + } | |
42 | + | |
43 | + byte[] data = ProtectedData.Unprotect(encrypted, entropy, DataProtectionScope.CurrentUser); | |
44 | + string str = Encoding.UTF8.GetString(data); | |
45 | + | |
46 | + return str; | |
47 | + } | |
48 | + } | |
49 | +} |