forked from emtek-at/GreeAC-DummyServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crypter.cs
56 lines (47 loc) · 1.78 KB
/
Crypter.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
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace DummyServer
{
public class Crypter
{
static string m_Key = "a3K8Bx%2r8Y7#xDh";
public static string Decrypt(string pack, string key)
{
if (key == "")
key = Crypter.m_Key;
var myaes = Aes.Create();
myaes.Mode = CipherMode.ECB;
myaes.Key = Encoding.UTF8.GetBytes(key);
myaes.Padding = PaddingMode.PKCS7;
myaes.GenerateIV();
var decryptor = myaes.CreateDecryptor(myaes.Key, myaes.IV);
var msDecrypt = new MemoryStream(Convert.FromBase64String(pack));
var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
var srDecrypt = new StreamReader(csDecrypt);
var plaintext = srDecrypt.ReadToEnd();
return plaintext;
}
public static string Encrypt(string pack, string key)
{
if (key == "")
key = Crypter.m_Key;
var myaes = Aes.Create();
myaes.Mode = CipherMode.ECB;
myaes.Key = Encoding.UTF8.GetBytes(key);
myaes.Padding = PaddingMode.PKCS7;
myaes.GenerateIV();
var encryptor = myaes.CreateEncryptor(myaes.Key, myaes.IV);
var msEncrypt = new MemoryStream();
var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
var swEncrypt = new StreamWriter(csEncrypt);
swEncrypt.Write(pack);
swEncrypt.Flush();
swEncrypt.Close();
msEncrypt.Flush();
var encrypted = Convert.ToBase64String(msEncrypt.ToArray());
return encrypted;
}
}
}