1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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));
- var des = _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;
- }
- }
- }
|