-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptoHelper.cs
78 lines (63 loc) · 2.29 KB
/
CryptoHelper.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
70
71
72
73
74
75
76
77
78
using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;
namespace SteamTokenDumper;
/// <summary>
/// AES/CBC/PKCS7, with a random IV prepended using AES/ECB/None
/// </summary>
internal static class CryptoHelper
{
private static readonly byte[] EncryptionKey = SHA256.HashData(Encoding.UTF8.GetBytes(string.Concat(nameof(SteamTokenDumper), GetMachineGuid())));
public static byte[] SymmetricEncrypt(ReadOnlySpan<byte> input)
{
using var aes = Aes.Create();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Key = EncryptionKey;
Span<byte> iv = stackalloc byte[16];
RandomNumberGenerator.Fill(iv);
var encryptedIv = aes.EncryptEcb(iv, PaddingMode.None);
var encryptedText = aes.EncryptCbc(input, iv, PaddingMode.PKCS7);
// final output is 16 byte ecb crypted IV + cbc crypted plaintext
var output = new byte[encryptedIv.Length + encryptedText.Length];
Array.Copy(encryptedIv, output, encryptedIv.Length);
Array.Copy(encryptedText, 0, output, encryptedIv.Length, encryptedText.Length);
return output;
}
public static byte[] SymmetricDecrypt(ReadOnlySpan<byte> input)
{
using var aes = Aes.Create();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Key = EncryptionKey;
// first 16 bytes of input is the ECB encrypted IV
Span<byte> iv = stackalloc byte[16];
aes.DecryptEcb(input[..iv.Length], iv, PaddingMode.None);
return aes.DecryptCbc(input[iv.Length..], iv, PaddingMode.PKCS7);
}
private static string GetMachineGuid()
{
if (!OperatingSystem.IsWindows())
{
return null;
}
#pragma warning disable CA1031 // Do not catch general exception types
try
{
using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
using var localKey = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography");
if (localKey == null)
{
return null;
}
var guid = localKey.GetValue("MachineGuid");
return guid?.ToString();
}
catch
{
return null;
}
#pragma warning restore CA1031
}
}