Last active
December 9, 2019 18:23
-
-
Save sidsbrmnn/c222e31d93d4072fabd8ce24ed444055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
public class Rsa { | |
public static void main(String[] args) { | |
BigInteger publicKey = new BigInteger(args[0]); | |
BigInteger secretKey = new BigInteger(args[1]); | |
BigInteger n = new BigInteger(args[2]); | |
BigInteger m = new BigInteger(args[3]); | |
BigInteger e = m.modPow(publicKey, n); | |
System.out.println("Encrypted message: " + e); | |
BigInteger d = e.modPow(secretKey, n); | |
System.out.println("Decrypted message: " + d); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.util.Random; | |
public class RsaKeygen { | |
public static void main(String[] args) { | |
Random r1 = new Random(System.currentTimeMillis()); | |
Random r2 = new Random(System.currentTimeMillis() * 10); | |
int e = Integer.parseInt(args[0]); | |
BigInteger p = BigInteger.probablePrime(32, r1); | |
BigInteger q = BigInteger.probablePrime(32, r2); | |
BigInteger n = p.multiply(q); | |
BigInteger p1 = p.subtract(BigInteger.ONE); | |
BigInteger q1 = q.subtract(BigInteger.ONE); | |
BigInteger phi = p1.multiply(q1); | |
while (true) { | |
BigInteger gcd = phi.gcd(new BigInteger("" + e)); | |
if (gcd.equals(BigInteger.ONE)) break; | |
e++; | |
} | |
BigInteger publicKey = new BigInteger("" + e); | |
BigInteger secretKey = publicKey.modInverse(phi); | |
System.out.println("Public key: " + publicKey + ", " + n); | |
System.out.println("Secret key: " + secretKey + ", " + n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment