-
Notifications
You must be signed in to change notification settings - Fork 10
/
encryptDecrypt.cs
executable file
·74 lines (54 loc) · 2.3 KB
/
encryptDecrypt.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
using System;
using System.Web;
using System.Security.Cryptography;
public class TestEncrypt
{
const String tokenKey = "x8FWQUedjyiUGlTf5appPQ==";
const String appCode = "DEMOAPP";
const String companyCode = "10";
const String version = "2";
const String method = "getStudents";
const String endPoint = "http://api.tasscloud.com.au/tassweb/api/";
public static void Main()
{
string original = "{\"currentstatus\":\"current\"}";
var UTF8 = new System.Text.UTF8Encoding();
var encString = Encrypt( original );
Console.WriteLine("Original: {0}", original );
Console.WriteLine("Encrypted: {0}", encString );
Console.WriteLine("Decrypted: {0}", Decrypt( encString ) );
Console.WriteLine("URL: {0}?method={1}&appcode={2}&company={3}&v={4}&token={5}", endPoint , method , appCode , companyCode , version , HttpUtility.UrlEncode ( encString ) );
}
public static string Encrypt (string toEncrypt)
{
var UTF8 = new System.Text.UTF8Encoding();
// The Token Key is already Base64.
byte[] keyArray = Convert.FromBase64String(tokenKey);
// The Paramater String is UTF8.
byte[] toEncryptArray = UTF8.GetBytes(toEncrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.BlockSize = 128;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor( keyArray , null );
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt (string toDecrypt)
{
var UTF8 = new System.Text.UTF8Encoding();
// The Token Key is already Base64.
byte[] keyArray = Convert.FromBase64String(tokenKey);
// The encrypted string is already Base64.
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.BlockSize = 128;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor( keyArray , null );
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8.GetString(resultArray);
}
}