Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. namespace CSEncryptDecrypt
  2. {
  3.     class Class1
  4.     {
  5.         //  Call this function to remove the key from memory after use for security
  6.         [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
  7.         public static extern bool ZeroMemory(IntPtr Destination, int Length);
  8.  
  9.         // Function to Generate a 64 bits Key.
  10.         static DESCryptoServiceProvider GenerateKey()
  11.         {
  12.             // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
  13.             DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  14.  
  15.             // Use the Automatically generated key for Encryption.
  16.             return desCrypto;
  17.         }
  18.  
  19.         static void EncryptFile(string sInputFilename,
  20.            string sOutputFilename,
  21.           DESCryptoServiceProvider sKey)
  22.         {
  23.             FileStream fsInput = new FileStream(sInputFilename,
  24.                FileMode.Open,
  25.                FileAccess.Read);
  26.  
  27.             FileStream fsEncrypted = new FileStream(sOutputFilename,
  28.                FileMode.Create,
  29.                FileAccess.Write);
  30.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  31.             DES.Key = sKey.Key;
  32.             DES.IV = sKey.IV;
  33.             ICryptoTransform desencrypt = DES.CreateEncryptor();
  34.             CryptoStream cryptostream = new CryptoStream(fsEncrypted,
  35.                desencrypt,
  36.                CryptoStreamMode.Write);
  37.  
  38.             byte[] bytearrayinput = new byte[fsInput.Length];
  39.             fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
  40.             cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  41.             cryptostream.Close();
  42.             fsInput.Close();
  43.             fsEncrypted.Close();
  44.         }
  45.  
  46.         static void DecryptFile(string sInputFilename,
  47.            string sOutputFilename,
  48.            DESCryptoServiceProvider sKey)
  49.         {
  50.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  51.             //A 64 bit key and IV is required for this provider.
  52.             //Set secret key For DES algorithm.
  53.             DES.Key = sKey.Key;
  54.             //Set initialization vector.
  55.             DES.IV = sKey.IV;
  56.  
  57.             //Create a file stream to read the encrypted file back.
  58.             FileStream fsread = new FileStream(sInputFilename,
  59.                FileMode.Open,
  60.                FileAccess.Read);
  61.             //Create a DES decryptor from the DES instance.
  62.             ICryptoTransform desdecrypt = DES.CreateDecryptor();
  63.             //Create crypto stream set to read and do a
  64.             //DES decryption transform on incoming bytes.
  65.             CryptoStream cryptostreamDecr = new CryptoStream(fsread,
  66.                desdecrypt,
  67.                CryptoStreamMode.Read);
  68.             //Print the contents of the decrypted file.
  69.             FileStream fsDecrypted = new FileStream(sOutputFilename, FileMode.Create);
  70.             byte[] bytearrayinput = new byte[1024];
  71.             int length;
  72.             do
  73.             {
  74.                 length = cryptostreamDecr.Read(bytearrayinput, 0, 1024);
  75.                 fsDecrypted.Write(bytearrayinput, 0, length);
  76.             } while (length == 1024);            
  77.             fsDecrypted.Flush();
  78.             fsDecrypted.Close();
  79.         }
  80.  
  81.         static void Main()
  82.         {
  83.             // Must be 64 bits, 8 bytes.
  84.             // Distribute this key to the user who will decrypt this file.
  85.             DESCryptoServiceProvider sSecretKey;
  86.  
  87.             // Get the Key for the file to Encrypt.
  88.             sSecretKey = GenerateKey();
  89.  
  90.             // For additional security Pin the key.
  91.             GCHandle gch = GCHandle.Alloc(sSecretKey.Key, GCHandleType.Pinned);
  92.  
  93.             // Encrypt the file.        
  94.             EncryptFile(@"d:\MyData.txt",
  95.                @"d:\Encrypted.txt",
  96.                sSecretKey);
  97.  
  98.             // Decrypt the file.
  99.             DecryptFile(@"d:\Encrypted.txt",
  100.                @"d:\Decrypted.txt",
  101.                sSecretKey);
  102.  
  103.             gch.Free();
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement