Click here to Skip to main content
15,888,802 members
Home / Discussions / C#
   

C#

 
GeneralRe: Reflection Example Pin
eggie514-Aug-07 17:56
eggie514-Aug-07 17:56 
QuestionToolStripSplit Button and related problems Pin
logicon7-Aug-07 10:47
logicon7-Aug-07 10:47 
AnswerRe: C# GradientBrushes Problem (VS2005) Pin
Judah Gabriel Himango7-Aug-07 13:09
sponsorJudah Gabriel Himango7-Aug-07 13:09 
Questioncryptography Pin
M.V7-Aug-07 8:28
M.V7-Aug-07 8:28 
AnswerRe: cryptography Pin
Guffa7-Aug-07 9:09
Guffa7-Aug-07 9:09 
GeneralRe: cryptography Pin
M.V7-Aug-07 10:22
M.V7-Aug-07 10:22 
AnswerRe: cryptography Pin
Guffa7-Aug-07 12:33
Guffa7-Aug-07 12:33 
AnswerRe: cryptography Pin
Farhan Noor Qureshi7-Aug-07 11:46
Farhan Noor Qureshi7-Aug-07 11:46 
MSDN has several good samples. Here is one.

using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{

	static void Main()
	{
		try
		{
			//Create a UnicodeEncoder to convert between byte array and string.
			UnicodeEncoding ByteConverter = new UnicodeEncoding();

			//Create byte arrays to hold original, encrypted, and decrypted data.
			byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
			byte[] encryptedData;
			byte[] decryptedData;
			
			//Create a new instance of RSACryptoServiceProvider to generate
			//public and private key data.
			RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

			//Pass the data to ENCRYPT, the public key information 
			//(using RSACryptoServiceProvider.ExportParameters(false),
			//and a boolean flag specifying no OAEP padding.
			encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false), false);

			//Pass the data to DECRYPT, the private key information 
			//(using RSACryptoServiceProvider.ExportParameters(true),
			//and a boolean flag specifying no OAEP padding.
			decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true), false);

			//Display the decrypted plaintext to the console. 
			Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
		}
		catch(ArgumentNullException)
		{
			//Catch this exception in case the encryption did
			//not succeed.
			Console.WriteLine("Encryption failed.");

		}
	}

	static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
	{
		try
		{	
			//Create a new instance of RSACryptoServiceProvider.
			RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

			//Import the RSA Key information. This only needs
			//toinclude the public key information.
			RSA.ImportParameters(RSAKeyInfo);

			//Encrypt the passed byte array and specify OAEP padding.  
			//OAEP padding is only available on Microsoft Windows XP or
			//later.  
			return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
		}
		//Catch and display a CryptographicException  
		//to the console.
		catch(CryptographicException e)
		{
			Console.WriteLine(e.Message);

			return null;
		}

	}

	static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
	{
		try
		{
			//Create a new instance of RSACryptoServiceProvider.
			RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

			//Import the RSA Key information. This needs
			//to include the private key information.
			RSA.ImportParameters(RSAKeyInfo);

			//Decrypt the passed byte array and specify OAEP padding.  
			//OAEP padding is only available on Microsoft Windows XP or
			//later.  
			return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
		}
		//Catch and display a CryptographicException  
		//to the console.
		catch(CryptographicException e)
		{
			Console.WriteLine(e.ToString());

			return null;
		}

	}
}



Farhan Noor Qureshi

Questionfindcontrol Pin
jayeshjmt7-Aug-07 7:56
jayeshjmt7-Aug-07 7:56 
AnswerRe: findcontrol Pin
Guffa7-Aug-07 9:15
Guffa7-Aug-07 9:15 
AnswerRe: findcontrol Pin
Farhan Noor Qureshi7-Aug-07 11:50
Farhan Noor Qureshi7-Aug-07 11:50 
QuestionRichTextBox ZoomFactor Property Pin
Mark F.7-Aug-07 6:35
Mark F.7-Aug-07 6:35 
AnswerRe: RichTextBox ZoomFactor Property Pin
Luc Pattyn7-Aug-07 6:46
sitebuilderLuc Pattyn7-Aug-07 6:46 
GeneralRe: RichTextBox ZoomFactor Property Pin
Mark F.7-Aug-07 6:48
Mark F.7-Aug-07 6:48 
AnswerRe: RichTextBox ZoomFactor Property Pin
Hessam Jalali7-Aug-07 6:52
Hessam Jalali7-Aug-07 6:52 
QuestionLabel text alignment Pin
RussBus7-Aug-07 6:34
RussBus7-Aug-07 6:34 
AnswerRe: Label text alignment Pin
Luc Pattyn7-Aug-07 6:39
sitebuilderLuc Pattyn7-Aug-07 6:39 
GeneralRe: Label text alignment Pin
RussBus7-Aug-07 9:38
RussBus7-Aug-07 9:38 
QuestionDjango type database access Pin
eggie57-Aug-07 5:58
eggie57-Aug-07 5:58 
AnswerRe: Django type database access Pin
originSH7-Aug-07 6:08
originSH7-Aug-07 6:08 
AnswerRe: Django type database access Pin
Judah Gabriel Himango7-Aug-07 9:11
sponsorJudah Gabriel Himango7-Aug-07 9:11 
QuestionMulti Threading / Invoke Pin
Peterixina7-Aug-07 5:47
Peterixina7-Aug-07 5:47 
AnswerRe: Multi Threading / Invoke Pin
Judah Gabriel Himango7-Aug-07 6:39
sponsorJudah Gabriel Himango7-Aug-07 6:39 
Questionwebsite code generator Pin
Richard Blythe7-Aug-07 4:20
Richard Blythe7-Aug-07 4:20 
AnswerRe: website code generator Pin
leppie7-Aug-07 5:07
leppie7-Aug-07 5:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.