Translate

Wednesday 23 January 2013

Public Key Encryption- RSA Cryptographic Algorithm Implemented in C#

RSA Cryptographic Algorithm:

As per wiki-

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 the algorithm in 1977. Clifford Cocks, an English mathematician, had developed an equivalent system in 1973, but it was 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.

See:wiki-RSA_(algorithm) for details

I have implemented it in C#, during my college times, and took help from net for a piece of code.  The COde is Below:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RSA
{
    class Program
    {
        static void Main(string[] args)
        {
            long plaintext=0,ciphertext=0;
            long plaintext1 = 0, ciphertext1 = 0;
            Random rnd = new Random();
       long p = rnd.Next(1, 50);
            long q
        check:  q = rnd.Next(1, 70);
            if (p == q)
                goto check;

            long gcd = gcd_check(p, q);

            p = p / gcd;
            q = q / gcd;

            long n = p * q;
            long fi = (p - 1)*(q - 1);
            long e, gd;
            do
            {
                e = rnd.Next(Convert.ToInt32(fi));
                gd = gcd_check(fi, e);
            }
            while (gd != 1);

            long d = modinv(e, fi);
            long[] pub = new long[2];
            long[] pri = new long[2];
            pub[0] = e; pub[1] = n;
            pri[0] = d; pri[1] = n;           
            char c = 'y';
            while (c == 'y')
            {
                Console.WriteLine("Press E to encrypt and D to decrypt:");
                string choice = Console.ReadLine();
                switch (choice.ToUpper())
                {
                    case "E": Console.Write("enter Plain Text:(Numeric)< ");
                      Console.WriteLine(n);
                      plaintext = Convert.ToInt64(Console.ReadLine());
                     ciphertext =Convert.ToInt64(Math.Pow(Convert.ToDouble(plaintext), Convert.ToDouble(e)) % n);
                       Console.WriteLine("Ciphertext=");
                       Console.WriteLine(ciphertext);
                        break;
                    case "D": Console.WriteLine("enter Cipher Text:(Numeric)");
                        ciphertext1 = Convert.ToInt64(Console.ReadLine());
                        plaintext1 =Convert.ToInt64(Math.Pow(Convert.ToDouble(ciphertext1), Convert.ToDouble(d)) % n);
                        Console.Write("plaintext=");
                        Console.WriteLine(plaintext1);
                        break;
                    default: Console.WriteLine("wrong entry..!!");
                        break;


                }
                Console.WriteLine("press y to continue..:");
                c =Convert.ToChar( Console.ReadLine());
            }
        }
        static long gcd_check(long a, long b)
        {
            long c;
            if (a < b)
            {
                c = a;
                a = b;
                b = c;
            }
            while (true)
            {
                c = a % b;
                if (c == 0)
                    return b;
                a = b;
                b = c;
            }
        }

       static long modinv( long u,  long v)
          {
        long inv, u1, u3, v1, v3, t1, t3, q;
        long iter;
        /* Step X1. Initialise */
        u1 = 1;
        u3 = u;
        v1 = 0;
        v3 = v;
        /* Remember odd/even iterations */
        iter = 1;
        /* Step X2. Loop while v3 != 0 */
        while (v3 != 0)
        {
            /* Step X3. Divide and "Subtract" */
            q = u3 / v3;
            t3 = u3 % v3;
            t1 = u1 + q * v1;
            /* Swap */
            u1 = v1; v1 = t1; u3 = v3; v3 = t3;
            iter = -iter;
         }
                /* Make sure u3 = gcd(u,v) == 1 */
                if (u3 != 1)
                    return 0;   /* Error: No inverse exists */
                /* Ensure a positive result */
                if (iter < 0)
                    inv = v - u1;
                else
                    inv = u1;
                return inv;
           }  /// end modinv

       
       
    }
}
----------------------------------------------------


No comments:

Post a Comment