Exemplo n.º 1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            AuthorizeFile af = new AuthorizeFile();

            af.Version = cbxVersions.Text;

            af.AuthorizeType    = cbxAuthoTypes.Text;
            af.AuthorizeContent = txtContent.Text;

            if (string.IsNullOrWhiteSpace(af.Version))
            {
                MessageBox.Show("版本类型不可为空");
                return;
            }

            if (string.IsNullOrWhiteSpace(af.AuthorizeContent))
            {
                MessageBox.Show("授权类型中的内容不可为空");
                return;
            }

            string         localFilePath   = String.Empty;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            //设置文件类型
            saveFileDialog1.Filter = "授权文件(*.dat)|*.dat|所有文件(*.*)|*.*";
            //设置文件名称:
            saveFileDialog1.FileName = "授权文件[" + af.Version + "+" + af.AuthorizeType + "]" + DateTime.Now.ToString("yyyyMMddHHmm") + ".dat";

            //设置默认文件类型显示顺序
            saveFileDialog1.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录
            saveFileDialog1.RestoreDirectory = true;

            //点了保存按钮进入
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //获得文件路径
                localFilePath = saveFileDialog1.FileName.ToString();

                string json = af.ToJson();
                //读取公钥内容
                string publicKeyXml = VersionSettingsManager.Instance.ReadPublicKey(af.Version);
                //读取私钥内容
                string privateKeyXml = VersionSettingsManager.Instance.ReadPrivateKey(af.Version);

                LisenceSign signer = new LisenceSign();
                var         sign   = signer.Sign(json, privateKeyXml);

                System.IO.File.WriteAllText(localFilePath, json + "+++++=====+++++" + sign + "+++++=====+++++" + publicKeyXml);
            }
        }
Exemplo n.º 2
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title       = "请选择要打开的文件";
            ofd.Multiselect = false;
            ofd.Filter      = "授权文件(*.dat)|*.dat|所有文件(*.*)|*.*";;
            ofd.FilterIndex = 1;
            //设置对话框是否记忆之前打开的目录
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的文件完整路径
                string   filePath = ofd.FileName;
                string   content  = System.IO.File.ReadAllText(filePath);
                string[] items    = content.Split(new string[] { "+++++=====+++++" }, StringSplitOptions.RemoveEmptyEntries);

                if (items.Length != 3)
                {
                    MessageBox.Show("授权文件格式无法识别");
                    return;
                }

                string json         = items[0];
                string sign         = items[1];
                string publicKeyXml = items[2];

                LisenceSign signer   = new LisenceSign();
                var         isVerify = signer.Verify(json, sign, publicKeyXml);

                if (isVerify == false)
                {
                    MessageBox.Show("授权文件格式校验未通过");
                    return;
                }

                AuthorizeFile af = AuthorizeFile.FromJson(json);

                cbxVersions.Text = af.Version;

                cbxAuthoTypes.Text = af.AuthorizeType;
                txtContent.Text    = af.AuthorizeContent;
                txtSignDate.Text   = af.SignDate.ToString("yyyy年MM月dd日");
            }
        }