-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
CryptExtensions.cs
131 lines (104 loc) · 3.8 KB
/
CryptExtensions.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.IO;
using System.Security.Cryptography;
using Aes = System.Security.Cryptography.Aes;
namespace UVtools.Core.Extensions;
public static class CryptExtensions
{
public static readonly SHA1 SHA1Instance = SHA1.Create();
public static readonly MD5 MD5Instance = MD5.Create();
public static string ComputeSHA1Hash(byte[] input)
{
return Convert.ToBase64String(SHA1Instance.ComputeHash(input));
}
public static string ComputeMD5Hash(byte[] input)
{
return Convert.ToBase64String(MD5Instance.ComputeHash(input));
}
public static string ComputeCRC32Hash(byte[] input)
{
return Convert.ToBase64String(MD5Instance.ComputeHash(input));
}
public static readonly SHA256 SHA256Instance = SHA256.Create();
public static byte[] ComputeSHA256Hash(byte[] input)
{
return SHA256Instance.ComputeHash(input);
}
public static byte[] AesCryptBytes(byte[] data, byte[] key, CipherMode mode, PaddingMode paddingMode, bool encrypt, byte[]? iv = null)
{
if (data.Length % 16 != 0)
{
Array.Resize(ref data, ((data.Length / 16) + 1) * 16);
}
var aes = Aes.Create();
aes.KeySize = key.Length * 8;
aes.Key = key;
aes.Padding = paddingMode;
aes.Mode = mode;
if (iv != null)
{
aes.IV = iv;
}
var cryptor = encrypt ? aes.CreateEncryptor() : aes.CreateDecryptor();
using var msDecrypt = new MemoryStream(data);
using var csDecrypt = new CryptoStream(msDecrypt, cryptor, CryptoStreamMode.Read);
var outputBuffer = new byte[data.Length];
csDecrypt.Read(outputBuffer, 0, data.Length);
return outputBuffer;
}
public static MemoryStream AesCryptMemoryStream(byte[] data, byte[] key, CipherMode mode, PaddingMode paddingMode, bool encrypt, byte[]? iv = null)
=> new(AesCryptBytes(data, key, mode, paddingMode, encrypt, iv));
public static string Base64EncodeString(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}
public static string Base64DecodeString(string base64EncodedData)
{
var base64EncodedBytes = Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
public static string XORCipherString(string text, string key)
{
var output = new char[text.Length];
for (int i = 0; i < text.Length; i++)
{
output[i] = (char)(text[i] ^ key[i % key.Length]);
}
return new string(output);
}
public static string XORCipherString(byte[] bytes, string key)
{
var output = new char[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
{
output[i] = (char)(bytes[i] ^ key[i % key.Length]);
}
return new string(output);
}
public static byte[] XORCipher(string text, string key)
{
var output = new byte[text.Length];
for (int i = 0; i < text.Length; i++)
{
output[i] = (byte)(text[i] ^ key[i % key.Length]);
}
return output;
}
public static byte[] XORCipher(byte[] bytes, string key)
{
var output = new byte[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
{
output[i] = (byte)(bytes[i] ^ key[i % key.Length]);
}
return output;
}
}