Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. print("Welcome to ElGamal Cryptosystems");
  2. print("Enter help() for further information");
  3.  
  4. help(c)=
  5. {
  6. print("ElGamal Cryptosystems");
  7. print("");
  8. print("Enter :");
  9. print("- setP(P) : to set the prime number P");
  10. print("- setG(g) : to set the generator g");
  11. print("- setKey(u) : to set the private key u");
  12. print("- encrypt(m) : to encrypt a message m. The output is (a, b).");
  13. print("- decrypt(a, b) : to decrypt (a, b). The output is plaintext.");
  14. print("");
  15. print("Automatic Generation");
  16. print("- genP(l) : to generate P with size l bits and then set an appropriate generator g");
  17. print("");
  18. print("Print Setting:");
  19. print("- printSetting() : to print the value of p and g");
  20.  
  21. }
  22.  
  23. setP(P)=
  24. {
  25. if(isprime(P), p = P, print("P is not a prime number") );
  26. }
  27.  
  28. setG(G)=
  29. {
  30. x = lift(Mod(G^((p-1)/2),p));
  31.  
  32. if(x != 1, g = G, print("Sorry, alpha must be a primitive element"));
  33. }
  34.  
  35. setKey(U)=
  36. {
  37. u = U;
  38. y = lift(Mod(g^u,p));
  39. }
  40.  
  41. encrypt(m)=
  42. {
  43. k = random(p-1);
  44. while(gcd(k,p-1) != 1, k = random(p-1));
  45.  
  46. a = lift(Mod(g^k, p));
  47. b = lift(Mod(y^k * m, p));
  48.  
  49. print("Ciphertext : (" a ", " b ")");
  50. }
  51.  
  52. decrypt(a,b)=
  53. {
  54. pm = lift(Mod(b * a^(p-1-u), p));
  55.  
  56. print("Plaintext : " pm);
  57. }
  58.  
  59. printSetting(ps) =
  60. {
  61.  
  62. print ("The current settings:");
  63.  
  64. print ("p = " p);
  65. print ("g = " g);
  66. print ("y = " y);
  67. }
  68.  
  69. genP(l)=
  70. {
  71. p = primes(1000)[random(1000) + 1];
  72.  
  73. while(floor(log(p)/log(2)) != l, p = primes(1000)[random(1000) + 1]);
  74.  
  75. print("p = " p);
  76.  
  77. xx = random(p-1);
  78. if(lift(Mod(xx^((p-1)/2),p)) != 1, g = xx, g = lift(Mod(-xx, p)));
  79.  
  80. print("g = " g);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement