terça-feira, 16 de outubro de 2012

RSA (algorithm)

RSA is an algorithm for public-key cryptography that is based on the presumed difficulty of factoring large integers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described it in 1977. Clifford Cocks, an English mathematician, had developed an equivalent system in 1973, but it wasn't classified until 1997. A user of RSA creates and then publishes the product of two large prime numbers, along with an auxiliary value, as their public key. The prime factors must be kept secret. Anyone can use the public key to encrypt a message, but with currently published methods, if the public key is large enough, only someone with knowledge of the prime factors can feasibly decode the message.[1] Whether breaking RSA encryption is as hard as factoring is an open question known as the RSA problem.


The RSA algorithm involves three steps: key generation, encryption and decryption.


A working example

Here is an example of RSA encryption and decryption. The parameters used here are artificially small, but one can also use OpenSSL to generate and examine a real keypair.
  1. Choose two distinct prime numbers, such as
    p = 61 and q = 53.
  2. Compute n = p q giving
    n = 61 \times 53 = 3233.
  3. Compute the totient of the product as \phi(n) = (p-1)(q-1) giving
    \phi(3233) = (61 - 1)(53 - 1) = 3120.
  4. Choose any number 1 < e < 3120 that is coprime to 3120. Choosing a prime number for e leaves us only to check that e is not a divisor of 3120.
    Let e = 17.
  5. Compute d, the modular multiplicative inverse of e\text{ (mod }\phi(n)\text{)} yielding
    d = 2753.
The public key is (n = 3233, e = 17). For a padded plaintext message m, the encryption function is m^{17}\text{ (mod }3233\text{)}.
The private key is (n = 3233, d = 2753). For an encrypted ciphertext c, the decryption function is c^{2753}\text{ (mod }3233\text{)}.
For instance, in order to encrypt m = 65, we calculate
c = 65^{17}\text{ (mod }3233\text{)} = 2790.
To decrypt c = 2790, we calculate
m = 2790^{2753}\text{ (mod }3233\text{)} = 65.

Source: http://en.wikipedia.org/wiki/RSA_(algorithm) access in 16-october 2012


public class RSACSPSample

{


public 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.


using (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

{


byte[] encryptedData;


//Create a new instance of RSACryptoServiceProvider.


using (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.

encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);

}


return encryptedData;

}


//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

{


byte[] decryptedData;


//Create a new instance of RSACryptoServiceProvider.


using (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.

decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);

}


return decryptedData;

}


//Catch and display a CryptographicException


//to the console.


catch (CryptographicException e)

{


Console.WriteLine(e.ToString());


return null;

}

}

}


C# Reference: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx


Postar um comentário