Advertisement
FishJk

KeyAuth

Oct 7th, 2021
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 22.42 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Security.Cryptography.X509Certificates;
  4. using System.Collections.Specialized;
  5. using System.Text;
  6. using System.Net;
  7. using System.IO;
  8. using System.Net.Security;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Json;
  11. using System.Diagnostics;
  12. using System.Security.Principal;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. using System.Collections.Generic;
  16.  
  17. namespace Laucher
  18. {
  19.     public class api
  20.     {
  21.         public string name, ownerid, secret, version;
  22.         public api(string name, string ownerid, string secret, string version)
  23.         {
  24.  
  25.             if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(ownerid) || string.IsNullOrWhiteSpace(secret) || string.IsNullOrWhiteSpace(version))
  26.             {
  27.                 MessageBox.Show("Application not setup correctly. Please watch video link found in Login.cs");
  28.                 Environment.Exit(0);
  29.             }
  30.  
  31.             this.name = name;
  32.  
  33.             this.ownerid = ownerid;
  34.  
  35.             this.secret = secret;
  36.  
  37.             this.version = version;
  38.         }
  39.  
  40.         #region structures
  41.         [DataContract]
  42.         private class response_structure
  43.         {
  44.             [DataMember]
  45.             public bool success { get; set; }
  46.  
  47.             [DataMember]
  48.             public string sessionid { get; set; }
  49.  
  50.             [DataMember]
  51.             public string contents { get; set; }
  52.  
  53.             [DataMember]
  54.             public string response { get; set; }
  55.  
  56.             [DataMember]
  57.             public string message { get; set; }
  58.  
  59.             [DataMember]
  60.             public string download { get; set; }
  61.  
  62.             [DataMember(IsRequired = false, EmitDefaultValue = false)]
  63.             public user_data_structure info { get; set; }
  64.         }
  65.  
  66.         [DataContract]
  67.         private class user_data_structure
  68.         {
  69.             [DataMember]
  70.             public string username { get; set; }
  71.  
  72.             [DataMember]
  73.             public List<Data> subscriptions { get; set; }
  74.  
  75.             [DataMember]
  76.             public string ip { get; set; }
  77.         }
  78.         #endregion
  79.         private string sessionid, enckey;
  80.         bool initzalized;
  81.         public void init()
  82.         {
  83.             enckey = encryption.sha256(encryption.iv_key());
  84.             var init_iv = encryption.sha256(encryption.iv_key());
  85.             var values_to_upload = new NameValueCollection
  86.             {
  87.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("init")),
  88.                 ["ver"] = encryption.encrypt(version, secret, init_iv),
  89.                 ["enckey"] = encryption.encrypt(enckey, secret, init_iv),
  90.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  91.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  92.                 ["init_iv"] = init_iv
  93.             };
  94.  
  95.             var response = req(values_to_upload);
  96.  
  97.  
  98.             if (response == "KeyAuth_Invalid")
  99.             {
  100.                 MessageBox.Show("Application not found.");
  101.                 Environment.Exit(0);
  102.             }
  103.  
  104.             response = encryption.decrypt(response, secret, init_iv);
  105.             var json = response_decoder.string_to_generic<response_structure>(response);
  106.  
  107.             if (json.success)
  108.             {
  109.                 sessionid = json.sessionid;
  110.                 initzalized = true;
  111.             }
  112.             else if (json.message == "invalidver")
  113.             {
  114.                 Process.Start(json.download);
  115.                 Environment.Exit(0);
  116.             }
  117.             else
  118.             {
  119.                 MessageBox.Show(json.message);
  120.                 Environment.Exit(0);
  121.             }
  122.  
  123.         }
  124.  
  125.         public bool register(string username, string pass, string key)
  126.         {
  127.             if (!initzalized)
  128.             {
  129.                 MessageBox.Show("Please initzalize first");
  130.                 return false;
  131.             }
  132.  
  133.             string hwid = WindowsIdentity.GetCurrent().User.Value;
  134.  
  135.             var init_iv = encryption.sha256(encryption.iv_key());
  136.  
  137.             var values_to_upload = new NameValueCollection
  138.             {
  139.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("register")),
  140.                 ["username"] = encryption.encrypt(username, enckey, init_iv),
  141.                 ["pass"] = encryption.encrypt(pass, enckey, init_iv),
  142.                 ["key"] = encryption.encrypt(key, enckey, init_iv),
  143.                 ["hwid"] = encryption.encrypt(hwid, enckey, init_iv),
  144.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  145.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  146.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  147.                 ["init_iv"] = init_iv
  148.             };
  149.  
  150.             var response = req(values_to_upload);
  151.  
  152.             response = encryption.decrypt(response, enckey, init_iv);
  153.             var json = response_decoder.string_to_generic<response_structure>(response);
  154.  
  155.             if (!json.success)
  156.             {
  157.                 MessageBox.Show(json.message);
  158.                 return false;
  159.             }
  160.             else
  161.             {
  162.                 load_user_data(json.info);
  163.                 // optional success msg
  164.                 return true;
  165.             }
  166.         }
  167.  
  168.         public bool login(string username, string pass)
  169.         {
  170.             if (!initzalized)
  171.             {
  172.                 MessageBox.Show("Please initzalize first");
  173.                 return false;
  174.             }
  175.  
  176.             string hwid = WindowsIdentity.GetCurrent().User.Value;
  177.  
  178.             var init_iv = encryption.sha256(encryption.iv_key());
  179.  
  180.             var values_to_upload = new NameValueCollection
  181.             {
  182.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("login")),
  183.                 ["username"] = encryption.encrypt(username, enckey, init_iv),
  184.                 ["pass"] = encryption.encrypt(pass, enckey, init_iv),
  185.                 ["hwid"] = encryption.encrypt(hwid, enckey, init_iv),
  186.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  187.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  188.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  189.                 ["init_iv"] = init_iv
  190.             };
  191.  
  192.             var response = req(values_to_upload);
  193.  
  194.             response = encryption.decrypt(response, enckey, init_iv);
  195.             var json = response_decoder.string_to_generic<response_structure>(response);
  196.  
  197.             if (!json.success)
  198.             {
  199.                 MessageBox.Show(json.message);
  200.                 return false;
  201.             }
  202.             else
  203.             {
  204.                 load_user_data(json.info);
  205.                 // optional success msg
  206.                 return true;
  207.             }
  208.         }
  209.  
  210.         public void upgrade(string username, string key)
  211.         {
  212.             if (!initzalized)
  213.             {
  214.                 MessageBox.Show("Please initzalize first");
  215.                 return;
  216.             }
  217.  
  218.             string hwid = WindowsIdentity.GetCurrent().User.Value;
  219.  
  220.             var init_iv = encryption.sha256(encryption.iv_key());
  221.  
  222.             var values_to_upload = new NameValueCollection
  223.             {
  224.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("upgrade")),
  225.                 ["username"] = encryption.encrypt(username, enckey, init_iv),
  226.                 ["key"] = encryption.encrypt(key, enckey, init_iv),
  227.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  228.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  229.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  230.                 ["init_iv"] = init_iv
  231.             };
  232.  
  233.             var response = req(values_to_upload);
  234.  
  235.             response = encryption.decrypt(response, enckey, init_iv);
  236.             var json = response_decoder.string_to_generic<response_structure>(response);
  237.  
  238.             if (!json.success)
  239.             {
  240.                 MessageBox.Show(json.message);
  241.                 Environment.Exit(0);
  242.             }
  243.             else
  244.             {
  245.                 MessageBox.Show(json.message);
  246.                 // optional success msg
  247.             }
  248.         }
  249.  
  250.         public bool license(string key)
  251.         {
  252.             if (!initzalized)
  253.             {
  254.                 MessageBox.Show("Please initzalize first");
  255.                 return false;
  256.             }
  257.  
  258.             string hwid = WindowsIdentity.GetCurrent().User.Value;
  259.  
  260.             var init_iv = encryption.sha256(encryption.iv_key());
  261.  
  262.             var values_to_upload = new NameValueCollection
  263.             {
  264.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("license")),
  265.                 ["key"] = encryption.encrypt(key, enckey, init_iv),
  266.                 ["hwid"] = encryption.encrypt(hwid, enckey, init_iv),
  267.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  268.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  269.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  270.                 ["init_iv"] = init_iv
  271.             };
  272.  
  273.             var response = req(values_to_upload);
  274.  
  275.             response = encryption.decrypt(response, enckey, init_iv);
  276.  
  277.             var json = response_decoder.string_to_generic<response_structure>(response);
  278.  
  279.             if (!json.success)
  280.             {
  281.                 MessageBox.Show(json.message);
  282.                 Environment.Exit(0);
  283.                 return false;
  284.             }
  285.             else
  286.             {
  287.                 // optional success msg
  288.                 load_user_data(json.info);
  289.                 return true;
  290.             }
  291.         }
  292.  
  293.         public void ban()
  294.         {
  295.             if (!initzalized)
  296.             {
  297.                 MessageBox.Show("Please initzalize first");
  298.                 return;
  299.             }
  300.  
  301.             var init_iv = encryption.sha256(encryption.iv_key());
  302.  
  303.             var values_to_upload = new NameValueCollection
  304.             {
  305.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("ban")),
  306.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  307.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  308.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  309.                 ["init_iv"] = init_iv
  310.             };
  311.  
  312.             var response = req(values_to_upload);
  313.  
  314.             response = encryption.decrypt(response, enckey, init_iv);
  315.             var json = response_decoder.string_to_generic<response_structure>(response);
  316.  
  317.             if (!json.success)
  318.             {
  319.                 MessageBox.Show(json.message);
  320.                 Environment.Exit(0);
  321.             }
  322.             else
  323.             {
  324.                 // optional success msg
  325.             }
  326.         }
  327.  
  328.         public string var(string varid)
  329.         {
  330.             if (!initzalized)
  331.             {
  332.                 MessageBox.Show("Please initzalize first");
  333.                 return "";
  334.             }
  335.  
  336.             string hwid = WindowsIdentity.GetCurrent().User.Value;
  337.  
  338.             var init_iv = encryption.sha256(encryption.iv_key());
  339.  
  340.             var values_to_upload = new NameValueCollection
  341.             {
  342.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("var")),
  343.                 ["varid"] = encryption.encrypt(varid, enckey, init_iv),
  344.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  345.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  346.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  347.                 ["init_iv"] = init_iv
  348.             };
  349.  
  350.             var response = req(values_to_upload);
  351.  
  352.             response = encryption.decrypt(response, enckey, init_iv);
  353.             var json = response_decoder.string_to_generic<response_structure>(response);
  354.  
  355.             if (!json.success)
  356.             {
  357.                 MessageBox.Show(json.message);
  358.                 return "";
  359.             }
  360.             else
  361.             {
  362.                 return json.message;
  363.             }
  364.         }
  365.  
  366.         public void webhook(string webid, string param)
  367.         {
  368.             if (!initzalized)
  369.             {
  370.                 MessageBox.Show("Please initzalize first");
  371.                 return;
  372.             }
  373.  
  374.             string hwid = WindowsIdentity.GetCurrent().User.Value;
  375.  
  376.             var init_iv = encryption.sha256(encryption.iv_key());
  377.  
  378.             var values_to_upload = new NameValueCollection
  379.             {
  380.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("webhook")),
  381.                 ["webid"] = encryption.encrypt(webid, enckey, init_iv),
  382.                 ["params"] = encryption.encrypt(param, enckey, init_iv),
  383.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  384.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  385.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  386.                 ["init_iv"] = init_iv
  387.             };
  388.  
  389.             var response = req(values_to_upload);
  390.  
  391.             response = encryption.decrypt(response, enckey, init_iv);
  392.             var json = response_decoder.string_to_generic<response_structure>(response);
  393.  
  394.             if (!json.success)
  395.             {
  396.                 MessageBox.Show(json.message);
  397.             }
  398.             else
  399.             {
  400.                 // optional success message
  401.             }
  402.         }
  403.  
  404.         public byte[] download(string fileid)
  405.         {
  406.             if (!initzalized)
  407.             {
  408.                 MessageBox.Show("Please initzalize first");
  409.                 return new byte[0];
  410.             }
  411.  
  412.             var init_iv = encryption.sha256(encryption.iv_key());
  413.  
  414.             var values_to_upload = new NameValueCollection
  415.             {
  416.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("file")),
  417.                 ["fileid"] = encryption.encrypt(fileid, enckey, init_iv),
  418.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  419.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  420.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  421.                 ["init_iv"] = init_iv
  422.             };
  423.  
  424.             var response = req(values_to_upload);
  425.  
  426.             response = encryption.decrypt(response, enckey, init_iv);
  427.  
  428.             var json = response_decoder.string_to_generic<response_structure>(response);
  429.  
  430.             if (!json.success)
  431.             {
  432.                 MessageBox.Show(json.message);
  433.             }
  434.             else
  435.             {
  436.                 // optional success message
  437.             }
  438.  
  439.             return encryption.str_to_byte_arr(json.contents);
  440.         }
  441.  
  442.         public void log(string message)
  443.         {
  444.             if (!initzalized)
  445.             {
  446.                 MessageBox.Show("Please initzalize first");
  447.                 return;
  448.             }
  449.  
  450.             var init_iv = encryption.sha256(encryption.iv_key());
  451.             var values_to_upload = new NameValueCollection
  452.             {
  453.                 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("log")),
  454.                 ["pcuser"] = encryption.encrypt(Environment.UserName, enckey, init_iv),
  455.                 ["message"] = encryption.encrypt(message, enckey, init_iv),
  456.                 ["sessionid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(sessionid)),
  457.                 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
  458.                 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
  459.                 ["init_iv"] = init_iv
  460.             };
  461.  
  462.             req(values_to_upload);
  463.         }
  464.  
  465.         private static string req(NameValueCollection post_data)
  466.         {
  467.             try
  468.             {
  469.                 using (WebClient client = new WebClient())
  470.                 {
  471.                     var raw_response = client.UploadValues("https://keyauth.business/1.0/", post_data);
  472.  
  473.                     return Encoding.Default.GetString(raw_response);
  474.                 }
  475.             }
  476.             catch
  477.             {
  478.                 MessageBox.Show("Connection failure. Please try again, or contact us for help.");
  479.                 Thread.Sleep(3500);
  480.                 Environment.Exit(0);
  481.                 return "nothing";
  482.             }
  483.         }
  484.  
  485.  
  486.         #region user_data
  487.         public user_data_class user_data = new user_data_class();
  488.  
  489.         public class user_data_class
  490.         {
  491.             public string username { get; set; }
  492.             public List<Data> subscriptions { get; set; }
  493.             public string ip { get; set; }
  494.         }
  495.         public class Data
  496.         {
  497.             public string subscription { get; set; }
  498.             public string expiry { get; set; }
  499.         }
  500.         private void load_user_data(user_data_structure data)
  501.         {
  502.             user_data.username = data.username;
  503.             user_data.ip = data.ip;
  504.             user_data.subscriptions = data.subscriptions;
  505.         }
  506.         #endregion
  507.  
  508.         private json_wrapper response_decoder = new json_wrapper(new response_structure());
  509.     }
  510.  
  511.     public static class encryption
  512.     {
  513.         public static string byte_arr_to_str(byte[] ba)
  514.         {
  515.             StringBuilder hex = new StringBuilder(ba.Length * 2);
  516.             foreach (byte b in ba)
  517.                 hex.AppendFormat("{0:x2}", b);
  518.             return hex.ToString();
  519.         }
  520.  
  521.         public static byte[] str_to_byte_arr(string hex)
  522.         {
  523.             int NumberChars = hex.Length;
  524.             byte[] bytes = new byte[NumberChars / 2];
  525.             for (int i = 0; i < NumberChars; i += 2)
  526.                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  527.             return bytes;
  528.         }
  529.  
  530.         public static string encrypt_string(string plain_text, byte[] key, byte[] iv)
  531.         {
  532.             Aes encryptor = Aes.Create();
  533.  
  534.             encryptor.Mode = CipherMode.CBC;
  535.             encryptor.Key = key;
  536.             encryptor.IV = iv;
  537.  
  538.             using (MemoryStream mem_stream = new MemoryStream())
  539.             {
  540.                 using (ICryptoTransform aes_encryptor = encryptor.CreateEncryptor())
  541.                 {
  542.                     using (CryptoStream crypt_stream = new CryptoStream(mem_stream, aes_encryptor, CryptoStreamMode.Write))
  543.                     {
  544.                         byte[] p_bytes = Encoding.Default.GetBytes(plain_text);
  545.  
  546.                         crypt_stream.Write(p_bytes, 0, p_bytes.Length);
  547.  
  548.                         crypt_stream.FlushFinalBlock();
  549.  
  550.                         byte[] c_bytes = mem_stream.ToArray();
  551.  
  552.                         return byte_arr_to_str(c_bytes);
  553.                     }
  554.                 }
  555.             }
  556.         }
  557.  
  558.         public static string decrypt_string(string cipher_text, byte[] key, byte[] iv)
  559.         {
  560.             Aes encryptor = Aes.Create();
  561.  
  562.             encryptor.Mode = CipherMode.CBC;
  563.             encryptor.Key = key;
  564.             encryptor.IV = iv;
  565.  
  566.             using (MemoryStream mem_stream = new MemoryStream())
  567.             {
  568.                 using (ICryptoTransform aes_decryptor = encryptor.CreateDecryptor())
  569.                 {
  570.                     using (CryptoStream crypt_stream = new CryptoStream(mem_stream, aes_decryptor, CryptoStreamMode.Write))
  571.                     {
  572.                         byte[] c_bytes = str_to_byte_arr(cipher_text);
  573.  
  574.                         crypt_stream.Write(c_bytes, 0, c_bytes.Length);
  575.  
  576.                         crypt_stream.FlushFinalBlock();
  577.  
  578.                         byte[] p_bytes = mem_stream.ToArray();
  579.  
  580.                         return Encoding.Default.GetString(p_bytes, 0, p_bytes.Length);
  581.                     }
  582.                 }
  583.             }
  584.         }
  585.  
  586.         public static string iv_key() =>
  587.             Guid.NewGuid().ToString().Substring(0, Guid.NewGuid().ToString().IndexOf("-", StringComparison.Ordinal));
  588.  
  589.         public static string sha256(string r) =>
  590.             byte_arr_to_str(new SHA256Managed().ComputeHash(Encoding.Default.GetBytes(r)));
  591.  
  592.         public static string encrypt(string message, string enc_key, string iv)
  593.         {
  594.             byte[] _key = Encoding.Default.GetBytes(sha256(enc_key).Substring(0, 32));
  595.  
  596.             byte[] _iv = Encoding.Default.GetBytes(sha256(iv).Substring(0, 16));
  597.  
  598.             return encrypt_string(message, _key, _iv);
  599.         }
  600.  
  601.         public static string decrypt(string message, string enc_key, string iv)
  602.         {
  603.             byte[] _key = Encoding.Default.GetBytes(sha256(enc_key).Substring(0, 32));
  604.  
  605.             byte[] _iv = Encoding.Default.GetBytes(sha256(iv).Substring(0, 16));
  606.  
  607.             return decrypt_string(message, _key, _iv);
  608.         }
  609.     }
  610.  
  611.     public class json_wrapper
  612.     {
  613.         public static bool is_serializable(Type to_check) =>
  614.             to_check.IsSerializable || to_check.IsDefined(typeof(DataContractAttribute), true);
  615.  
  616.         public json_wrapper(object obj_to_work_with)
  617.         {
  618.             current_object = obj_to_work_with;
  619.  
  620.             var object_type = current_object.GetType();
  621.  
  622.             serializer = new DataContractJsonSerializer(object_type);
  623.  
  624.             if (!is_serializable(object_type))
  625.                 throw new Exception($"the object {current_object} isn't a serializable");
  626.         }
  627.  
  628.         public object string_to_object(string json)
  629.         {
  630.             var buffer = Encoding.Default.GetBytes(json);
  631.  
  632.             //SerializationException = session expired
  633.  
  634.             using (var mem_stream = new MemoryStream(buffer))
  635.                 return serializer.ReadObject(mem_stream);
  636.         }
  637.  
  638.         public T string_to_generic<T>(string json) =>
  639.             (T)string_to_object(json);
  640.  
  641.         private DataContractJsonSerializer serializer;
  642.  
  643.         private object current_object;
  644.     }
  645. }
  646.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement