Advertisement
Guest User

Untitled

a guest
Jun 27th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5.  
  6. namespace ProbeConnect
  7. {
  8.     class Crypter
  9.     {
  10.         private const string EncPassword = "bavEccyc%sabHij$onLoa$$$wAwC/oachH(iQuivC)ankUng";
  11.  
  12.         public static String Encrypt(String str)
  13.         {
  14.             const string password = EncPassword;
  15.  
  16.             var pdb = new PasswordDeriveBytes(password,
  17.             new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
  18.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  19.  
  20.             var key = pdb.GetBytes(16);
  21.             var iv = pdb.GetBytes(8);
  22.  
  23.             var des = new CTripleDes(key, iv);
  24.  
  25.             try
  26.             {
  27.                 return des.Encrypt(str);
  28.             }
  29.             catch
  30.             {
  31.                 return "error encrypting content";
  32.             }
  33.         }
  34.  
  35.         public static String Decrypt(String str)
  36.         {
  37.             const string password = EncPassword;
  38.  
  39.             var pdb = new PasswordDeriveBytes(password,
  40.             new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
  41.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  42.  
  43.             byte[] key = pdb.GetBytes(16);
  44.             byte[] iv = pdb.GetBytes(8);
  45.             CTripleDes des = new CTripleDes(key, iv);
  46.  
  47.             try
  48.             {
  49.                 return des.Decrypt(str);
  50.             }
  51.             catch
  52.             {
  53.                 return "error decrypting content";
  54.             }
  55.         }
  56.  
  57.         #region Cryptography alogrithm
  58.         class CTripleDes
  59.         {
  60.             private TripleDESCryptoServiceProvider m_des =
  61.                      new TripleDESCryptoServiceProvider();
  62.  
  63.             private UTF8Encoding m_utf8 = new UTF8Encoding();
  64.  
  65.             private byte[] m_key;
  66.             private byte[] m_iv;
  67.  
  68.             public CTripleDes(byte[] key, byte[] iv)
  69.             {
  70.                 this.m_key = key;
  71.                 this.m_iv = iv;
  72.             }
  73.  
  74.             public byte[] Encrypt(byte[] input)
  75.             {
  76.                 return Transform(input,
  77.                        m_des.CreateEncryptor(m_key, m_iv));
  78.             }
  79.  
  80.             public byte[] Decrypt(byte[] input)
  81.             {
  82.                 return Transform(input,
  83.                        m_des.CreateDecryptor(m_key, m_iv));
  84.             }
  85.  
  86.             public string Encrypt(string text)
  87.             {
  88.                 byte[] input = m_utf8.GetBytes(text);
  89.                 byte[] output = Transform(input,
  90.                                 m_des.CreateEncryptor(m_key, m_iv));
  91.                 return Convert.ToBase64String(output);
  92.             }
  93.  
  94.             public string Decrypt(string text)
  95.             {
  96.                 byte[] input = Convert.FromBase64String(text);
  97.                 byte[] output = Transform(input,
  98.                                 m_des.CreateDecryptor(m_key, m_iv));
  99.                 return m_utf8.GetString(output);
  100.             }
  101.  
  102.             private byte[] Transform(byte[] input,
  103.                            ICryptoTransform CryptoTransform)
  104.             {
  105.                 MemoryStream memStream = new MemoryStream();
  106.                 CryptoStream cryptStream = new CryptoStream(memStream,
  107.                              CryptoTransform, CryptoStreamMode.Write);
  108.                 cryptStream.Write(input, 0, input.Length);
  109.                 cryptStream.FlushFinalBlock();
  110.                 memStream.Position = 0;
  111.                 byte[] result = memStream.ToArray();
  112.                 memStream.Close();
  113.                 cryptStream.Close();
  114.                 return result;
  115.             }
  116.         }
  117.         #endregion
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement