scottgal

Compress and encrypt toy.

Feb 19th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.45 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6.  
  7. namespace CompressSample
  8. {
  9.     public class DecryptResult
  10.     {
  11.         public string DecryptedString { get; set; }
  12.  
  13.         public bool IsDecrypted { get; set; }
  14.     }
  15.  
  16.     public static class CryptoHelper
  17.     {
  18.         /// <summary>
  19.         ///     Optional start for the string which permits detection of encrypted files...
  20.         /// </summary>
  21.         private const string MapHeader = "";
  22.  
  23.         private static readonly byte[] Salt =
  24.         {
  25.             0x49, 0x46, 0x61, 0x6f, 0x21, 0x3d,
  26.             0x65, 0xE4, 0x76, 0x95, 0x64, 0x65, 0x86
  27.         };
  28.  
  29.         public static string EncryptAndCompressString(this string clearText, string password)
  30.         {
  31.             var mapText = clearText.Compress().Encrypt(password);
  32.             return MapHeader + mapText;
  33.         }
  34.  
  35.         public static bool IsEncryptedString(this string mapText)
  36.         {
  37.             if (string.IsNullOrEmpty(MapHeader)) return true;
  38.             return mapText.StartsWith(MapHeader);
  39.         }
  40.  
  41.         public static DecryptResult DecryptAndDecompress(this string mapText, string password)
  42.         {
  43.             var result = new DecryptResult();
  44.             if (!mapText.IsEncryptedString())
  45.             {
  46.                 result.IsDecrypted = false;
  47.                 result.DecryptedString = mapText;
  48.             }
  49.             var strippedMap = mapText.Substring(MapHeader.Length);
  50.             try
  51.             {
  52.                 result.DecryptedString = strippedMap.Decrypt(password).Decompress();
  53.                 result.IsDecrypted = true;
  54.             }
  55.             catch (Exception)
  56.             {
  57.                 result.IsDecrypted = false;
  58.             }
  59.  
  60.             return result;
  61.         }
  62.  
  63.         public static string Compress(this string text)
  64.         {
  65.             var buffer = Encoding.UTF8.GetBytes(text);
  66.             var ms = new MemoryStream();
  67.             using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
  68.             {
  69.                 zip.Write(buffer, 0, buffer.Length);
  70.             }
  71.  
  72.             ms.Position = 0;
  73.  
  74.             var compressed = new byte[ms.Length];
  75.             ms.Read(compressed, 0, compressed.Length);
  76.  
  77.             var gzBuffer = new byte[compressed.Length + 4];
  78.             Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
  79.             Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
  80.             return Convert.ToBase64String(gzBuffer);
  81.         }
  82.  
  83.         public static string Decompress(this string compressedText)
  84.         {
  85.             var gzBuffer = Convert.FromBase64String(compressedText);
  86.             using (var ms = new MemoryStream())
  87.             {
  88.                 var msgLength = BitConverter.ToInt32(gzBuffer, 0);
  89.                 ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
  90.  
  91.                 var buffer = new byte[msgLength];
  92.  
  93.                 ms.Position = 0;
  94.                 using (var zip = new GZipStream(ms, CompressionMode.Decompress))
  95.                 {
  96.                     zip.Read(buffer, 0, buffer.Length);
  97.                 }
  98.  
  99.                 return Encoding.UTF8.GetString(buffer);
  100.             }
  101.         }
  102.  
  103.  
  104.         public static string Encrypt(this string clearText, string password)
  105.         {
  106.             var clearBytes = Encoding.Unicode.GetBytes(clearText);
  107.             var pdb = new Rfc2898DeriveBytes(password,
  108.                 Salt);
  109.  
  110.             var encryptedData = Encrypt(clearBytes,
  111.                 pdb.GetBytes(32), pdb.GetBytes(16));
  112.  
  113.             return Convert.ToBase64String(encryptedData);
  114.         }
  115.  
  116.         public static byte[] Encrypt(byte[] clearData, byte[] key, byte[] iv)
  117.         {
  118.             byte[] encryptedData;
  119.             using (var ms = new MemoryStream())
  120.             {
  121.                 using (var alg = Rijndael.Create())
  122.                 {
  123.                     alg.Key = key;
  124.                     alg.IV = iv;
  125.                     using (var cs = new CryptoStream(ms,
  126.                         alg.CreateEncryptor(), CryptoStreamMode.Write))
  127.                     {
  128.                         cs.Write(clearData, 0, clearData.Length);
  129.                     }
  130.                     encryptedData = ms.ToArray();
  131.                 }
  132.             }
  133.             return encryptedData;
  134.         }
  135.  
  136.         public static string Decrypt(this string cipherText, string password)
  137.         {
  138.             var cipherBytes = Convert.FromBase64String(cipherText);
  139.             var pdb = new Rfc2898DeriveBytes(password, Salt);
  140.             var decryptedData = Decrypt(cipherBytes,
  141.                 pdb.GetBytes(32), pdb.GetBytes(16));
  142.             return Encoding.Unicode.GetString(decryptedData);
  143.         }
  144.  
  145.         public static byte[] Decrypt(byte[] cipherData, byte[] key, byte[] iv)
  146.         {
  147.             byte[] decryptedData;
  148.             using (var ms = new MemoryStream())
  149.             {
  150.                 using (var alg = Rijndael.Create())
  151.                 {
  152.                     alg.Key = key;
  153.                     alg.IV = iv;
  154.                     using (var cs = new CryptoStream(ms,
  155.                         alg.CreateDecryptor(), CryptoStreamMode.Write))
  156.                     {
  157.                         cs.Write(cipherData, 0, cipherData.Length);
  158.                     }
  159.                     decryptedData = ms.ToArray();
  160.                 }
  161.             }
  162.             return decryptedData;
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment