-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathTripleDESStringEncryptor.cs
62 lines (55 loc) · 2.08 KB
/
TripleDESStringEncryptor.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
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SirenOfShame.Lib.Settings
{
public class TripleDesStringEncryptor
{
private readonly byte[] _key;
private readonly byte[] _iv;
private readonly TripleDESCryptoServiceProvider _provider;
public TripleDesStringEncryptor()
{
_key = Encoding.ASCII.GetBytes("GSYAHAGCBDUUADIADKOPAAAW");
_iv = Encoding.ASCII.GetBytes("USAZBGAW");
_provider = new TripleDESCryptoServiceProvider();
}
public string EncryptString(string text)
{
ICryptoTransform transform = _provider.CreateEncryptor(_key, _iv);
if (string.IsNullOrWhiteSpace(text))
{
return null;
}
using (MemoryStream stream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
{
byte[] input = Encoding.UTF8.GetBytes(text);
cryptoStream.Write(input, 0, input.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(stream.ToArray());
}
}
}
public string DecryptString(string text)
{
ICryptoTransform transform = _provider.CreateDecryptor(_key, _iv);
if (string.IsNullOrWhiteSpace(text))
{
return null;
}
using (MemoryStream stream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
{
byte[] input = Convert.FromBase64String(text);
cryptoStream.Write(input, 0, input.Length);
cryptoStream.FlushFinalBlock();
return Encoding.UTF8.GetString(stream.ToArray());
}
}
}
}
}