Question

Write a program that can encrypt and decrypt using the El-GAMALCipher Please show in Java with...

Write a program that can encrypt and decrypt using the El-GAMALCipher Please show in Java with message = 100.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* ElGamalEncryptDecrypt.java */


package dateoperation;

import java.math.*;
import java.util.*;
import java.security.*;
import java.io.*;

public class ElGamalEncryptDecrypt
{
    public static void main(String[] args) throws IOException
    {
        BigInteger secretKey,p, b, c;
        Random random = new SecureRandom();
        secretKey = new BigInteger("12345678901234567890");
        ///getting public key
        System.out.println("Your secretKey = " + secretKey);
        p = BigInteger.probablePrime(64, random);
        b = new BigInteger("3");
        c = b.modPow(secretKey, p);
        System.out.println("p = " + p);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        //
        // Encryption
        // 100 as plain text
      
        String plaintext ="100";
        BigInteger X = new BigInteger(plaintext);
        BigInteger r = new BigInteger(64, random);
        BigInteger Encryptedmessage = X.multiply(c.modPow(r, p)).mod(p);
        BigInteger brmodp = b.modPow(r, p);
        System.out.println("Plaintext = " + X);
        System.out.println("r = " + r);
        System.out.println("Encrypted Message = " + Encryptedmessage);
        System.out.println("b^r mod p = " + brmodp);
        //
        // Decryption
        //
        BigInteger crmodp = brmodp.modPow(secretKey, p);
        BigInteger d = crmodp.modInverse(p);
        BigInteger DecryptedMessage = d.multiply(Encryptedmessage).mod(p);
        System.out.println("\n\nc^r mod p = " + crmodp);
        System.out.println("d = " + d);
        System.out.println("Decrypted Message: " + DecryptedMessage);

    }
}

/*

output

run:
Your secretKey = 12345678901234567890
p = 15529472551224172823
b = 3
c = 15214339181978182378
Plaintext = 100
r = 1766218395270747804
Encrypted Message = 8737472744694909418
b^r mod p = 9030210705495725744


c^r mod p = 5367395394863167854
d = 13554491082901739413
Decrypted Message: 100
BUILD SUCCESSFUL (total time: 0 seconds)

*/

Add a comment
Know the answer?
Add Answer to:
Write a program that can encrypt and decrypt using the El-GAMALCipher Please show in Java with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT