-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcryptor.cs
67 lines (53 loc) · 1.97 KB
/
cryptor.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
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace aes256withsalt
{
class cryptor
{
public static byte[] getdecryptor(string strkey, byte[] textByte, string strsalt)
{
byte [] keybyte = Encoding.UTF8.GetBytes(strkey);
byte [] saltbyte = null;
if (string.IsNullOrEmpty(strsalt))
{
saltbyte = new byte[] { 0x14, 0x00, 0x77, 0x55, 0x02, 0x04, 0x88, 0x99, 0x14, 0x50, 0x71, 0x55, 0x52, 0x04, 0x48, 0x00 };
}
else
{
saltbyte = Encoding.UTF8.GetBytes(strsalt);
}
byte[] encryptedfile = null;
try
{
using (var memmory = new MemoryStream())
{
using (var aes = new RijndaelManaged())
{
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 256;
aes.BlockSize = 128;
var key = new Rfc2898DeriveBytes(keybyte, saltbyte, 2000); //دمج
aes.Key = key.GetBytes(aes.KeySize / 8); //bytes
aes.IV = key.GetBytes(aes.BlockSize / 8);//bytes
using (var ICrypto = new CryptoStream(memmory, aes.CreateDecryptor(), CryptoStreamMode.Write))
{ //start decryption
ICrypto.Write(textByte, 0, textByte.Length);
//fininsh decrypting
ICrypto.FlushFinalBlock();
ICrypto.Close();
}
}
encryptedfile = memmory.ToArray();
memmory.Close();
}
return encryptedfile;
}
catch
{
return null;
}
}
}
}