⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace DesktopKeyUnlocker
{
    public partial class Form1 : Form
    {
        private RSACryptoServiceProvider rsaprovider;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string publicKey = GetRSAKeyPair("DesktopKeyUnlockerContainer");
            textBoxPublicKey.Text = publicKey;
        }

        private string GetRSAKeyPair(string containerName)
        {
            //Create a CryptoServiceProvider Parameter object
            CspParameters cspParams = new CspParameters();
            // Set the key container name that has the RSA key pair
            cspParams.KeyContainerName = containerName;
            //Set the CSP Provider Type PROV_RSA_FULL
            cspParams.ProviderType = 1;
            //Set the CSP Provider Name
            cspParams.ProviderName = "Microsoft Enhanced Cryptographic Provider v1.0";

            //Create a new RSA provider and pass our parameter object to the constructor
            //if the specified key container does not exist, a new key-pair is created.
            rsaprovider = new RSACryptoServiceProvider(cspParams);

            //Indicate that we would like the new key-pair to be persisted in the key 
            //container we specified.
            rsaprovider.PersistKeyInCsp = true;

            //return the PUBLIC key info.
            return rsaprovider.ToXmlString(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckFileExists = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBoxFilePath.Text = openFileDialog1.FileName;
            }
        }

        private void textBoxFilePath_TextChanged(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(textBoxFilePath.Text))
            {
                string cipherBase64 = System.IO.File.ReadAllText(textBoxFilePath.Text);
                byte[] cipherText = Convert.FromBase64String(cipherBase64);

                // Decrypt the file using the PRIVATE key
                byte[] decipheredText = rsaprovider.Decrypt(cipherText, false);

                //Display the Deciphered Message
                labelResult.Text = new UnicodeEncoding().GetString(decipheredText);

            }
        }


    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -