<p>How can I encrypt and decrypt a string in C#?</p>

Here is an example using RSA.
Important:
There is a limit to the size of data you can encrypt with the RSA encryption,
```
KeySize - MinimumPadding
```
.
e.g. 256 bytes (assuming 2048 bit key) - 42 bytes (min OEAP padding) = 214 bytes (max plaintext size)
Replace your_rsa_key with your RSA key.
```
```
var provider = new System.Security.Cryptography.RSACryptoServiceProvider();
provider.ImportParameters(your_rsa_key);

var encryptedBytes = provider.Encrypt(
    System.Text.Encoding.UTF8.GetBytes("Hello World!"), true);

string decryptedTest = System.Text.Encoding.UTF8.GetString(
    provider.Decrypt(encryptedBytes, true));
```
```
For more info, visit
MSDN - RSACryptoServiceProvider