Exemplo n.º 1
0
        private void btnG_Click(object sender, RoutedEventArgs e)
        {
            byte bytUsrLv = 0;

            if (cbkeylv.SelectedItem == null)
            {
                MessageBox.Show("Plz Select the User Level!!");
                return;
            }
            bytUsrLv = byte.Parse((cbkeylv.SelectedItem as ComboBoxItem).Tag.ToString());
            Int32 usId = 0;

            try
            {
                usId = Int32.Parse(tbId.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("Plz Insert Valid UserId!!");
                return;
            }
            _UsbKeyDataStruct.UsrLv = bytUsrLv;
            _UsbKeyDataStruct.UsrID = usId;
            byte[] tmp = System.Text.Encoding.ASCII.GetBytes(tbCpr.Text);
            _UsbKeyDataStruct.copyright = new byte[20];
            Array.Copy(tmp, _UsbKeyDataStruct.copyright, (int)(tmp.Length > 20 ? 20 : tmp.Length));
            _UsbKeyDataStruct.EncryptLen = (UInt16)(4 + 4 + 1 + 4 + _UsbKeyDataStruct.Sn.Length);



            byte[] Encrypbuf = _UsbKeyDataStruct.Decode();
            byte[] toDecry   = new byte[Encrypbuf.Length - 20];

            Array.Copy(Encrypbuf, 20, toDecry, 0, toDecry.Length);
            byte[] doencry = Encrypt(toDecry);

            byte[] Destbuf = new byte[doencry.Length + 20];
            //save to file
            //using (StreamWriter sw = new StreamWriter(@"USBKEY\SCR.TOK",false))
            // {
            //     sw.Write(Encrypbuf,0,
            //     sw.Flush();
            //     sw.Close();
            // }
            Array.Copy(_UsbKeyDataStruct.copyright, 0, Destbuf, 0, 20);
            Array.Copy(doencry, 0, Destbuf, 20, doencry.Length);

            FileStream   fs = new FileStream(@"USBKEY\SCR.TOK", FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            fs.Write(Destbuf, 0, Destbuf.Length);
            bw.Close();
            fs.Close();
            MessageBox.Show("Generate Key finished!!");
        }
Exemplo n.º 2
0
        private void btnG_Click(object sender, RoutedEventArgs e)
        {
            byte bytUsrLv = 0;

            if (cbkeylv.SelectedItem == null)
            {
                MessageBox.Show("Please select the key type !");
                return;
            }
            bytUsrLv = byte.Parse((cbkeylv.SelectedItem as ComboBoxItem).Tag.ToString());
            Int32 usId = 0;

            try
            {
                usId = Int32.Parse(tbId.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("Please insert valid Key ID !");
                return;
            }
            _UsbKeyDataStruct.UsrLv = bytUsrLv;
            _UsbKeyDataStruct.UsrID = usId;
            byte[] tmp = System.Text.Encoding.ASCII.GetBytes("Crem ShangHai");
            _UsbKeyDataStruct.copyright = new byte[20];
            Array.Copy(tmp, _UsbKeyDataStruct.copyright, (int)(tmp.Length > 20 ? 20 : tmp.Length));
            _UsbKeyDataStruct.EncryptLen = (UInt16)(4 + 4 + 1 + 4 + _UsbKeyDataStruct.Sn.Length);



            byte[] Encrypbuf = _UsbKeyDataStruct.Decode();
            byte[] toDecry   = new byte[Encrypbuf.Length - 20];

            Array.Copy(Encrypbuf, 20, toDecry, 0, toDecry.Length);
            byte[] doencry = Encrypt(toDecry);

            byte[] Destbuf = new byte[doencry.Length + 20];
            //save to file
            //using (StreamWriter sw = new StreamWriter(@"USBKEY\SCR.TOK",false))
            // {
            //     sw.Write(Encrypbuf,0,
            //     sw.Flush();
            //     sw.Close();
            // }
            Array.Copy(_UsbKeyDataStruct.copyright, 0, Destbuf, 0, 20);
            Array.Copy(doencry, 0, Destbuf, 20, doencry.Length);



            string removedevicename = "";

            DriveInfo[] s = DriveInfo.GetDrives();

            foreach (DriveInfo i in s)
            {
                try
                {
                    if (i.DriveType == DriveType.Removable)
                    {
                        removedevicename = i.Name;
                    }
                }
                catch (Exception)
                {
                }
            }
            FileStream   fs = new FileStream(removedevicename + "SCR.TOK", FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            fs.Write(Destbuf, 0, Destbuf.Length);
            bw.Close();
            fs.Close();
            MessageBox.Show("Key successfully generated !");
        }