SoFunction
Updated on 2025-03-07

c# rsa registration to implement encrypted text


RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

private void btencoding_Click(object sender, EventArgs e)
{
    if ( == "")
    {
("Encrypted text information cannot be empty!");
        return;
    }
    if ( == "")
    {
("Please generate a public key!");
        return;
    }
    try
    {
        string pubKey = ;
        byte[] mw = ();
        RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
        (pubKey);

        mw = (mw, false);
string encryption text = Convert.ToBase64String(mw);//How to deal with the encryption results to solve the problem of garbled display
        = encryttext;
    }
    catch
    {
("Please check whether the public key is opened or whether the public key is damaged!");
    }
}

private void btdecoding_Click(object sender, EventArgs e)
{
    if ( == "")
    {
("Please generate a key!");
        return;
    }
    try
    {
        RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
byte[] bytes = Convert.FromBase64String();//The string taken from the ciphertext box can be correctly processed before decryption can be decrypted.
        string prtKey = ;
        (prtKey);
        byte[] decryptbyte = (bytes, false);
        string decrypttext = (decryptbyte);

        = decrypttext;
    }
    catch (CryptographicException ex)
    {
//("Please check whether the private key is opened or whether the private key is damaged!");
        (());
    } if ( == "")
    {
("Please generate a key!");
        return;
    }
}