-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathEncryption.cs
43 lines (34 loc) · 1.27 KB
/
Encryption.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace MemCacheDManager
{
public class Encryption
{
private static byte[] _key = System.Text.ASCIIEncoding.ASCII.GetBytes("somesimplekey!!!");
private static byte[] _iv = System.Text.ASCIIEncoding.ASCII.GetBytes("anothersomesimpl");
public static string Encrypt(string value)
{
RijndaelManaged rm = new RijndaelManaged();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, rm.CreateEncryptor(_key, _iv), CryptoStreamMode.Write);
StreamWriter streamWriter = new StreamWriter(cryptoStream);
streamWriter.Write(value);
streamWriter.Flush();
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
public static string Decrypt(string value)
{
if (value == String.Empty)
return value;
RijndaelManaged rm = new RijndaelManaged();
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(value));
CryptoStream cryptoStream = new CryptoStream(memoryStream, rm.CreateDecryptor(_key, _iv), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cryptoStream);
return streamReader.ReadToEnd();
}
}
}