Last active
November 22, 2020 12:03
-
-
Save cosminpopescu14/cecd9a5ed76ebc6b5512e434b4831128 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
package com.company; | |
import java.math.BigInteger; | |
import java.security.SecureRandom; | |
public class RSA { | |
private final static BigInteger one = new BigInteger("1"); | |
private final static SecureRandom random = new SecureRandom(); | |
private BigInteger privateKey; | |
private BigInteger publicKey; | |
private BigInteger modulus; | |
// generate an N-bit (roughly) public and private key | |
RSA(int N) { | |
BigInteger p = BigInteger.probablePrime(N/2, random); | |
BigInteger q = BigInteger.probablePrime(N/2, random); | |
BigInteger phi = (p.subtract(one)).multiply(q.subtract(one)); | |
modulus = p.multiply(q); | |
publicKey = new BigInteger("65537"); // common value in practice = 2^16 + 1 | |
privateKey = publicKey.modInverse(phi); | |
} | |
BigInteger encrypt(BigInteger message) { | |
return message.modPow(publicKey, modulus); | |
} | |
BigInteger decrypt(BigInteger encrypted) { | |
return encrypted.modPow(privateKey, modulus); | |
} | |
public String toString() { | |
String s = ""; | |
s += "public = " + publicKey + "\n"; | |
s += "private = " + privateKey + "\n"; | |
s += "modulus = " + modulus; | |
return s; | |
} | |
public static void main(String[] args) { | |
int N = Integer.parseInt(args[0]); | |
RSA key = new RSA(N); | |
System.out.println(key); | |
// create random message, encrypt and decrypt | |
// BigInteger message = new BigInteger(N-1, random); | |
// create message by converting string to integer | |
String s = "test"; | |
byte[] bytes = s.getBytes(); | |
BigInteger message = new BigInteger(bytes); | |
BigInteger encrypt = key.encrypt(message); | |
BigInteger decrypt = key.decrypt(encrypt); | |
System.out.println("message = " + message); | |
System.out.println("encrypted = " + encrypt); | |
System.out.println("decrypted = " + decrypt); | |
} | |
} |
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
package com.company; | |
import java.io.DataInputStream; | |
import java.io.IOException; | |
import java.math.BigInteger; | |
import java.util.Random; | |
public class RSA | |
{ | |
private BigInteger p; | |
private BigInteger q; | |
private BigInteger N; | |
private BigInteger phi; | |
private BigInteger e; | |
private BigInteger d; | |
private int bitlength = 1024; | |
private Random r; | |
public RSA() | |
{ | |
r = new Random(); | |
p = BigInteger.probablePrime(bitlength, r); | |
q = BigInteger.probablePrime(bitlength, r); | |
N = p.multiply(q); | |
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); | |
e = BigInteger.probablePrime(bitlength / 2, r); | |
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0) | |
{ | |
e.add(BigInteger.ONE); | |
} | |
d = e.modInverse(phi); | |
} | |
public RSA(BigInteger e, BigInteger d, BigInteger N) | |
{ | |
this.e = e; | |
this.d = d; | |
this.N = N; | |
} | |
@SuppressWarnings("deprecation") | |
public static void main(String[] args) throws IOException, IOException { | |
RSA rsa = new RSA(); | |
DataInputStream in = new DataInputStream(System.in); | |
String teststring; | |
System.out.println("Enter the plain text:"); | |
teststring = in.readLine(); | |
System.out.println("Encrypting String: " + teststring); | |
System.out.println("String in Bytes: " | |
+ bytesToString(teststring.getBytes())); | |
// encrypt | |
byte[] encrypted = rsa.encrypt(teststring.getBytes()); | |
// decrypt | |
byte[] decrypted = rsa.decrypt(encrypted); | |
System.out.println("Decrypting Bytes: " + bytesToString(decrypted)); | |
System.out.println("Decrypted String: " + new String(decrypted)); | |
} | |
private static String bytesToString(byte[] encrypted) | |
{ | |
String test = ""; | |
for (byte b : encrypted) | |
{ | |
test += Byte.toString(b); | |
} | |
return test; | |
} | |
// Encrypt message | |
public byte[] encrypt(byte[] message) | |
{ | |
return (new BigInteger(message)).modPow(e, N).toByteArray(); | |
} | |
// Decrypt message | |
public byte[] decrypt(byte[] message) | |
{ | |
return (new BigInteger(message)).modPow(d, N).toByteArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment