12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace GCHR.Model
- {
- class CTripleDes
- {
- // define the triple des provider
- private readonly TripleDESCryptoServiceProvider _des =
- new TripleDESCryptoServiceProvider();
- // define the string handler
- private readonly UTF8Encoding _utf8 = new UTF8Encoding();
- // define the local property arrays
- 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)
- {
- // create the necessary streams
- var memStream = new MemoryStream();
- var cryptStream = new CryptoStream(memStream,
- cryptoTransform, CryptoStreamMode.Write);
- // transform the bytes as requested
- cryptStream.Write(input, 0, input.Length);
- cryptStream.FlushFinalBlock();
- // Read the memory stream and
- // convert it back into byte array
- memStream.Position = 0;
- var result = memStream.ToArray();
- // close and release the streams
- memStream.Close();
- cryptStream.Close();
- // hand back the encrypted buffer
- return result;
- }
- }
- }
|