12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace GCHR.Model
- {
- class CTripleDes
- {
-
- private readonly TripleDESCryptoServiceProvider _des =
- new TripleDESCryptoServiceProvider();
-
- private readonly UTF8Encoding _utf8 = new UTF8Encoding();
-
- private readonly byte[] _key;
- private readonly byte[] _iv;
- public CTripleDes(byte[] key, byte[] iv)
- {
- _key = key;
- _iv = iv;
- }
- public byte[] Encrypt(byte[] input)
- {
- return Transform(input,
- _des.CreateEncryptor(_key, _iv));
- }
- public byte[] Decrypt(byte[] input)
- {
- return Transform(input,
- _des.CreateDecryptor(_key, _iv));
- }
- public string Encrypt(string text)
- {
- var input = _utf8.GetBytes(text);
- var output = Transform(input,
- _des.CreateEncryptor(_key, _iv));
- return Convert.ToBase64String(output);
- }
- public string Decrypt(string text)
- {
- var input = Convert.FromBase64String(text);
- var output = Transform(input,
- _des.CreateDecryptor(_key, _iv));
- return _utf8.GetString(output);
- }
- private static byte[] Transform(byte[] input,
- ICryptoTransform cryptoTransform)
- {
-
- var memStream = new MemoryStream();
- var cryptStream = new CryptoStream(memStream,
- cryptoTransform, CryptoStreamMode.Write);
-
- cryptStream.Write(input, 0, input.Length);
- cryptStream.FlushFinalBlock();
-
-
- memStream.Position = 0;
- var result = memStream.ToArray();
-
- memStream.Close();
- cryptStream.Close();
-
- return result;
- }
- }
- }
|