Advertisement
Stranck

fools2018 riley fast

Apr 2nd, 2018
177
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.08 KB
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Collections.Generic;
  8.  
  9. class zzazz_crypto {
  10. private static readonly byte[] b680 = new byte[] {
  11. 0x6d, 0xe5, 0x9a, 0x4c, 0xc7, 0x35, 0x1a, 0x3b, 0x78, 0xfb, 0x02, 0x84, 0x7b, 0x4b, 0x4a,
  12. 0xc0, 0x6c, 0x9b, 0x36, 0x1f, 0x34, 0x4d, 0xce, 0x24, 0xb9, 0xe0, 0x29, 0x54, 0x99, 0x67,
  13. 0x19, 0x21, 0x73, 0xcb, 0x57, 0x46, 0x2f, 0xdf, 0x5e, 0x43, 0x72, 0x7a, 0x28, 0xb0, 0x0f,
  14. 0xf6, 0x49, 0xe2, 0x12, 0xf0, 0x09, 0x44, 0xcd, 0x69, 0x95, 0x6b, 0xd3, 0xd1, 0xe6, 0x87,
  15. 0x92, 0xf7, 0xdd, 0x89, 0xc2, 0x63, 0xea, 0x1d, 0xbb, 0xa7, 0x0a, 0x48, 0x93, 0x90, 0xc1,
  16. 0x08, 0x14, 0x1b, 0x79, 0x91, 0x65, 0xf8, 0x0d, 0xd8, 0xd0, 0x47, 0xe1, 0xf9, 0x15, 0x9e,
  17. 0x05, 0x41, 0xc8, 0xb7, 0x0e, 0x7e, 0x22, 0xe9, 0xda, 0xb1, 0x62, 0x13, 0x26, 0x42, 0xab,
  18. 0xd4, 0x5c, 0x4f, 0x74, 0xc4, 0x04, 0x66, 0xd6, 0x5f, 0x38, 0x4e, 0x10, 0xa5, 0x75, 0x52,
  19. 0xaa, 0xfe, 0xf2, 0xa3, 0x70, 0x25, 0x82, 0x3a, 0x0c, 0x9d, 0x97, 0x56, 0x7d, 0xd5, 0xa4,
  20. 0xe8, 0xaf, 0x11, 0xb8, 0x33, 0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xb4, 0x3f, 0xeb, 0x3b, 0x39, 0xa0, 0x64, 0xee, 0xc4, 0xe3, 0xa0, 0x89, 0x2f, 0x83, 0xd1,
  33. 0x98, 0x62, 0xb6, 0xff, 0xeb, 0x07, 0x0f, 0xc2, 0xe8, 0xdb, 0x53, 0xda, 0x0b, 0xfa, 0x16,
  34. 0x53, 0x9b, 0x51, 0x6f, 0xb3, 0xd8, 0x5e, 0xbd, 0x05, 0xd1, 0xd6, 0x0d, 0xf6, 0x66, 0xbc,
  35. 0x89, 0x84, 0x44, 0xad, 0x68, 0x09, 0x4c, 0x00, 0x9f, 0x60, 0x8d, 0x79, 0xab, 0x73, 0xf8,
  36. 0x12, 0x98, 0xe1, 0x56, 0x62, 0x36, 0x9b, 0xdf, 0xe7, 0x60, 0x61, 0x62, 0x63, 0x4f, 0x64,
  37. 0x65, 0x66, 0x67, 0x4f, 0x68, 0x69, 0x6a, 0x6b, 0x4f, 0x6c, 0x6d, 0x6e, 0x6f, 0x50
  38. };
  39.  
  40. private static readonly byte[] c660 = new byte[] {
  41. 0x7d, 0xbc, 0x5d, 0x92, 0xc7, 0x66, 0xfb, 0x16, 0xb3, 0xc7, 0x25, 0x39, 0x07, 0x65,
  42. 0xe4, 0x00, 0x2e, 0xce, 0xbc, 0xa8, 0xdf, 0x6a, 0x85, 0xe7, 0x8e, 0x5e, 0x49, 0x51, 0xd9,
  43. 0x8e, 0xfc, 0x5c, 0xd5, 0xca, 0x3e, 0x0e, 0x90, 0xf8, 0x6a, 0x3e, 0x38, 0xc1, 0xbe, 0x33,
  44. 0xdc, 0x4b, 0xce, 0x04, 0x50, 0x4d, 0x50, 0xa3, 0xe7, 0x80, 0x88, 0xdb, 0xef, 0x6d, 0xc8,
  45. 0xb4, 0x3f, 0xeb, 0x3b, 0x39, 0xa0, 0x64, 0xee, 0xc4, 0xe3, 0xa0, 0x89, 0x2f, 0x83, 0xd1,
  46. 0x98, 0x62, 0xb6, 0xff, 0xeb, 0x07, 0x0f, 0xc2, 0xe8, 0xdb, 0x53, 0xda, 0x0b, 0xfa, 0x16,
  47. 0x53, 0x9b, 0x51, 0x6f, 0xb3, 0xd8, 0x5e, 0xbd, 0x05, 0xd1, 0xd6, 0x0d, 0xf6, 0x66, 0xbc,
  48. 0x89, 0x84, 0x44, 0xad, 0x68, 0x09, 0x4c, 0x00, 0x9f, 0x60, 0x8d, 0x79, 0xab, 0x73, 0xf8,
  49. 0x12, 0x98, 0xe1, 0x56, 0x62, 0x36, 0x9b, 0xdf, 0xe7
  50. };
  51.  
  52. byte[] key;
  53.  
  54. public zzazz_crypto(string key) {
  55.     if (key.Length != 10) throw new Exception("key length must be 10");
  56.     if (!key.All(char.IsDigit)) throw new Exception("key must be all-digit");
  57.     this.key = Encoding.ASCII.GetBytes(key);
  58.     for (int i = 0; i < 10; i++) this.key[i] += 0xf6 - 0x30;
  59. }
  60.  
  61. public zzazz_crypto(byte[] key) {
  62.     if (key.Length < 10) throw new Exception("key length must be 10");
  63.     this.key = new byte[10];
  64.     for (int i = 0; i < 10; i++) this.key[i] = key[i];
  65. }
  66.  
  67. public string GetKeyString() {
  68.     byte[] keyAscii = new byte[10];
  69.     for (int i = 0; i < 10; i++) keyAscii[i] = (byte) ( key[i] - 0xf6 + 0x30 );
  70.     return Encoding.ASCII.GetString(keyAscii);
  71. }
  72.  
  73. public void InitialDerivation() {
  74.     for (int i = 0; i < 0x19; i++) {
  75.         InitialDerivationIteration(key[9]);
  76.     }
  77. }
  78.  
  79. private void InitialDerivationIteration(byte b) {
  80.     for (int i = 0; i < 10; i++) {
  81.         byte a = key[i];
  82.         byte bit = (byte) (a & 1);
  83.         a >>= 1;
  84.         a |= (byte)(bit << 7);
  85.         a ^= 0x5c;
  86.         a += 0x1e;
  87.         a = b680[a];
  88.         a ^= b;
  89.         b=a;
  90.         key[i] = a;
  91.     }
  92. }
  93.  
  94. private void UndoDerivationIteration() {
  95.     for (int i = 9; i > -1; i--) {
  96.         byte a = key[i];
  97.         int index = i - 1;
  98.         if (i == 0) index = 9;
  99.         a ^= key[index];
  100.         a = (byte) Array.IndexOf(b680, a);
  101.         a -= 0x1e;
  102.         a ^= 0x5c;
  103.         byte bit = (byte) ((a & 0x80) >> 7);
  104.         a <<= 1;
  105.         a |= bit;
  106.         key[i] = a;
  107.     }
  108. }
  109.  
  110. public void UndoInitialDerivation() {
  111.     for (int i = 0; i < 0x19; i++) {
  112.         UndoDerivationIteration();
  113.     }
  114. }
  115.  
  116. public byte[] DoCrypto() {
  117.     byte[] ret = new byte[c660.Length];
  118.     int idx = 0;
  119.     for (int i = 0; i < 0x80; i++) {
  120.         ret[i] = c660[i];
  121.         ret[i] ^= key[idx];
  122.         key[idx] = b680[idx];
  123.         idx++;
  124.         idx %= 10;
  125.     }
  126.     return ret;
  127. }
  128.  
  129. public static string HexDump(byte[] bytes, int bytesPerLine = 16)
  130.         {
  131.             if (bytes == null) return "<null>";
  132.             int bytesLength = bytes.Length;
  133.  
  134.             char[] HexChars = "0123456789ABCDEF".ToCharArray();
  135.  
  136.             int firstHexColumn =
  137.                   8                   // 8 characters for the address
  138.                 + 3;                  // 3 spaces
  139.  
  140.             int firstCharColumn = firstHexColumn
  141.                 + bytesPerLine * 3       // - 2 digit for the hexadecimal value and 1 space
  142.                 + (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
  143.                 + 2;                  // 2 spaces
  144.  
  145.             int lineLength = firstCharColumn
  146.                 + bytesPerLine           // - characters to show the ascii value
  147.                 + Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
  148.  
  149.             char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
  150.             int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
  151.             StringBuilder result = new StringBuilder(expectedLines * lineLength);
  152.  
  153.             for (int i = 0; i < bytesLength; i += bytesPerLine)
  154.             {
  155.                 line[0] = HexChars[(i >> 28) & 0xF];
  156.                 line[1] = HexChars[(i >> 24) & 0xF];
  157.                 line[2] = HexChars[(i >> 20) & 0xF];
  158.                 line[3] = HexChars[(i >> 16) & 0xF];
  159.                 line[4] = HexChars[(i >> 12) & 0xF];
  160.                 line[5] = HexChars[(i >> 8) & 0xF];
  161.                 line[6] = HexChars[(i >> 4) & 0xF];
  162.                 line[7] = HexChars[(i >> 0) & 0xF];
  163.  
  164.                 int hexColumn = firstHexColumn;
  165.                 int charColumn = firstCharColumn;
  166.  
  167.                 for (int j = 0; j < bytesPerLine; j++)
  168.                 {
  169.                     if (j > 0 && (j & 7) == 0) hexColumn++;
  170.                     if (i + j >= bytesLength)
  171.                     {
  172.                         line[hexColumn] = ' ';
  173.                         line[hexColumn + 1] = ' ';
  174.                         line[charColumn] = ' ';
  175.                     }
  176.                     else
  177.                     {
  178.                         byte b = bytes[i + j];
  179.                         line[hexColumn] = HexChars[(b >> 4) & 0xF];
  180.                         line[hexColumn + 1] = HexChars[b & 0xF];
  181.                         line[charColumn] = (b < 32 ? '·' : (char)b);
  182.                     }
  183.                     hexColumn += 3;
  184.                     charColumn++;
  185.                 }
  186.                 result.Append(line);
  187.             }
  188.             return result.ToString();
  189.         }
  190.  
  191. [DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
  192. static extern int memcmp(byte[] b1, byte[] b2, long count);
  193.  
  194. public static IEnumerable<ulong> Range(ulong fromInclusive, ulong toExclusive)
  195. {
  196.   for (var i = fromInclusive; i < toExclusive; i++) yield return i;
  197. }
  198.  
  199. public static void ParallelFor(ulong fromInclusive, ulong toExclusive, Action<ulong> body)
  200. {
  201.   Parallel.ForEach(
  202.      Range(fromInclusive, toExclusive),
  203.      new ParallelOptions { MaxDegreeOfParallelism = 4 },
  204.      body);
  205. }
  206.  
  207. private static long done_ids = 0;
  208. private const long total_ids = 10000000000;
  209. private static DateTime LastUpdate = DateTime.Now;
  210.  
  211. public static void Main(string[] args) {
  212.     /*Parallel.For(0,total_ids,delegate(long i) {
  213.         string str = i.ToString();
  214.         if (str.Length < 10) str = new string('0',10-str.Length) + str;
  215.         //Console.Write("{0}\r",str);
  216.         var crypto = new zzazz_crypto(str);
  217.         crypto.InitialDerivation();
  218.         if (memcmp(crypto.key,c660,8) == 0) Console.WriteLine("{0}                                                ",str);
  219.         Interlocked.Increment(ref done_ids);
  220.         if (DateTime.Now.Subtract(LastUpdate).TotalSeconds > 1) {
  221.             LastUpdate = DateTime.Now;
  222.                     float progress = (float)done_ids / (float)total_ids;
  223.                     Console.Write("Progress: " + ((int)(progress * 100)) + "% (" + done_ids + "/" + total_ids + ")\r");
  224.         }
  225.         });*/
  226.     /*for (ulong i = 0; i < 10000000000; i++) {
  227.         string str = i.ToString();
  228.         if (str.Length < 10) str = new string('0',10-str.Length) + str;
  229.         Console.Write("{0}\r",str);
  230.         var crypto = new zzazz_crypto(str);
  231.         crypto.InitialDerivation();
  232.         if (memcmp(crypto.key,c660,8) == 0) Console.Write("\n");
  233.     }*/
  234.     /*crypto = new zzazz_crypto(args[0]);
  235.     Console.WriteLine("key (before derivation):\n{0}",HexDump(crypto.key));
  236.     crypto.InitialDerivation();
  237.     Console.WriteLine("key (after derivation):\n{0}",HexDump(crypto.key));
  238.     Console.WriteLine("plaintext:\n{0}",HexDump(crypto.DoCrypto()));*/
  239.     var crypto = new zzazz_crypto(c660);
  240.     Console.WriteLine("key (after derivation):\n{0}",HexDump(crypto.key));
  241.     crypto.UndoInitialDerivation();
  242.     Console.WriteLine("key (before derivation):\n{0}",HexDump(crypto.key));
  243.     Console.WriteLine("key (ascii): {0}",crypto.GetKeyString());
  244. }
  245.  
  246. }
Advertisement
RAW Paste Data Copied
Advertisement