Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-schwarz committed Jun 3, 2024
2 parents 8831546 + f271243 commit e636ba1
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/main/java/rsa_encryption/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

import java.math.BigInteger;

/**
* The {@code Key} interface provides methods for generating and retrieving keys.
*
*/
public interface Key {

/**
* Generates the key.
*/
public void generateKey();
/**
* Retrieves the generated key.
*/
public BigInteger getKey();

/**
* Retrieves the generator number associated with the key.
*/
public BigInteger getGeneratorNumber();


Expand Down
42 changes: 40 additions & 2 deletions src/main/java/rsa_encryption/KeyGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,63 @@

import java.math.BigInteger;

/**
* The {@code KeyGenerator} class is responsible for generating RSA key pairs.
*/
public class KeyGenerator {
private KeyPair keys;

/**
* Constructs a {@code KeyGenerator} with specified prime numbers {@code p} and {@code q}.
*
* @param p the first prime number.
* @param q the second prime number.
*/
public KeyGenerator(BigInteger p, BigInteger q) {
keys = new KeyPair(p, q);
if (p.isProbablePrime(100) && q.isProbablePrime(100)) {
keys = new KeyPair(p, q);
} else {
throw new IllegalArgumentException("Keine Primzahl!");
}
}

/**
* Constructs a {@code KeyGenerator} with specified prime numbers {@code p} and {@code q}.
*
* @param p the first prime number.
* @param q the second prime number.
*/
public KeyGenerator(int p, int q) {
keys = new KeyPair(BigInteger.valueOf(p), BigInteger.valueOf(q));
if (BigInteger.valueOf(p).isProbablePrime(100) && BigInteger.valueOf(q).isProbablePrime(100)) {
keys = new KeyPair(BigInteger.valueOf(p), BigInteger.valueOf(q));
} else {
throw new IllegalArgumentException("Keine Primzahl!");
}

}

/**
* Generates the RSA public and private keys.
*/
public void generateKeys() {
keys.generatePublicKey();
keys.generatePrivateKey();
}

/**
* Retrieves the generated key pair.
*
* @return the key pair as a {@code KeyPair}.
*/
public KeyPair getKeyPair() {
return keys;
}

/**
* Returns a string representation of the key pair.
*
* @return a string representation of the public and private keys.
*/
public String toString() {
String g = keys.getPublicKey().getGeneratorNumber().toString();
String e = keys.getPublicKey().getKey().toString();
Expand Down
37 changes: 31 additions & 6 deletions src/main/java/rsa_encryption/KeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,52 @@

import java.math.BigInteger;

/**
* The {@code KeyPair} class represents a pair of RSA keys, consisting of a public key and a private key.
*/
public class KeyPair {
private PublicKey publicKey;
private PrivateKey privateKey;

/**
* Constructs a {@code KeyPair} with specified prime numbers {@code p} and {@code q}.
*
* @param p the first prime number.
* @param q the second prime number.
*/
public KeyPair(BigInteger p, BigInteger q){
publicKey = new PublicKey(p,q);
privateKey = new PrivateKey(p,q);
if (p.isProbablePrime(100) && q.isProbablePrime(100)) {
publicKey = new PublicKey(p, q);
privateKey = new PrivateKey(p, q);
} else {
throw new IllegalArgumentException("Keine Primzahl");
}
}

/**
* Generates the private key.
*/
public void generatePrivateKey() {
privateKey.generateKey();
}

/**
* Generates the public key.
*/
public void generatePublicKey() {
publicKey.generateKey();
}

/**
* Retrieves the public key.
*
* @return the public key as a {@code PublicKey}.
*/
public PublicKey getPublicKey(){
return publicKey;
}

/**
* Retrieves the private key.
*
* @return the private key as a {@code PrivateKey}.
*/
public PrivateKey getPrivateKey(){
return privateKey;
}
Expand Down
35 changes: 31 additions & 4 deletions src/main/java/rsa_encryption/PrivateKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.math.BigInteger;

/**
* The {@code PrivateKey} class implements the {@code Key} interface and represents
* an RSA private key.
*/
public class PrivateKey implements Key{

private BigInteger p;
Expand All @@ -10,18 +14,32 @@ public class PrivateKey implements Key{
private BigInteger generatorKey;


/**
* Constructs a {@code PrivateKey} with specified prime numbers {@code p} and {@code q}.
*
* @param p the first prime number.
* @param q the second prime number.
*/
public PrivateKey(
BigInteger p,
BigInteger q) {
this.p = p;
this.q = q;
generatorKey = this.p.multiply(this.q);
if (p.isProbablePrime(100) && q.isProbablePrime(100)) {
this.p = p;
this.q = q;
generatorKey = this.p.multiply(this.q);
} else {
throw new IllegalArgumentException("Keine Primzahl!");
}
}

private BigInteger ggt(BigInteger a, BigInteger b) {
if (b.signum() == 0) return a;
return ggt(b,a.mod(b));
}

/**
* Generates the RSA private key.
*/
public void generateKey() {
BigInteger f = (p.subtract(BigInteger.valueOf(1))).multiply(q.subtract(BigInteger.valueOf(1)));

Expand All @@ -37,11 +55,20 @@ public void generateKey() {
this.keyNumber = d;
}


/**
* Retrieves the generator number associated with the key.
*
* @return the generator number as a {@code BigInteger}.
*/
public BigInteger getGeneratorNumber() {
return generatorKey;
}

/**
* Retrieves the generated cryptographic key.
*
* @return the cryptographic key as a {@code BigInteger}.
*/
@Override
public BigInteger getKey() {
return keyNumber;
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/rsa_encryption/PublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import java.math.BigInteger;

/**
* The {@code PublicKey} class implements the {@code Key} interface and represents
* an RSA public key.
*
* @version 1.0
* @since 2023
*/
public class PublicKey implements Key{

private BigInteger p;
Expand All @@ -10,18 +17,32 @@ public class PublicKey implements Key{
private BigInteger generatorKey;


/**
* Constructs a {@code PublicKey} with specified prime numbers {@code p} and {@code q}.
*
* @param p the first prime number.
* @param q the second prime number.
*/
public PublicKey(
BigInteger p,
BigInteger q) {
this.p = p;
this.q = q;
generatorKey = this.p.multiply(this.q);
if (p.isProbablePrime(100) && q.isProbablePrime(100)) {
this.p = p;
this.q = q;
generatorKey = this.p.multiply(this.q);
} else {
throw new IllegalArgumentException("Keine Primzahl!");
}
}

private BigInteger ggt(BigInteger a, BigInteger b) {
if (b.signum() == 0) return a;
return ggt(b,a.mod(b));
}

/**
* Generates the RSA public key.
*/
public void generateKey() {
BigInteger f = (p.subtract(BigInteger.valueOf(1))).multiply(q.subtract(BigInteger.valueOf(1)));

Expand All @@ -33,11 +54,20 @@ public void generateKey() {
this.keyNumber = e;
}

/**
* Retrieves the generator number associated with the key.
*
* @return the generator number as a {@code BigInteger}.
*/
public BigInteger getGeneratorNumber() {
return generatorKey;
}

@Override
/**
* Retrieves the generated cryptographic key.
*
* @return the cryptographic key as a {@code BigInteger}.
*/
public BigInteger getKey() {
return keyNumber;
}
Expand Down

0 comments on commit e636ba1

Please sign in to comment.