Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace PangyaAPI
- {
- public static class PacketCompression
- {
- public static byte[] GeneratePacketId(byte key)
- {
- string keyCriptografia = (Convert.ToInt32(key) + 1).ToString();
- ProcessStartInfo pInfo = new ProcessStartInfo();
- pInfo.CreateNoWindow = false;
- pInfo.UseShellExecute = false;
- pInfo.FileName = @"C:\PangyaCompress\test\Project2.exe";
- pInfo.Arguments = $"0100E271D24D000000110000 {keyCriptografia}";
- pInfo.WindowStyle = ProcessWindowStyle.Hidden;
- pInfo.RedirectStandardOutput = true;
- pInfo.RedirectStandardError = true;
- string output = String.Empty;
- using (Process p = Process.Start(pInfo))
- {
- p.WaitForExit();
- var exitCode = p.ExitCode;
- output = p.StandardOutput.ReadToEnd();
- var error = p.StandardError.ReadToEnd();
- if (output.Length > 0)
- {
- // you have some output
- }
- if (error.Length > 0)
- {
- // you have some error details
- }
- }
- return StringToByteArray(output.Remove(8, output.Length - 8));
- }
- public enum TipoOperacao
- {
- Compress = 1,
- Decompress = 2
- }
- //Decompress
- public static byte[] Compress(byte[] packet, TipoOperacao operacao)
- {
- var packetStr = ConvertToString(packet);
- ProcessStartInfo pInfo = new ProcessStartInfo();
- pInfo.CreateNoWindow = false;
- pInfo.UseShellExecute = false;
- pInfo.FileName = @"C:\PangyaCompress\compression\Project2.exe";
- pInfo.Arguments = $"{(int)operacao} {packetStr}";
- pInfo.WindowStyle = ProcessWindowStyle.Hidden;
- pInfo.RedirectStandardOutput = true;
- pInfo.RedirectStandardError = true;
- string output = String.Empty;
- using (Process p = Process.Start(pInfo))
- {
- p.WaitForExit();
- var exitCode = p.ExitCode;
- output = p.StandardOutput.ReadToEnd();
- var error = p.StandardError.ReadToEnd();
- if (output.Length > 0)
- {
- // you have some output
- }
- if (error.Length > 0)
- {
- // you have some error details
- }
- }
- if (operacao == TipoOperacao.Compress)
- {
- //Remove os 4 primeiros bytes pois isto é tratado depois
- output = output.Remove(0, 8);
- //Remove 11 00 00
- output = output.Remove(output.Length - 6, 6);
- }
- //if (returnInHex)
- // return ConvertToCsharpByteHex(output);
- return StringToByteArray(output);
- }
- //public static byte[] CompressToByte(string packet, byte key)
- //{
- // return StringToByteArray(Compress(packet, key));
- //}
- public static byte[] StringToByteArray(String hex)
- {
- int NumberChars = hex.Length;
- byte[] bytes = new byte[NumberChars / 2];
- for (int i = 0; i < NumberChars; i += 2)
- bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
- return bytes;
- }
- private static string ConvertToString(byte[] message)
- {
- return BitConverter.ToString(message).Replace("-", "");
- }
- private static string ConvertToCsharpByteHex(string input)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < input.Length; i += 2)
- {
- sb.Append("0x"+input.Substring(i, 2)+", ");
- }
- return sb.ToString();
- }
- public static string GetCsharpStringFromFile(string path)
- {
- string filePath = path;
- return ConvertToCsharpByteHex(ConvertToString(System.IO.File.ReadAllBytes(filePath)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement