RSACryptoServiceProvider.Encrypt Method
Assembly: mscorlib (in mscorlib.dll)
public byte[] Encrypt ( byte[] rgb, boolean fOAEP )
public function Encrypt ( rgb : byte[], fOAEP : boolean ) : byte[]
Parameters
- rgb
The data to be encrypted.
- fOAEP
true to perform direct RSA encryption using OAEP padding (only available on a computer running Microsoft Windows XP or later); otherwise, false to use PKCS#1 v1.5 padding.
Return Value
The encrypted data.The following table describes the padding supported by different versions of Microsoft Windows and the maximum length of rgb allowed by the different combinations of operating systems and padding.
| Padding | Operating System Supported | Maximum Length of rgb Parameter |
|---|---|---|
| OAEP padding (PKCS#1 v2) | Microsoft Windows XP or later. | Modulus size -2 -2*hLen, where hLen is the size of the hash. |
| Direct Encryption (PKCS#1 v1.5) | Microsoft Windows 2000 or later with the high encryption pack installed. | Modulus size - 11. (11 bytes is the minimum padding possible.) |
| Direct Encryption and OAEP padding not supported | Microsoft Windows 98, Microsoft Windows Millennium, or Windows 2000 or later without the high encryption pack installed. | The maximum size allowed for a symmetric key. |
Use Decrypt to decrypt the results of this method.
The following code example initializes an RSACryptoServiceProvider object to the value of a public key (sent by another party), generates a session key using the RijndaelManaged algorithm, and then encrypts the session key using the RSACryptoServiceProvider object. Using this scheme, the session key could be sent back to the owner of the private RSA key and the two parties could use the session key to exchange encrypted data.
using System; using System.Security.Cryptography; class RSACSPSample { static void Main() { try { //initialze the byte arrays to the public key information. byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56, 74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222, 207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175, 108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194, 240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139, 168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171, 38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93, 106,99,179,68,175,211,164,116,64,148,226,254,172,147}; byte[] Exponent = {1,0,1}; //Values to store encrypted symmetric keys. byte[] EncryptedSymmetricKey; byte[] EncryptedSymmetricIV; //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //Create a new instance of RSAParameters. RSAParameters RSAKeyInfo = new RSAParameters(); //Set RSAKeyInfo to the public key values. RSAKeyInfo.Modulus = PublicKey; RSAKeyInfo.Exponent = Exponent; //Import key parameters into RSA. RSA.ImportParameters(RSAKeyInfo); //Create a new instance of the RijndaelManaged class. RijndaelManaged RM = new RijndaelManaged(); //Encrypt the symmetric key and IV. EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false); EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false); Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider."); } //Catch and display a CryptographicException //to the console. catch(CryptographicException e) { Console.WriteLine(e.Message); } } }
import System.*;
import System.Security.Cryptography.*;
class RSACSPSample
{
public static void main(String[] args)
{
try {
// initialze the byte arrays to the public key information.
ubyte publicKey[] = {214, 46, 220, 83, 160, 73, 40, 39, 201,
155, 19, 202, 3, 11, 191, 178, 56, 74, 90, 36, 248,
103, 18, 144, 170, 163, 145, 87, 54, 61, 34, 220,
222, 207, 137, 149, 173, 14, 92, 120, 206, 222,
158, 28, 40, 24, 30, 16, 175, 108, 128, 35,
230, 118, 40, 121, 113, 125, 216, 130, 11,
24, 90, 48, 194, 240, 105, 44, 76, 34, 57,
249, 228, 125, 80, 38, 9, 136, 29, 117,
207, 139, 168, 181, 85, 137, 126, 10, 126, 242, 120,
247, 121, 8, 100, 12, 201, 171, 38, 226, 193, 180,
190, 117, 177, 87, 143, 242, 213, 11, 44, 180, 113,
93, 106, 99, 179, 68, 175, 211, 164, 116, 64, 148,
226, 254, 172, 147};
ubyte exponent[] = {1, 0, 1};
// Values to store encrypted symmetric keys.
ubyte encryptedSymmetricKey[];
ubyte encryptedSymmetricIV[];
// Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
// Create a new instance of RSAParameters.
RSAParameters rsaKeyInfo = new RSAParameters();
// Set rsaKeyInfo to the public key values.
rsaKeyInfo.Modulus = publicKey;
rsaKeyInfo.Exponent = exponent;
// Import key parameters into rsa.
rsa.ImportParameters(rsaKeyInfo);
// Create a new instance of the RijndaelManaged class.
RijndaelManaged rm = new RijndaelManaged();
// Encrypt the symmetric key and IV.
encryptedSymmetricKey = rsa.Encrypt(rm.get_Key(), false);
encryptedSymmetricIV = rsa.Encrypt(rm.get_IV(), false);
Console.WriteLine("RijndaelManaged Key and IV have been "
+ "encrypted with RSACryptoServiceProvider.");
}
// Catch and display a CryptographicException
// to the console.
catch (CryptographicException e) {
Console.WriteLine(e.get_Message());
}
} //main
} //RSACSPSample
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.