-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
KeyHelper.cs
69 lines (62 loc) · 1.9 KB
/
KeyHelper.cs
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
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using System.Security.Cryptography;
namespace Leosac.KeyManager.Library
{
public static class KeyHelper
{
public static uint GetBlockSize(IEnumerable<string> tags)
{
uint size = 8;
if (tags.Contains("AES"))
{
size = 16;
}
return size;
}
public static string? GetAlgorithmName(Key key)
{
string? algoName = null;
if (key.Tags.Contains("AES"))
{
algoName = "AES";
}
else if (key.Tags.Contains("DES") && key.KeySize > 8)
{
algoName = "DESEDE";
}
else if (key.Tags.Contains("DES"))
{
algoName = "DES";
}
else if (key.Tags.Contains("Blowfish"))
{
algoName = "Blowfish";
}
return algoName;
}
public static IBufferedCipher? GetSymmetricAlgorithm(Key key)
{
return GetSymmetricAlgorithm(key, CipherMode.CBC);
}
public static IBufferedCipher? GetSymmetricAlgorithm(Key key, CipherMode cipherMode)
{
return GetSymmetricAlgorithm(key, cipherMode, false);
}
public static IBufferedCipher? GetSymmetricAlgorithm(Key key, CipherMode cipherMode, bool noPadding)
{
IBufferedCipher? crypto = null;
var algoName = GetAlgorithmName(key);
if (!string.IsNullOrEmpty(algoName))
{
var fullName = algoName + "/" + cipherMode.ToString();
if (noPadding)
{
fullName += "/" + "NoPadding";
}
crypto = CipherUtilities.GetCipher(fullName);
}
return crypto;
}
}
}