nikkkilll

soni4

Oct 3rd, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. Practical No : 04
  2. Aim :Write a program to implement RSA algorithm to perform encryption / decryption of a given string.
  3.  
  4. Input ;
  5.  
  6. using System;
  7. usingSystem.Collections.Generic;
  8. usingSystem.Linq;
  9. usingSystem.Text;
  10.  
  11. namespace ConsoleApplication6
  12. {
  13. class Program
  14. {
  15. static bool IsPrime(intiNumber)
  16. {
  17. intiCount;
  18. int f = 0;
  19. iCount = 2;
  20. if (iNumber< 2)
  21. return true;
  22. while (iCount<= (iNumber / 2))
  23. {
  24. if (iNumber % iCount == 0)
  25. {
  26. f = 1;
  27. break;
  28. }
  29. iCount++;
  30. }
  31. if (f == 0) return true;
  32. return false;
  33. }
  34. staticint Multiply(int iNum1, int iNum2)
  35. {
  36. return iNum1 * iNum2;
  37. }
  38. staticintfind_E(intiNum)
  39. {
  40. intiCount = 2;
  41. while (iCount<iNum)
  42. {
  43. if (iNum % iCount != 0)
  44. returniCount;
  45. iCount++;
  46. }
  47. return 0;
  48. }
  49. staticintFind_D(int pie, int e)
  50. {
  51. int k = 1;
  52. intdvalue = 0;
  53. bool state = true;
  54. while (state)
  55. {
  56. if ((k * pie + 1) % e == 0)
  57. {
  58. dvalue = (k * pie + 1) / e;
  59. state = false;
  60. }
  61. ++k;
  62. }
  63. returndvalue;
  64. }
  65. staticintFind_Text(int iNum1, int iNum2, int iNum3)
  66. {
  67. intiCount, t;
  68. iCount = 1;
  69. t = 1;
  70. while (iCount<= iNum2)
  71. {
  72. t = t * iNum1;
  73. t = t % iNum3;
  74. iCount++;
  75. }
  76. return (t % iNum3);
  77. }
  78. static void Main(String[] args)
  79. {
  80. int p, q, n, d, e, pi, pt, ct;
  81. //clrscr();
  82. Console.WriteLine("------IMPLIMENTATION OF R.S.A ALGORITHM------");
  83. Console.WriteLine("Enter a prime number:");
  84. p = Convert.ToInt32(Console.ReadLine());
  85. Console.WriteLine("Enter another prime number:");
  86. q = Convert.ToInt32(Console.ReadLine());
  87. if (IsPrime(p) &&IsPrime(q))
  88. {
  89. n = Multiply(p, q);
  90. Console.WriteLine("/*Intermediate Calculation..");
  91. Console.WriteLine("n:" + n);
  92. pi = Multiply(p - 1, q - 1);
  93. Console.WriteLine("pi:" + pi);
  94. e = find_E(pi);
  95. Console.WriteLine("e:" + e);
  96. d = Find_D(pi, e);
  97. Console.WriteLine("d:" + d);
  98. Console.WriteLine("*/");
  99. Console.WriteLine();
  100. Console.WriteLine();
  101. Console.WriteLine("----KEYS---");
  102. Console.WriteLine("Public Key:({0},{1})", e, n);
  103. Console.WriteLine("Private Key:(" + d + "," + n + ")");
  104. Console.WriteLine();
  105. Console.WriteLine("----ENCRYPTION----");
  106. Console.WriteLine("Enter the Number to Encrypt");
  107. pt = Convert.ToInt32(Console.ReadLine());
  108. ct = Find_Text(pt, e, n);
  109. pt = Find_Text(ct, d, n);
  110. Console.WriteLine("Cipher Text:" + ct);
  111. Console.WriteLine("----Decryption---");
  112. Console.WriteLine("Plain Text:" + pt);
  113. }
  114. else
  115. {
  116. Console.WriteLine("Error! Enter two prime numbers!");
  117. }
  118. Console.Read();
  119. }
  120. }
  121. }
Add Comment
Please, Sign In to add comment