forked from APIJSON/APIJSON-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleStringCipher.cs
More file actions
134 lines (117 loc) · 4.43 KB
/
SimpleStringCipher.cs
File metadata and controls
134 lines (117 loc) · 4.43 KB
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
132
133
134
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace APIJSON.Data;
public class SimpleStringCipher
{
public static SimpleStringCipher Instance { get; }
/// <summary>
/// This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.
/// This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be
/// 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array.
/// </summary>
public byte[] InitVectorBytes;
/// <summary>
/// Default password to encrypt/decrypt texts.
/// It's recommented to set to another value for security.
/// Default value: "gsKnGZ041HLL4IM8"
/// </summary>
public static string DefaultPassPhrase { get; set; }
/// <summary>
/// Default value: Encoding.ASCII.GetBytes("jkE49230Tf093b42")
/// </summary>
public static byte[] DefaultInitVectorBytes { get; set; }
/// <summary>
/// Default value: Encoding.ASCII.GetBytes("hgt!16kl")
/// </summary>
public static byte[] DefaultSalt { get; set; }
/// <summary>
/// This constant is used to determine the keysize of the encryption algorithm.
/// </summary>
public const int Keysize = 256;
static SimpleStringCipher()
{
DefaultPassPhrase = "gsKnGZ041HLL4IM9";
DefaultInitVectorBytes = Encoding.ASCII.GetBytes("jkE49230Tf093b42");
DefaultSalt = Encoding.ASCII.GetBytes("hgt!11kl");
Instance = new SimpleStringCipher();
}
public SimpleStringCipher()
{
InitVectorBytes = DefaultInitVectorBytes;
}
public string Encrypt(string plainText, string passPhrase = null, byte[] salt = null)
{
if (plainText == null)
{
return null;
}
if (passPhrase == null)
{
passPhrase = DefaultPassPhrase;
}
if (salt == null)
{
salt = DefaultSalt;
}
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (var password = new Rfc2898DeriveBytes(passPhrase, salt))
{
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = Aes.Create())
{
symmetricKey.Mode = CipherMode.CBC;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, InitVectorBytes))
{
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
var cipherTextBytes = memoryStream.ToArray();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
}
public string Decrypt(string cipherText, string passPhrase = null, byte[] salt = null)
{
if (string.IsNullOrEmpty(cipherText))
{
return null;
}
if (passPhrase == null)
{
passPhrase = DefaultPassPhrase;
}
if (salt == null)
{
salt = DefaultSalt;
}
var cipherTextBytes = Convert.FromBase64String(cipherText);
using (var password = new Rfc2898DeriveBytes(passPhrase, salt))
{
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = Aes.Create())
{
symmetricKey.Mode = CipherMode.CBC;
using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, InitVectorBytes))
{
using (var memoryStream = new MemoryStream(cipherTextBytes))
{
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
var plainTextBytes = new byte[cipherTextBytes.Length];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
}
}
}