Guest User

Untitled

a guest
Jul 7th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace ConsoleApplication
  6. {
  7.  
  8. public class Cryraz : IDisposable {
  9.  
  10. private byte[] key = new byte[0];
  11.  
  12. /// <summary>
  13. /// Represents the standart operation for encrypting or decrypting types.
  14. /// </summary>
  15. public enum Operation {
  16. /// <summary>
  17. /// Byte decrypting algorithin.
  18. /// </summary>
  19. Decrypt,
  20. /// <summary>
  21. /// Byte encrypting algorithin.
  22. /// </summary>
  23. Encrypt
  24. }
  25.  
  26. /// <summary>
  27. /// Gets the garbagge collector instance generation id.
  28. /// </summary>
  29. public int GenerationId => GC.GetGeneration(this);
  30.  
  31. /// <summary>
  32. /// Creates a new <seealso cref="Cryraz"/> instance with specified key array.
  33. /// </summary>
  34. /// <param name="key">The new instance byte-based key.</param>
  35. public Cryraz(byte[] key){
  36. if(key == null){
  37. throw new ArgumentNullException(nameof(key));
  38. }
  39. this.key = key;
  40. }
  41.  
  42. /// <summary>
  43. /// Updates the key. You cannot get it directly.
  44. /// </summary>
  45. public byte[] Key { set { key = value; } }
  46.  
  47. /// <summary>
  48. /// Encrypts or decrypts an given string using the system's default encoding.
  49. /// </summary>
  50. /// <param name="op">Processing operation, such as encrypt or decrypt.</param>
  51. /// <param name="entryStringData">An raster string for encrypting or decrypting.</param>
  52. /// <param name="key">The string-based key for the operation.</param>
  53. /// <returns>The encrypted or decrypted string.</returns>
  54. public static string ProcessData(Operation op, string entryStringData, string key) {
  55. return ProcessData(op, entryStringData, key, Encoding.UTF8);
  56. }
  57.  
  58. /// <summary>
  59. /// Encrypts or decrypts an given string using the specified encoding.
  60. /// </summary>
  61. /// <param name="op">Processing operation, such as encrypt or decrypt.</param>
  62. /// <param name="entryStringData">An raster string for encrypting or decrypting.</param>
  63. /// <param name="key">The string-based key for the operation.</param>
  64. /// <param name="encoder">The encoding for converting both key and entry string to byte array.</param>
  65. /// <returns>The encrypted or decrypted string.</returns>
  66. public static string ProcessData(Operation op, string entryStringData, string key, Encoding encoder) {
  67. byte[] data = encoder.GetBytes(entryStringData);
  68. byte[] k = encoder.GetBytes(key);
  69. return encoder.GetString(ProcessData(op, data, k));
  70. }
  71.  
  72. /// <summary>
  73. /// Encrypts or decrypts an given byte array.
  74. /// </summary>
  75. /// <param name="op">Processing operation, such as encrypt or decrypt.</param>
  76. /// <param name="entryByteData">The operating byte-array.</param>
  77. /// <param name="key">The key for encrypting or decrypting the input.</param>
  78. /// <returns>The encrypted or decrypted byte array.</returns>
  79. public static byte[] ProcessData(Operation op, byte[] entryByteData, byte[] key) {
  80. Cryraz c = new Cryraz(key);
  81. if (op == Operation.Encrypt) {
  82. c.EncryptData(ref entryByteData);
  83. } else c.DecryptData(ref entryByteData);
  84. return entryByteData;
  85. }
  86.  
  87. internal static byte performKeyHash(byte[] key) {
  88. int x = 0;
  89. foreach (byte b in key) {
  90. x += b;
  91. x *= 1 + (b % 2);
  92. }
  93. return (byte)(x / key.Length);
  94. }
  95.  
  96. internal static byte computePos(byte[] inputArray, int pos) {
  97. int length = inputArray.Length;
  98. if (pos <= length - 1) return inputArray[pos];
  99.  
  100. int divisor = pos / length;
  101. pos -= length * divisor;
  102. return inputArray[pos];
  103. }
  104.  
  105. /// <summary>
  106. /// Decrypts an given single dimension byte-array with this class key.
  107. /// </summary>
  108. /// <param name="entryByteData">The input byte-array to decrypting.</param>
  109. public void DecryptData(ref byte[] entryByteData, byte? checkSum = null) {
  110.  
  111. if (entryByteData == null) throw new ArgumentNullException("entryByteData", "Input data cannot be nothing.");
  112. if (key == null) throw new ArgumentNullException("key", "Key cannot be nothing.");
  113.  
  114. checkSum = checkSum ?? entryByteData[entryByteData.Length - 1];
  115. // first pw check
  116. byte hash_x = performKeyHash(key);
  117.  
  118. if (hash_x != checkSum)
  119. throw new System.Security.SecurityException("Invalid key for this data."); // wrong password on auth 0
  120.  
  121. performData(ref entryByteData, Operation.Decrypt, 0);
  122.  
  123. entryByteData[entryByteData.Length - 1] = 0;
  124.  
  125. Array.Resize(ref entryByteData, entryByteData.Length - 1);
  126.  
  127. GC.Collect();
  128. }
  129.  
  130. /// <summary>
  131. /// Encrypts an given single dimension byte-array with this class key.
  132. /// </summary>
  133. /// <param name="entryByteData">The input byte-array to encrypting.</param>
  134. public void EncryptData(ref byte[] entryByteData, bool complete = false) {
  135.  
  136. if (entryByteData == null) throw new ArgumentNullException("entryByteData", "Input data cannot be nothing.");
  137. if (key == null) throw new ArgumentNullException("key", "Key cannot be nothing.");
  138.  
  139. performData(ref entryByteData, Operation.Encrypt, 0);
  140. if(complete){
  141. Array.Resize(ref entryByteData, entryByteData.Length + 1);
  142.  
  143. byte hash_x = performKeyHash(key);
  144. entryByteData[entryByteData.Length - 1] = hash_x;
  145. }
  146.  
  147. GC.Collect();
  148. }
  149.  
  150. internal void performData(ref byte[] entryByteData, Operation op, int keyOffset) {
  151. for (int i = 0; i <= entryByteData.Length - 1; i++) {
  152. int pos = computePos(key, keyOffset + i);
  153. int a = entryByteData[i];
  154. if (op == Operation.Encrypt) {
  155. a += pos;
  156. } else a -= pos;
  157. entryByteData[i] = ((byte)a);
  158. }
  159. }
  160.  
  161. /// <summary>
  162. /// Releases and finalizes all resources useds by this class and the master key.
  163. /// </summary>
  164. public void Dispose() {
  165. Array.Clear(key, 0, key.Length);
  166. this.key = null;
  167. // ... //
  168. GC.Collect();
  169. GC.WaitForPendingFinalizers();
  170. }
  171. }
  172.  
  173. public class Sample{
  174.  
  175. public static void Encripta(string src, string dest){
  176. Directory.CreateDirectory(Path.GetDirectoryName(dest));
  177. var buffer = new byte[4096];
  178. var cipher = new Cryraz(new byte[]{10, 11, 12});
  179.  
  180. using(var reader = File.OpenRead(src))
  181. using(var writer = File.Create(dest))
  182. {
  183. int bytes;
  184. while((bytes = reader.Read(buffer, 0, buffer.Length)) > 0){
  185. Array.Resize(ref buffer, bytes);
  186. cipher.EncryptData(ref buffer, reader.Position == reader.Length);
  187. //chama o seu encryptData com buffer aqui
  188. writer.Write(buffer, 0, buffer.Length);
  189. }
  190. writer.Flush();
  191. }
  192.  
  193. }
  194.  
  195. public static void Desencripta(string src, string dest){
  196. Directory.CreateDirectory(Path.GetDirectoryName(dest));
  197. var buffer = new byte[4096];
  198. var cipher = new Cryraz(new byte[]{10, 11, 12});
  199.  
  200. using(var reader = File.OpenRead(src))
  201. using(var writer = File.Create(dest))
  202. {
  203. reader.Position = reader.Length - 1;
  204. var checkSum = reader.ReadByte();
  205. reader.Position = 0;
  206. int bytes;
  207. while((bytes = reader.Read(buffer, 0, buffer.Length)) > 0){
  208. Array.Resize(ref buffer, bytes);
  209. cipher.DecryptData(ref buffer, (byte)checkSum);
  210. //chama o seu encryptData com buffer aqui
  211. writer.Write(buffer, 0, buffer.Length);
  212. }
  213. writer.Flush();
  214. }
  215.  
  216. }
  217.  
  218. public static void Main(string[] args)
  219. {
  220. Encripta(@"C:\code\bin\Debug\netcoreapp1.1\dummy.dll", @"C:\code\bin\Debug\netcoreapp1.1\a.dll");
  221. Desencripta(@"C:\code\bin\Debug\netcoreapp1.1\a.dll", @"C:\code\bin\Debug\netcoreapp1.1\b.dll");
  222. }
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment