-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathXts.cs
65 lines (59 loc) · 1.78 KB
/
Xts.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
using System;
using System.Security.Cryptography;
namespace XTSSharp;
public class Xts
{
private readonly SymmetricAlgorithm _key1;
private readonly SymmetricAlgorithm _key2;
protected Xts(Func<SymmetricAlgorithm> create, byte[] key1, byte[] key2)
{
if (create == null)
{
throw new ArgumentNullException(nameof(create));
}
if (key1 == null)
{
throw new ArgumentNullException(nameof(key1));
}
if (key2 == null)
{
throw new ArgumentNullException(nameof(key2));
}
_key1 = create();
_key2 = create();
if (key1.Length != key2.Length)
{
throw new ArgumentException("Key lengths don't match");
}
_key1.KeySize = key1.Length * 8;
_key2.KeySize = key2.Length * 8;
_key1.Key = key1;
_key2.Key = key2;
_key1.Mode = CipherMode.ECB;
_key2.Mode = CipherMode.ECB;
_key1.Padding = PaddingMode.None;
_key2.Padding = PaddingMode.None;
_key1.BlockSize = 128;
_key2.BlockSize = 128;
}
public XtsCryptoTransform CreateEncryptor()
{
return new XtsCryptoTransform(_key1.CreateEncryptor(), _key2.CreateEncryptor(), false);
}
public XtsCryptoTransform CreateDecryptor()
{
return new XtsCryptoTransform(_key1.CreateDecryptor(), _key2.CreateEncryptor(), true);
}
protected static byte[] VerifyKey(int expectedSize, byte[] key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (key.Length * 8 != expectedSize)
{
throw new ArgumentException($"Expected key length of {expectedSize} bits, got {key.Length * 8}");
}
return key;
}
}