Advertisement
ejectedmatrix

Untitled

Jul 9th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.VisualBasic.Devices;
  7. using System.Threading.Tasks;
  8.  
  9. namespace BinaryBufferSerializer
  10. {
  11. public class Program
  12. {
  13. static string[] primIdens = { "1", "4", "7", "a", "d", "g", "j", "m", "p", "s", "v", "y", "A", "D", "G", "J", "M", "P", "S", "V", "Y" };
  14. static string[] secoIdens = { "2", "5", "8", "b", "e", "h", "k", "n", "q", "t", "w", "z", "B", "E", "H", "K", "N", "Q", "T", "W", "Z" };
  15. static string[] paddingIdens = { "0", "3", "6", "9", "c", "f", "i", "l", "o", "r", "u", "x", "C", "F", "I", "L", "O", "R", "U", "X" };
  16.  
  17. public static void Main(string[] args)
  18. {
  19. ComputerInfo pcInfo = new ComputerInfo();
  20. var importantPcData = new
  21. {
  22. os = new
  23. {
  24. name = pcInfo.OSFullName,
  25. version = pcInfo.OSVersion,
  26. platform = pcInfo.OSPlatform,
  27. },
  28. memory = new
  29. {
  30. total_physical_memory = pcInfo.TotalPhysicalMemory,
  31. total_virtual_memory = pcInfo.TotalVirtualMemory,
  32. available_physical_memory = pcInfo.AvailablePhysicalMemory,
  33. available_virtual_memory = pcInfo.AvailableVirtualMemory
  34. },
  35. time = new
  36. {
  37. time_now = DateTime.Now.ToString(),
  38. time_now_epoch = DateTime.Now.ToFileTime().ToString(),
  39. time_utc = DateTime.Now.ToUniversalTime().ToString(),
  40. time_utc_epoch = DateTime.Now.ToFileTimeUtc().ToString(),
  41. },
  42. framework = new
  43. {
  44. framework_version = "net461",
  45. framework_interop_functional = true,
  46. framework_json_dep = "netstandard1.1"
  47. }
  48. };
  49.  
  50. string data = JsonConvert.SerializeObject(importantPcData);
  51. Console.WriteLine("Serializing " + data);
  52. string binaryData = SerializeData(data);
  53. Console.WriteLine(binaryData);
  54. Console.ReadLine();
  55. }
  56.  
  57. static string SerializeData(string data)
  58. {
  59. return SerializeBinaryData(Encoding.UTF8.GetBytes(data));
  60. }
  61.  
  62. static string SerializeBinaryData(byte[] buffer)
  63. {
  64. Random random = new Random();
  65. string _binaryRep = ReadBinaryData(buffer);
  66. char[] binaryChars = _binaryRep.ToCharArray();
  67. string serializedData = null;
  68.  
  69. for (int charLoc = 0; charLoc < binaryChars.Length; charLoc++)
  70. {
  71. BitType bitType = binaryChars[charLoc] == '0'
  72. ? BitType.Zero : binaryChars[charLoc] == '1'
  73. ? BitType.One : BitType.Padding;
  74.  
  75. switch (bitType)
  76. {
  77. case BitType.Zero:
  78. serializedData += primIdens[random.Next(primIdens.Length - 1)];
  79. break;
  80. case BitType.One:
  81. serializedData += secoIdens[random.Next(secoIdens.Length - 1)];
  82. break;
  83. case BitType.Padding:
  84. serializedData += paddingIdens[random.Next(paddingIdens.Length - 1)];
  85. break;
  86. default:
  87. throw new InvalidOperationException();
  88. }
  89. }
  90.  
  91. return serializedData;
  92. }
  93.  
  94. static string ReadBinaryData(byte[] buffer)
  95. {
  96. string binaryData = null;
  97.  
  98. for (int byteLoc = 0; byteLoc < buffer.Length; byteLoc++)
  99. {
  100. binaryData += Convert.ToString(buffer[byteLoc], 2).PadLeft(8, '0');
  101. binaryData += " "; // Add bit/byte padding.
  102. }
  103.  
  104. return binaryData;
  105. }
  106. }
  107.  
  108. public enum BitType
  109. {
  110. Zero = 0,
  111. One = 1,
  112. Padding = 3,
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement