jeudi 17 octobre 2013

mercredi 16 octobre 2013

lundi 14 octobre 2013

C# / Console / Cryptage et décryptage RSA

Thanks to the following article writer http://www.codestructs.com/tag-blogger-com-1999-blog-2555797619867133640-post-8272894101105901249 from which I took the code to paste it in a console C# application under SharpDevelop 4.3.3.


/*
 * Created by SharpDevelop.
 * User: 0r4cl3D1gg4
 * Date: 14/10/2013
 * Time: 20:31
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Security.Cryptography;
using System.Text;

namespace CSharpRsaTest
{
    class Program
    {
        public static void Main(string[] args)
        {
            RSAParameters PublicKeyInfo;
            RSAParameters PrivateKeyInfo;
            byte[] EncryptedByte;
            byte[] DecryptedByte;

            /* Initialize RSA and Get Private and Public KeyInfo in paramters */
            RSACryptoServiceProvider Rsa = new RSACryptoServiceProvider();
            Rsa.KeySize = 1024;
          
            /* Generating Info that Contain Public Key Info and Private Key Info*/
            PublicKeyInfo = Rsa.ExportParameters(false);
            PrivateKeyInfo = Rsa.ExportParameters(true);
          
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            /* Encrypt Data using Public Key Info */
            rsa.ImportParameters(PublicKeyInfo);
            UnicodeEncoding ue = new UnicodeEncoding();
          
            String strStringToEncrypt="This blog is cool";
            Console.WriteLine("String to encrypt = " + strStringToEncrypt);
            Console.WriteLine("Length of string to encrypt = " + strStringToEncrypt.Length.ToString());
          
            byte[] DataToEncrypt = ue.GetBytes(strStringToEncrypt);
            EncryptedByte = rsa.Encrypt(DataToEncrypt, false);

            /* Show Encrypted Data into Text Box */
            String strEncryptedByte = ue.GetString(EncryptedByte);
          
            Console.WriteLine("Length of encrypted byte array = " + EncryptedByte.GetLength(0));
          
            for (int i=0;i<EncryptedByte.GetLength(0);i++){
                Console.Write(EncryptedByte[i].ToString());
                if (i<EncryptedByte.GetLength(0)-1){
                    Console.Write(":");
                }
            }
          
            Console.WriteLine("");
            Console.WriteLine("Content of encrypted byte array = ");
            Console.WriteLine(strEncryptedByte);
          
            rsa.Dispose();

            Console.WriteLine("");
            Console.WriteLine("Decrypting");
          
            rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(PrivateKeyInfo);

            ue = new UnicodeEncoding();

            /* Decrypt Data and Show in Same TextBox */
            DecryptedByte  = rsa.Decrypt(EncryptedByte, false);

            /* Show Decrypted Data into TextBox */
            Console.WriteLine("Decrypted text = " + ue.GetString(DecryptedByte));
            rsa.Dispose();
          
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}