RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//---Loads the public key---
RSA.FromXmlString(publicKey);
//---Encrypts the string---
byte[] encryptedStr =
RSA.Encrypt(ASCIIEncoding.ASCII.GetBytes(str), false);
//---Converts the encrypted byte array to string---
return System.Convert.ToBase64String(encryptedStr);
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
return String.Empty;
}
}
The encrypted string is returned as a Base64-encoded string. To decrypt a string encrypted with the public key, define the following AsymmetricDecryption()
function. It takes in two parameters (the encrypted string and the private key) and returns the decrypted string.
static string AsymmetricDecryption(string str, string privateKey) {
try {
//---Creates a new instance of RSACryptoServiceProvider---
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//---Loads the private key---
RSA.FromXmlString(privateKey);
//---Decrypts the string---
byte[] DecryptedStr =
RSA.Decrypt(System.Convert.FromBase64String(str), false);
//---Converts the decrypted byte array to string---
return ASCIIEncoding.ASCII.GetString(DecryptedStr);
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
return String.Empty;
}
}
The following code snippet shows how to use the AsymmetricEncryption()
and AsymmetricDecryption()
functions to encrypt and decrypt a string:
string publicKey, privateKey;
RSACryptoServiceProvider RSA =
new RSACryptoServiceProvider();
//---get public key---
publicKey = RSA.ToXmlString(false);
Console.WriteLine('Public key: ' + publicKey);
Console.WriteLine();
//---get private and public key---
privateKey = RSA.ToXmlString(true);
Console.WriteLine('Private key: ' + privateKey);
Console.WriteLine();
//---encrypt the string---
string cipherText =
AsymmetricEncryption('C# 2008 Programmer's Reference', publicKey);
Console.WriteLine('Ciphertext: ' + cipherText);
Console.WriteLine();
//---decrypt the string---
Console.WriteLine('Original string: ' +
AsymmetricDecryption(cipherText, privateKey));
Console.WriteLine();
You can obtain the public and private keys generated by the RSA algorithm by using the ToXmlString()
method from the RSACryptoServiceProvider
class. This method takes in a Bool variable, and returns a public key if the value false is supplied. If the value true is supplied, it returns both the private and public keys.
Figure 11-8 shows the output.

Figure 11-8
Compressions for Stream Objects
The System.IO.Compression
namespace contains classes that provide basic compression and decompression services for streams. This namespace contains two classes for data compression: DeflateStream
and GZipStream
. Both support lossless compression and decompression and are designed for dealing with streams.
Compression is useful for reducing the size of data. If you have huge amount of data to store in your SQL database, for instance, you can save on disk space if you compress the data before saving it into a table. Moreover, because you are now saving smaller blocks of data into your database, the time spent in performing disk I/O is significantly reduced. The downside of compression is that it takes additional processing power from your machine (and requires additional processing time), and you need to factor in this additional time before deciding you want to use compression in your application.
Compression is extremely useful in cases where you need to transmit data over networks, especially slow and costly networks such as General Packet Radio Service (GPRS).connections. In such cases, using compression can drastically cut down the data size and reduce the overall cost of communication. Web Services are another area where using compression can provide a great advantage because XML data can be highly compressed.
But once you've decided the performance cost is worth it, you'll need help deciphering the utilization of these two compression classes, which is what this section is about.
Compression
The compression classes read data (to be compressed) from a byte array, compress it, and store the results in a Stream
object. For decompression, the compressed data stored in a Stream
object is decompressed and then stored in another Stream
object.
Let's see how you can perform compression. First, define the Compress()
function, which takes in two parameters: algo
and data
. The first parameter specifies which algorithm to use (GZip or Deflate), and the second parameter is a byte array that contains the data to compress. A MemoryStream
object will be used to store the compressed data. The compressed data stored in the MemoryStream
is then copied into another byte array and returned to the calling function. The Compress()
function is defined as follows:
static byte[] Compress(string algo, byte[] data) {