Encrypted binary serialization

C/C++ — Tags :, — admin @ 0:18

For re-use: a small class that does encryption and binary serialization:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Security.Cryptography;
  7. using System.Windows.Forms;
  8.  
  9. namespace MyNamespace
  10. {
  11.     class Tools
  12.     {
  13.         // change me…
  14.         private static string m_encryptionKey = "password";
  15.  
  16.         public static byte[] Encrypt(byte[] plainData, string sKey)
  17.         {
  18.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  19.             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  20.             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  21.             ICryptoTransform desencrypt = DES.CreateEncryptor();
  22.             byte[] encryptedData = desencrypt.TransformFinalBlock(plainData, 0, plainData.Length);
  23.             return encryptedData;
  24.         }
  25.  
  26.         public static byte[] Decrypt(byte[] encryptedData, string sKey)
  27.         {
  28.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  29.             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  30.             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  31.             ICryptoTransform desDecrypt = DES.CreateDecryptor();
  32.             byte[] decryptedData = desDecrypt.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
  33.             return decryptedData;
  34.         }
  35.  
  36.         public static void SaveObjectToFile(object obj, string path){
  37.             try
  38.             {
  39.                 MemoryStream memStream = new MemoryStream();
  40.                 BinaryFormatter binFormatter = new BinaryFormatter();
  41.                 binFormatter.Serialize(memStream, obj);
  42.                 byte[] encryptedBytes = Encrypt(memStream.ToArray(), m_encryptionKey);
  43.                 memStream.Close();
  44.                 Stream streamToFile = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  45.                 streamToFile.Write(encryptedBytes, 0, encryptedBytes.Length);
  46.                 streamToFile.Flush();
  47.                 streamToFile.Close();
  48.             }
  49.             catch (Exception e)
  50.             {
  51.                 MessageBox.Show(e.Message);
  52.             }
  53.         }
  54.  
  55.         public static object LoadObjectFromFile(string path)
  56.         {
  57.             try
  58.             {
  59.  
  60.                 Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  61.                 byte[] encryptedObj = new byte[fileStream.Length];
  62.                 fileStream.Read(encryptedObj, 0, (int)encryptedObj.Length);
  63.                 MemoryStream memStream = new MemoryStream(Decrypt(encryptedObj, m_encryptionKey));
  64.                 BinaryFormatter binFormatter = new BinaryFormatter();
  65.                 object decryptedObj = binFormatter.Deserialize(memStream);
  66.                 memStream.Close();
  67.                 fileStream.Close();
  68.                 return decryptedObj;
  69.             }
  70.             catch (Exception e)
  71.             {
  72.                 MessageBox.Show(e.Message);
  73.             }
  74.             return null;
  75.         }
  76.     }
  77. }

0 Comments »

Pas encore de commentaire.

Flux RSS des commentaires de cet article. TrackBack URI

Laisser un commentaire

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 Random namespace | powered by WordPress with Barecity