gigahf

TinyCrypt.cs

Aug 12th, 2017
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1.     ///
  2.     /// <summary>
  3.     /// Encrypt and compress data in one. Invented by gigajew.
  4.     /// </summary>
  5.     ///
  6.     public class TinyCrypt {
  7.        
  8.         private Random _random;
  9.         private byte[] _key;
  10.        
  11.         public byte[] Key {
  12.             get {
  13.                 return _key;
  14.             }
  15.             private set {
  16.                 _key = value;
  17.             }
  18.         }
  19.        
  20.         public byte[] Encrypt(byte[] data, byte[] key = null) {
  21.             if(key != null) {
  22.                 Key = key;
  23.             }
  24.             using (MemoryStream memory = new MemoryStream()) {
  25.                 data = Compress(data);
  26.                 for (int i = 0; i < data.Length; i++)
  27.                 {
  28.                     data[i] ^= Key[i % Key.Length];
  29.                     memory.WriteByte(data[i]);
  30.                 }
  31.                 return memory.ToArray();
  32.             }
  33.         }
  34.        
  35.         public byte[] Decrypt(byte[] data, byte[] key = null) {
  36.             if(key != null) {
  37.                 Key = key;
  38.             }
  39.             using (MemoryStream memory = new MemoryStream(data)) {
  40.                 byte[] buffer = new byte[memory.Length];
  41.                 for(int i = 0 ; i < buffer.Length; i++) {
  42.                     buffer[i] = (byte) ((int) memory.ReadByte() ^ (int) Key[i % Key.Length]);
  43.                 }
  44.                 return Decompress(buffer);
  45.             }
  46.         }
  47.        
  48.         private byte[] Compress(byte[] data) {
  49.             using (MemoryStream output = new MemoryStream())
  50.             {
  51.                 using (GZipStream zip = new GZipStream(output, CompressionMode.Compress, true)) {
  52.                     zip.Write(data, 0, data.Length);
  53.                 }
  54.                 return output.ToArray();
  55.             }
  56.         }
  57.        
  58.         private byte[] Decompress(byte[] data)
  59.         {
  60.             using (MemoryStream memory = new MemoryStream(data))
  61.             using (GZipStream zip = new GZipStream(memory, CompressionMode.Decompress))
  62.             {
  63.                 const int size = 4096;
  64.                 byte[] buffer = new byte[size];
  65.                 using (MemoryStream output = new MemoryStream())
  66.                 {
  67.                     int count = 0;
  68.                     do
  69.                     {
  70.                         count = zip.Read(buffer, 0, size);
  71.                         if (count > 0)
  72.                         {
  73.                             output.Write(buffer, 0, count);
  74.                         }
  75.                     }
  76.                     while (count > 0);
  77.                     return output.ToArray();
  78.                 }
  79.             }
  80.         }
  81.        
  82.         public void RandomizeKey() {
  83.             _key = new byte[4096];
  84.             _random.NextBytes(_key);
  85.         }
  86.        
  87.         public void RandomizeSeed() {
  88.             _random = new Random(Guid.NewGuid().GetHashCode());
  89.         }
  90.        
  91.         public TinyCrypt() {
  92.             RandomizeSeed();
  93.             RandomizeKey();
  94.         }
  95.     }
Add Comment
Please, Sign In to add comment