private void btnDecode_Click(object sender, System.EventArgs e) { LicenseKey.GenerateKey gkey; int numtokens; int lop; string result; string tokenvalue = ""; if (txtKeyInput.Text.Length == 0) { MessageBox.Show("Enter a Key Template", "Layout"); txtKeyInput.Focus(); return; } gkey = new LicenseKey.GenerateKey(); // set the base type. if (rdobtn10.Checked) { // use base 10 gkey.UseBase10 = true; } else { // use base 16 gkey.UseBase10 = false; } // set the token type. if (rdobtnBytes.Checked) { // use bytes gkey.UseBytes = true; } else { // use bits gkey.UseBytes = false; } // set the Checksum type. if (rdoBtnChksum1.Checked) { // use Algorithm 1 gkey.ChecksumAlgorithm = Checksum.ChecksumType.Type1; } else { // use Algorithm 2 gkey.ChecksumAlgorithm = Checksum.ChecksumType.Type2; } // Set the number of tokens. numtokens = listView1.Items.Count; if (numtokens > 0) { gkey.MaxTokens = numtokens; lop = 0; foreach (ListViewItem item in listView1.Items) { LicenseKey.GenerateKey.TokenTypes ttypes; string tempstr; tempstr = item.SubItems[1].Text; ttypes = LicenseKey.GenerateKey.TokenTypes.NUMBER; // default junk switch (tempstr) { case "numeric": case "Numeric": ttypes = LicenseKey.GenerateKey.TokenTypes.NUMBER; break; case "date": case "Date": ttypes = LicenseKey.GenerateKey.TokenTypes.DATE; break; case "character": case "Character": ttypes = LicenseKey.GenerateKey.TokenTypes.CHARACTER; break; default: MessageBox.Show("Illegal Token type in switch", "Generatekey"); break; } try { gkey.AddToken(lop, item.SubItems[0].Text, ttypes, item.SubItems[2].Text); } catch (Exception ex) { MessageBox.Show("Error is " + ex.InnerException); btnAdd.Focus(); return; } lop++; } gkey.LicenseTemplate = txtKeyInput.Text; // first create the key try { gkey.CreateKey(); } catch (Exception ex) { MessageBox.Show("Error exception is: " + ex.Message); return; } txtLicKey.Text = gkey.GetLicenseKey(); foreach (ListViewItem item in listView1.Items) { tokenvalue = item.SubItems[0].Text; // now decode the key try { result = gkey.DisassembleKey(tokenvalue); } catch (Exception ex) { MessageBox.Show("Error exception is: " + ex.Message); return; } item.SubItems[3].Text = result; } } }
/// <summary> /// Test a generic key generation with 5 parameters 5-5-5-5-5 /// </summary> [Test] public void Test99_DoFile() { // The StreamWriter must be defined outside of the try-catch block // in order to reference it in the Finally block. StreamWriter myStreamWriter = null; StreamReader myStreamReader = null; LicenseKey.GenerateKey gkey; string myInputstring; string delimStr = " "; char [] delimiter = delimStr.ToCharArray(); string [] split = null; int x = 8; int nNumTokens; int lop; int lnum; int ncnt; string arg1; string arg2; string arg3; string arg4; string arg5; string arg6; ncnt = 0; // Ensure that the creation of the new StreamWriter is wrapped in a // try-catch block, since an invalid filename could have been used. try { // create the license key object gkey = new LicenseKey.GenerateKey(); // Create a StreamReader using a static File class. myStreamReader = File.OpenText("LicTestDofile.TXT"); // Create a StreamWriter using a static File class. myStreamWriter = File.CreateText("LicTestDofileout.txt"); // Begin by reading a single line // Continue reading while there are still lines to be read while ((myInputstring = myStreamReader.ReadLine()) != null) { split = myInputstring.Split(delimiter, x); if (split.Length != x) { break; } arg1 = split[0]; // base 1= 10 0 =16 arg2 = split[1]; // bytes 1 = bytes 0 = bits arg3 = split[2]; // number of tokens myStreamWriter.Write(arg1 + " " + arg2 + " " + arg3 + " "); // see what base we are to use if (arg1 == "1") { // use base 10 gkey.UseBase10 = true; } else { // use base 16 gkey.UseBase10 = false; } // see what format we use if (arg2 == "1") { // use bytes gkey.UseBytes = true; } else { // use bits gkey.UseBytes = false; } // set the number of tokens nNumTokens = Convert.ToInt32(arg3); gkey.MaxTokens = nNumTokens; lnum = 3; for (lop = 0; lop < nNumTokens; lop++) { arg4 = split[lnum]; lnum++; arg5 = split[lnum]; lnum++; myStreamWriter.Write(arg4 + " " + arg5 + " "); try { gkey.AddToken(lop, arg4, LicenseKey.GenerateKey.TokenTypes.NUMBER, arg5); } catch (Exception ex) { Assert.AreEqual(66, 1, "Error from AddToken. " + ex.Message); return; } } arg6 = split[lnum]; gkey.LicenseTemplate = arg6; try { gkey.CreateKey(); } catch (Exception ex) { Assert.AreEqual(66, 1, "Error from CreateKey. " + ex.Message); return; } myStreamWriter.WriteLine(arg6 + " " + gkey.GetLicenseKey()); ncnt++; // myStreamWriter.WriteLine(arg1 + " " + arg2 + " " + arg3 + " " + arg4 + " " + arg5 + " " + gkey.getLicenseKey()); // myStreamWriter.WriteLine(arg1 + " " + arg2 + " " + arg3 + " " + arg6 + " " + gkey.getLicenseKey()); } myStreamWriter.Flush(); } catch (Exception exc) { string errstr; errstr = "File could not be opened or read." + Environment.NewLine + "Please verify that the filename is correct, and that you have read permissions for the desired directory." + Environment.NewLine + Environment.NewLine + "Exception: " + exc.Message; // Show the error to the user. Assert.AreEqual(66, 1, "UnExpected Failure. " + errstr); } finally { // Close the object if it has been created. if (myStreamReader != null) { myStreamReader.Close(); } // Close the object if it has been created. if (myStreamWriter != null) { myStreamWriter.Close(); } } }