Simple C# whitelist, problems with looking up username and expiry

0

I might not be writing this right (I'm new to C# lol) but I need help with a C# whitelist/activation/license thing.

So if I write 1111-1111-1111-1111 as license key in the textbox, I get 31-12-9999 as expiry and Not Discord#5550 as username which is intended but does not work well for multiple lines.

I need the expiry date and username as well that's right next to the key, I'm trying to split the string but it only works for like one line at a time, so even if I use 2222-2222-2222-2222 as a key, it will STILL get Not Discord#5550 as username which is not supposed to happen and should be charlootus#3330.

I have left the code for you guys to look at and fix.

Whitelist (also as pastebin, look in code):

1111-1111-1111-1111|31-12-9999|Not Discord#5550  
2222-2222-2222-2222|31-12-9999|charlootus#3330

Simplified:

the program DOES NOT know the expiry or username, it gets it from a string downloaded from Pastebin, and uses the key entered to search up the adjacent username and expiry date, then validates. The second key is pulling the expiry and username from the first line which isn't supposed to happen.

Should be an easy fix, but I've looked around places and can't seem to find anything.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace loader
{
    // Token: 0x02000002 RID: 2
    public partial class entry : Form
    {
        // Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
        public entry()
        {
            this.InitializeComponent();
        }

        // Token: 0x06000002 RID: 2 RVA: 0x00002234 File Offset: 0x00000434
        private async void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length != 19)
            {
                string caption = "License key is not valid.";
                string text = "License key is not valid." + Environment.NewLine + Environment.NewLine + "Make sure you enter it in AAAA-BBBB-CCCC-DDDD format.";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                if (MessageBox.Show(this, text, caption, buttons, MessageBoxIcon.Hand) == DialogResult.OK)
                {
                    this.button1.Text = "Submit key";
                    this.button1.Enabled = true;
                }
            }
            else
            {
                this.textBox1.Enabled = false;
                this.textBox1.Text = this.textBox1.Text.ToUpper();
                this.button1.Enabled = false;
                string[] array = new string[]
                {
                    "Verifying key.",
                    "Verifying key..",
                    "Verifying key...",
                };
                foreach (string text2 in array)
                {
                    this.button1.Text = text2;
                    await Task.Delay(new Random().Next(450, 850));
                }
                string[] array2 = null;
                string whitelist = new System.Net.WebClient() { Proxy = null }.DownloadString("https://pastebin.com/raw/ai8q5GEA");
                string fin = String.Format(this.textBox1.Text);
                string[] result = whitelist.Split('|');
                string whitelistx = result[0];
                string expiry = result[1];
                string username = result[2];
                if (whitelist.Contains(fin))
                {
                    var date = DateTime.ParseExact(expiry, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
                    var result1 = (date - DateTime.Now.Date).Days;
                    if (result1 >= 0)
                    { 
                        string caption2 = "Success!";
                    if (MessageBox.Show(this, string.Concat(new string[]
                    {
                        "License key is valid.",
                        Environment.NewLine,
                        Environment.NewLine,
                        "Hi ", username, "! Key expires on the ", expiry,
                        Environment.NewLine,
                        "Restart DeluxeUnban."
                }), caption2, MessageBoxButtons.OK, MessageBoxIcon.Asterisk) == DialogResult.OK)
                    {
                        this.button1.Text = "Verify license";
                        this.button1.Enabled = true;
                        this.textBox1.Enabled = true;
                        System.IO.File.WriteAllText(@"C:\NVIDIA\username.txt", username);
                        System.IO.File.WriteAllText(@"C:\NVIDIA\expiry.txt", expiry);
                        System.IO.File.WriteAllText(@"C:\NVIDIA\license.txt", this.textBox1.Text);
                    }
                    Application.Exit();
                    }
                    else
                    {
                        string caption3 = "License key is not valid";
                        if (MessageBox.Show(this, "License key has expired.", caption3, MessageBoxButtons.OK, MessageBoxIcon.Hand) == DialogResult.OK)
                        {
                            this.button1.Text = "Verify license";
                            this.button1.Enabled = true;
                            this.textBox1.Enabled = true;
                        }
                    }
                }
                else
                {
                    string caption3 = "License key is not valid";
                    if (MessageBox.Show(this, "License key is incorrect." + Environment.NewLine + Environment.NewLine + "Make sure you didn't enter O instead of 0 (zero) or 1 instead of l (small L)", caption3, MessageBoxButtons.OK, MessageBoxIcon.Hand) == DialogResult.OK)
                    {
                        this.button1.Text = "Verify license";
                        this.button1.Enabled = true;
                        this.textBox1.Enabled = true;
                    }
                }
            }
        }

        // Token: 0x06000003 RID: 3 RVA: 0x0000205E File Offset: 0x0000025E
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("link hidden");
        }
    }
}
c#
username
whitelist
asked on Stack Overflow Feb 1, 2019 by (unknown user) • edited Mar 3, 2019 by Zoe

3 Answers

1

It's may another approach, but the error seems to be the \r\n which you get for each new line.

I wrote a few lines which get rid of this error and works just fine, please read the comments and ask if anything is unclear!

/// <summary>
/// Class for a Licence
/// </summary>
class Licence
{
    public string sKey { get; set; }
    public string sExpiry { get; set; }
    public string sName { get; set; }
}

static void Main(string[] args)
{
    // Loading the data with a WebClient
    WebClient WB = new WebClient();
    // Downloads the string
    string whitelist = WB.DownloadString("https://pastebin.com/raw/ai8q5GEA");

    // List with all licences
    List<Licence> licences = new List<Licence>();

    // Foreach Row
    foreach (string sRow in whitelist.Split('\n'))
    {
        // Splits the Row
        string[] sData = sRow.Split('|');

        // Adds the licence data
        licences.Add(new Licence
        {
            sKey = sData[0],
            sExpiry = sData[1],
            sName = sData[2]
        });
    }

    // User input
    string fin = String.Format("2222-2222-2222-2222");

    // Gets the licence class for the specific key
    Licence lic = licences.Find(o => o.sKey == fin);

    // If there a licence found
    if (lic != default(Licence))
    {
        // Uses the licence class for data output
        var date = DateTime.ParseExact(lic.sExpiry, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
        var result1 = (date - DateTime.Now.Date).Days;
        if (result1 >= 0)
        {
            Console.WriteLine(string.Concat(new string[]
            {
                "License key is valid.",
                Environment.NewLine,
                Environment.NewLine,
                "Hi ", lic.sName, "! Key expires on the ", lic.sExpiry,
                Environment.NewLine,
                "Restart DeluxeUnban."
            }));
        }
    }
    Console.WriteLine("FINISHED");
    Console.ReadKey();
}
answered on Stack Overflow Feb 1, 2019 by Hille • edited Feb 1, 2019 by Hille
0

Your whitelist contains two lines of text. So your first step should be splitting it by new line

string listOfUsers = whitelist.Split('\n');

Rest of your code can be put in foreach or for loop like below:

foreach(var user in ListOfUsers) // thanks to it you will check all users, not only the first one
{
    string[] result = user.Split('|');
    string whitelistx = result[0];
    string expiry = result[1];
    string username = result[2];
    if (user.Contains(fin))
    {
         // rest of your code

Of course, you can optimize this code, but that is just to answer your question about checking only the first user.

answered on Stack Overflow Feb 1, 2019 by Piotr Wojsa
0

The problem is that you search on all lines at once. You need to split on linebreaks and search each line individually.

string data = new System.Net.WebClient() { Proxy = null }.DownloadString("https://pastebin.com/raw/ai8q5GEA");

// Split on line breaks
var lines = data.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

// Search for license key (might want to change this) 
var foundLicense = lines.FirstOrDefault(l => l.StartsWith(fin));
if (foundLicense != null)
{
    var values = foundLicense.Split('|');
    var whitelist = values[0];
    var date = DateTime.ParseExact(values[1], "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
    var username = values[2];
    /* ...  */
}

Personally I would create a class (DTO) to hold information when parsing the text.

void Main()
{
    string fin = "111";

    string data = new System.Net.WebClient() { Proxy = null }.DownloadString("https://pastebin.com/raw/ai8q5GEA");

    // Parse data to DTO-objects
    var licenseData = data.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
        .Select(l => l.Split('|'))
        .Select(x => new LicenseModel
        {
            LicenseKey = x[0],
            Date = DateTime.ParseExact(x[1], "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),
            Username = x[2]
        })
        .ToList();

    // Find matching license
    var foundLicense = licenseData.FirstOrDefault(l => l.LicenseKey.StartsWith(fin));
    if (foundLicense != null)
    {
        /* FOUND LICENSE  */
    }
}


public class LicenseModel
{
    public string LicenseKey { get; set; }
    public DateTime Date { get; set; }
    public string Username { get; set; }
}
answered on Stack Overflow Feb 1, 2019 by Torbjörn Hansson • edited Feb 1, 2019 by Torbjörn Hansson

User contributions licensed under CC BY-SA 3.0