Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Practical No : 04
- Aim :Write a program to implement RSA algorithm to perform encryption / decryption of a given string.
- Input ;
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- namespace ConsoleApplication6
- {
- class Program
- {
- static bool IsPrime(intiNumber)
- {
- intiCount;
- int f = 0;
- iCount = 2;
- if (iNumber< 2)
- return true;
- while (iCount<= (iNumber / 2))
- {
- if (iNumber % iCount == 0)
- {
- f = 1;
- break;
- }
- iCount++;
- }
- if (f == 0) return true;
- return false;
- }
- staticint Multiply(int iNum1, int iNum2)
- {
- return iNum1 * iNum2;
- }
- staticintfind_E(intiNum)
- {
- intiCount = 2;
- while (iCount<iNum)
- {
- if (iNum % iCount != 0)
- returniCount;
- iCount++;
- }
- return 0;
- }
- staticintFind_D(int pie, int e)
- {
- int k = 1;
- intdvalue = 0;
- bool state = true;
- while (state)
- {
- if ((k * pie + 1) % e == 0)
- {
- dvalue = (k * pie + 1) / e;
- state = false;
- }
- ++k;
- }
- returndvalue;
- }
- staticintFind_Text(int iNum1, int iNum2, int iNum3)
- {
- intiCount, t;
- iCount = 1;
- t = 1;
- while (iCount<= iNum2)
- {
- t = t * iNum1;
- t = t % iNum3;
- iCount++;
- }
- return (t % iNum3);
- }
- static void Main(String[] args)
- {
- int p, q, n, d, e, pi, pt, ct;
- //clrscr();
- Console.WriteLine("------IMPLIMENTATION OF R.S.A ALGORITHM------");
- Console.WriteLine("Enter a prime number:");
- p = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Enter another prime number:");
- q = Convert.ToInt32(Console.ReadLine());
- if (IsPrime(p) &&IsPrime(q))
- {
- n = Multiply(p, q);
- Console.WriteLine("/*Intermediate Calculation..");
- Console.WriteLine("n:" + n);
- pi = Multiply(p - 1, q - 1);
- Console.WriteLine("pi:" + pi);
- e = find_E(pi);
- Console.WriteLine("e:" + e);
- d = Find_D(pi, e);
- Console.WriteLine("d:" + d);
- Console.WriteLine("*/");
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("----KEYS---");
- Console.WriteLine("Public Key:({0},{1})", e, n);
- Console.WriteLine("Private Key:(" + d + "," + n + ")");
- Console.WriteLine();
- Console.WriteLine("----ENCRYPTION----");
- Console.WriteLine("Enter the Number to Encrypt");
- pt = Convert.ToInt32(Console.ReadLine());
- ct = Find_Text(pt, e, n);
- pt = Find_Text(ct, d, n);
- Console.WriteLine("Cipher Text:" + ct);
- Console.WriteLine("----Decryption---");
- Console.WriteLine("Plain Text:" + pt);
- }
- else
- {
- Console.WriteLine("Error! Enter two prime numbers!");
- }
- Console.Read();
- }
- }
- }
Add Comment
Please, Sign In to add comment