Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- namespace ConsoleApplication
- {
- public class Cryraz : IDisposable {
- private byte[] key = new byte[0];
- /// <summary>
- /// Represents the standart operation for encrypting or decrypting types.
- /// </summary>
- public enum Operation {
- /// <summary>
- /// Byte decrypting algorithin.
- /// </summary>
- Decrypt,
- /// <summary>
- /// Byte encrypting algorithin.
- /// </summary>
- Encrypt
- }
- /// <summary>
- /// Gets the garbagge collector instance generation id.
- /// </summary>
- public int GenerationId => GC.GetGeneration(this);
- /// <summary>
- /// Creates a new <seealso cref="Cryraz"/> instance with specified key array.
- /// </summary>
- /// <param name="key">The new instance byte-based key.</param>
- public Cryraz(byte[] key){
- if(key == null){
- throw new ArgumentNullException(nameof(key));
- }
- this.key = key;
- }
- /// <summary>
- /// Updates the key. You cannot get it directly.
- /// </summary>
- public byte[] Key { set { key = value; } }
- /// <summary>
- /// Encrypts or decrypts an given string using the system's default encoding.
- /// </summary>
- /// <param name="op">Processing operation, such as encrypt or decrypt.</param>
- /// <param name="entryStringData">An raster string for encrypting or decrypting.</param>
- /// <param name="key">The string-based key for the operation.</param>
- /// <returns>The encrypted or decrypted string.</returns>
- public static string ProcessData(Operation op, string entryStringData, string key) {
- return ProcessData(op, entryStringData, key, Encoding.UTF8);
- }
- /// <summary>
- /// Encrypts or decrypts an given string using the specified encoding.
- /// </summary>
- /// <param name="op">Processing operation, such as encrypt or decrypt.</param>
- /// <param name="entryStringData">An raster string for encrypting or decrypting.</param>
- /// <param name="key">The string-based key for the operation.</param>
- /// <param name="encoder">The encoding for converting both key and entry string to byte array.</param>
- /// <returns>The encrypted or decrypted string.</returns>
- public static string ProcessData(Operation op, string entryStringData, string key, Encoding encoder) {
- byte[] data = encoder.GetBytes(entryStringData);
- byte[] k = encoder.GetBytes(key);
- return encoder.GetString(ProcessData(op, data, k));
- }
- /// <summary>
- /// Encrypts or decrypts an given byte array.
- /// </summary>
- /// <param name="op">Processing operation, such as encrypt or decrypt.</param>
- /// <param name="entryByteData">The operating byte-array.</param>
- /// <param name="key">The key for encrypting or decrypting the input.</param>
- /// <returns>The encrypted or decrypted byte array.</returns>
- public static byte[] ProcessData(Operation op, byte[] entryByteData, byte[] key) {
- Cryraz c = new Cryraz(key);
- if (op == Operation.Encrypt) {
- c.EncryptData(ref entryByteData);
- } else c.DecryptData(ref entryByteData);
- return entryByteData;
- }
- internal static byte performKeyHash(byte[] key) {
- int x = 0;
- foreach (byte b in key) {
- x += b;
- x *= 1 + (b % 2);
- }
- return (byte)(x / key.Length);
- }
- internal static byte computePos(byte[] inputArray, int pos) {
- int length = inputArray.Length;
- if (pos <= length - 1) return inputArray[pos];
- int divisor = pos / length;
- pos -= length * divisor;
- return inputArray[pos];
- }
- /// <summary>
- /// Decrypts an given single dimension byte-array with this class key.
- /// </summary>
- /// <param name="entryByteData">The input byte-array to decrypting.</param>
- public void DecryptData(ref byte[] entryByteData, byte? checkSum = null) {
- if (entryByteData == null) throw new ArgumentNullException("entryByteData", "Input data cannot be nothing.");
- if (key == null) throw new ArgumentNullException("key", "Key cannot be nothing.");
- checkSum = checkSum ?? entryByteData[entryByteData.Length - 1];
- // first pw check
- byte hash_x = performKeyHash(key);
- if (hash_x != checkSum)
- throw new System.Security.SecurityException("Invalid key for this data."); // wrong password on auth 0
- performData(ref entryByteData, Operation.Decrypt, 0);
- entryByteData[entryByteData.Length - 1] = 0;
- Array.Resize(ref entryByteData, entryByteData.Length - 1);
- GC.Collect();
- }
- /// <summary>
- /// Encrypts an given single dimension byte-array with this class key.
- /// </summary>
- /// <param name="entryByteData">The input byte-array to encrypting.</param>
- public void EncryptData(ref byte[] entryByteData, bool complete = false) {
- if (entryByteData == null) throw new ArgumentNullException("entryByteData", "Input data cannot be nothing.");
- if (key == null) throw new ArgumentNullException("key", "Key cannot be nothing.");
- performData(ref entryByteData, Operation.Encrypt, 0);
- if(complete){
- Array.Resize(ref entryByteData, entryByteData.Length + 1);
- byte hash_x = performKeyHash(key);
- entryByteData[entryByteData.Length - 1] = hash_x;
- }
- GC.Collect();
- }
- internal void performData(ref byte[] entryByteData, Operation op, int keyOffset) {
- for (int i = 0; i <= entryByteData.Length - 1; i++) {
- int pos = computePos(key, keyOffset + i);
- int a = entryByteData[i];
- if (op == Operation.Encrypt) {
- a += pos;
- } else a -= pos;
- entryByteData[i] = ((byte)a);
- }
- }
- /// <summary>
- /// Releases and finalizes all resources useds by this class and the master key.
- /// </summary>
- public void Dispose() {
- Array.Clear(key, 0, key.Length);
- this.key = null;
- // ... //
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- }
- public class Sample{
- public static void Encripta(string src, string dest){
- Directory.CreateDirectory(Path.GetDirectoryName(dest));
- var buffer = new byte[4096];
- var cipher = new Cryraz(new byte[]{10, 11, 12});
- using(var reader = File.OpenRead(src))
- using(var writer = File.Create(dest))
- {
- int bytes;
- while((bytes = reader.Read(buffer, 0, buffer.Length)) > 0){
- Array.Resize(ref buffer, bytes);
- cipher.EncryptData(ref buffer, reader.Position == reader.Length);
- //chama o seu encryptData com buffer aqui
- writer.Write(buffer, 0, buffer.Length);
- }
- writer.Flush();
- }
- }
- public static void Desencripta(string src, string dest){
- Directory.CreateDirectory(Path.GetDirectoryName(dest));
- var buffer = new byte[4096];
- var cipher = new Cryraz(new byte[]{10, 11, 12});
- using(var reader = File.OpenRead(src))
- using(var writer = File.Create(dest))
- {
- reader.Position = reader.Length - 1;
- var checkSum = reader.ReadByte();
- reader.Position = 0;
- int bytes;
- while((bytes = reader.Read(buffer, 0, buffer.Length)) > 0){
- Array.Resize(ref buffer, bytes);
- cipher.DecryptData(ref buffer, (byte)checkSum);
- //chama o seu encryptData com buffer aqui
- writer.Write(buffer, 0, buffer.Length);
- }
- writer.Flush();
- }
- }
- public static void Main(string[] args)
- {
- Encripta(@"C:\code\bin\Debug\netcoreapp1.1\dummy.dll", @"C:\code\bin\Debug\netcoreapp1.1\a.dll");
- Desencripta(@"C:\code\bin\Debug\netcoreapp1.1\a.dll", @"C:\code\bin\Debug\netcoreapp1.1\b.dll");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment