Exemplo n.º 1
0
        private void btnAsymmEncrypt_Click(object sender, EventArgs e)
        {
            AsymmetricCrypto crypto = new AsymmetricCrypto();

            // 產生公鑰與私鑰
            AsymmetricCrypto.PublicKey  pubKey = new AsymmetricCrypto.PublicKey();
            AsymmetricCrypto.PrivateKey priKey = new AsymmetricCrypto.PrivateKey();
            crypto.GenerateNewKeyset(ref pubKey, ref priKey);

            // 用公鑰加密
            ByteArray inputData = new ByteArray(txtRawData.Text);
            ByteArray encrypted = crypto.Encrypt(inputData, pubKey);

            txtAsymmEncrypted.Text = encrypted.Base64String;

            // 用私鑰解密
            ByteArray decrypted = crypto.Decrypt(encrypted, priKey);

            txtAsymmDecrypted.Text = decrypted.Text;
        }