Advertisement
Guest User

Untitled

a guest
Aug 19th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security;
  7. using System.Security.Cryptography;
  8. using System.IO;
  9. using System.Security.Permissions;
  10. using System.Runtime.InteropServices;
  11.  
  12. namespace SafeConverter
  13. {
  14. class Cryption
  15. {
  16. // Call this function to remove the key from memory after use for security
  17. [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
  18. public static extern bool ZeroMemory(IntPtr Destination, int Length);
  19.  
  20. // Function to Generate a 64 bits Key.
  21. static string GenerateKey()
  22. {
  23. // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
  24. DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  25.  
  26. // Use the Automatically generated key for Encryption.
  27. return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
  28. }
  29.  
  30. static void EncryptFile(string sInputFilename,
  31. string sOutputFilename,
  32. string sKey)
  33. {
  34. FileStream fsInput = new FileStream(sInputFilename,
  35. FileMode.Open,
  36. FileAccess.Read);
  37.  
  38. FileStream fsEncrypted = new FileStream(sOutputFilename,
  39. FileMode.Create,
  40. FileAccess.Write);
  41. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  42. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  43. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  44. ICryptoTransform desencrypt = DES.CreateEncryptor();
  45. CryptoStream cryptostream = new CryptoStream(fsEncrypted,
  46. desencrypt,
  47. CryptoStreamMode.Write);
  48.  
  49. byte[] bytearrayinput = new byte[fsInput.Length];
  50. fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
  51. cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  52. cryptostream.Close();
  53. fsInput.Close();
  54. fsEncrypted.Close();
  55. }
  56.  
  57. static void DecryptFile(string sInputFilename,
  58. string sOutputFilename,
  59. string sKey)
  60. {
  61. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  62. //A 64 bit key and IV is required for this provider.
  63. //Set secret key For DES algorithm.
  64. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  65. //Set initialization vector.
  66. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  67.  
  68. //Create a file stream to read the encrypted file back.
  69. FileStream fsread = new FileStream(sInputFilename,
  70. FileMode.Open,
  71. FileAccess.Read);
  72. //Create a DES decryptor from the DES instance.
  73. ICryptoTransform desdecrypt = DES.CreateDecryptor();
  74. //Create crypto stream set to read and do a
  75. //DES decryption transform on incoming bytes.
  76. CryptoStream cryptostreamDecr = new CryptoStream(fsread,
  77. desdecrypt,
  78. CryptoStreamMode.Read);
  79. //Print the contents of the decrypted file.
  80. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
  81. fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
  82. fsDecrypted.Flush();
  83. fsDecrypted.Close();
  84. }
  85.  
  86. public static void encrypt(String filename)
  87. {
  88. // Must be 64 bits, 8 bytes.
  89. // Distribute this key to the user who will decrypt this file.
  90. string sSecretKey;
  91. // Get the Key for the file to Encrypt.
  92. sSecretKey = GenerateKey();
  93. // For additional security Pin the key.
  94. GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);
  95. String[] array = filename.Split('.');
  96. String newfile = array[0];
  97. EncryptFile(@filename,@newfile + "EN.txt",sSecretKey);
  98. // Remove the Key from memory.
  99. ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
  100. gch.Free();
  101. }
  102.  
  103. public static void decrypt(String filename)
  104. {
  105. // Must be 64 bits, 8 bytes.
  106. // Distribute this key to the user who will decrypt this file.
  107. string sSecretKey;
  108. // Get the Key for the file to Encrypt.
  109. sSecretKey = GenerateKey();
  110. // For additional security Pin the key.
  111. GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);
  112. String[] array = filename.Split('.');
  113. String newfile = array[0];
  114. DecryptFile(@filename, @newfile + "DE.txt", sSecretKey);
  115. // Remove the Key from memory.
  116. ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
  117. gch.Free();
  118. }
  119.  
  120. private static String getRandomFileAss()
  121. {
  122. int maxSize = 5;
  123. char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
  124. byte[] data = new byte[1];
  125. RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
  126. crypto.GetNonZeroBytes(data);
  127. data = new byte[maxSize];
  128. crypto.GetNonZeroBytes(data);
  129. StringBuilder result = new StringBuilder(maxSize);
  130. foreach (byte b in data)
  131. {
  132. result.Append(chars[b % (chars.Length - 1)]);
  133. }
  134. return result.ToString();
  135. }
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement