-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoJS.cs
More file actions
149 lines (125 loc) · 5.3 KB
/
CryptoJS.cs
File metadata and controls
149 lines (125 loc) · 5.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace z.Data
{
public static class CryptoJS
{
public static string Encrypt(string Data, string Password, string Salt)
{
var compData = Data.CompressUriEncoded();
var keybytes = Encoding.UTF8.GetBytes(Password);
var iv = Encoding.UTF8.GetBytes(Salt);
return Convert.ToBase64String(CryptoJS.EncryptStringToBytes(compData, keybytes, iv));
//Data.CompressUriEncoded();
//var keybytes = Encoding.UTF8.GetBytes(Password);
//var iv = Encoding.UTF8.GetBytes(Salt);
//var compData = Convert.ToBase64String(CryptoJS.EncryptStringToBytes(Data, keybytes, iv));
//return compData; //.CompressUriEncoded();
}
public static string Decrypt(string Data, string Password, string Salt)
{
var keybytes = Encoding.UTF8.GetBytes(Password);
var iv = Encoding.UTF8.GetBytes(Salt);
var decrypt = CryptoJS.DecryptStringFromBytes(Convert.FromBase64String(Data), keybytes, iv);
return decrypt.CompressFromUriEncoded();
//var keybytes = Encoding.UTF8.GetBytes(Password);
//var iv = Encoding.UTF8.GetBytes(Salt);
////var compdata = Data //.CompressFromUriEncoded(); //CryptoJS.DecryptStringFromBytes(Convert.FromBase64String(Data), keybytes, iv);
//var decrypt = CryptoJS.DecryptStringFromBytes(Convert.FromBase64String(Data), keybytes, iv);
//return decrypt; //.CompressFromBase64() ;
}
public static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
{
throw new ArgumentNullException("plainText");
}
if (key == null || key.Length <= 0)
{
throw new ArgumentNullException("key");
}
if (iv == null || iv.Length <= 0)
{
throw new ArgumentNullException("key");
}
byte[] encrypted;
// Create a RijndaelManaged object
// with the specified key and IV.
using (var rijAlg = new RijndaelManaged())
{
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.FeedbackSize = 128;
rijAlg.Key = key;
rijAlg.IV = iv;
// Create a decrytor to perform the stream transform.
var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
private static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
{
throw new ArgumentNullException("cipherText");
}
if (key == null || key.Length <= 0)
{
throw new ArgumentNullException("key");
}
if (iv == null || iv.Length <= 0)
{
throw new ArgumentNullException("key");
}
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (var rijAlg = new RijndaelManaged())
{
//Settings
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.FeedbackSize = 128;
rijAlg.Key = key;
rijAlg.IV = iv;
// Create a decrytor to perform the stream transform.
var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}