Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Welcome to ElGamal Cryptosystems");
- print("Enter help() for further information");
- help(c)=
- {
- print("ElGamal Cryptosystems");
- print("");
- print("Enter :");
- print("- setP(P) : to set the prime number P");
- print("- setG(g) : to set the generator g");
- print("- setKey(u) : to set the private key u");
- print("- encrypt(m) : to encrypt a message m. The output is (a, b).");
- print("- decrypt(a, b) : to decrypt (a, b). The output is plaintext.");
- print("");
- print("Automatic Generation");
- print("- genP(l) : to generate P with size l bits and then set an appropriate generator g");
- print("");
- print("Print Setting:");
- print("- printSetting() : to print the value of p and g");
- }
- setP(P)=
- {
- if(isprime(P), p = P, print("P is not a prime number") );
- }
- setG(G)=
- {
- x = lift(Mod(G^((p-1)/2),p));
- if(x != 1, g = G, print("Sorry, alpha must be a primitive element"));
- }
- setKey(U)=
- {
- u = U;
- y = lift(Mod(g^u,p));
- }
- encrypt(m)=
- {
- k = random(p-1);
- while(gcd(k,p-1) != 1, k = random(p-1));
- a = lift(Mod(g^k, p));
- b = lift(Mod(y^k * m, p));
- print("Ciphertext : (" a ", " b ")");
- }
- decrypt(a,b)=
- {
- pm = lift(Mod(b * a^(p-1-u), p));
- print("Plaintext : " pm);
- }
- printSetting(ps) =
- {
- print ("The current settings:");
- print ("p = " p);
- print ("g = " g);
- print ("y = " y);
- }
- genP(l)=
- {
- p = primes(1000)[random(1000) + 1];
- while(floor(log(p)/log(2)) != l, p = primes(1000)[random(1000) + 1]);
- print("p = " p);
- xx = random(p-1);
- if(lift(Mod(xx^((p-1)/2),p)) != 1, g = xx, g = lift(Mod(-xx, p)));
- print("g = " g);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement