inital commit

This commit is contained in:
Paul 2020-10-18 21:57:41 +02:00
commit c7705db7e3
14 changed files with 239 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" name="RSA_KeyGenerator:jar">
<output-path>$PROJECT_DIR$</output-path>
<root id="archive" name="RSA_KeyGenerator.jar">
<element id="module-output" name="RSA_KeyGenerator" />
</root>
</artifact>
</component>

1
.idea/description.html generated Normal file
View File

@ -0,0 +1 @@
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

6
.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

9
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_14" default="true" project-jdk-name="14" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/RSA_KeyGenerator.iml" filepath="$PROJECT_DIR$/RSA_KeyGenerator.iml" />
</modules>
</component>
</project>

3
.idea/project-template.xml generated Normal file
View File

@ -0,0 +1,3 @@
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>

12
RSA_KeyGenerator.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

BIN
RSA_KeyGenerator.jar Normal file

Binary file not shown.

80
Readme.txt Normal file
View File

@ -0,0 +1,80 @@
Name: Paul Lödige
Matrikel: 15405036
Die Schlüsselgrößen, die von dem KeyPairGenerator ausgegeben werden sind sehr merkwürdig.
Ich habe bei meinem Code dadrauf geachtet. dass das Produkt aus private und public key die angefragte Größe entspricht.
Da das Programm sehr lange zum laufen braucht (.jar beigefügt) folgt eine Beispielausgabe:
0) 1024 Bit mit KeyPairGenerator: 137ms PrivKeySize: 5070 PubKeySize: 1294 ProductSize: 6364
1) 1024 Bit mit KeyPairGenerator: 79ms PrivKeySize: 5070 PubKeySize: 1294 ProductSize: 6364
2) 1024 Bit mit KeyPairGenerator: 143ms PrivKeySize: 5070 PubKeySize: 1294 ProductSize: 6364
3) 1024 Bit mit KeyPairGenerator: 68ms PrivKeySize: 5062 PubKeySize: 1294 ProductSize: 6356
4) 1024 Bit mit KeyPairGenerator: 76ms PrivKeySize: 5086 PubKeySize: 1294 ProductSize: 6380
5) 1024 Bit mit KeyPairGenerator: 111ms PrivKeySize: 5078 PubKeySize: 1294 ProductSize: 6372
6) 1024 Bit mit KeyPairGenerator: 111ms PrivKeySize: 5078 PubKeySize: 1294 ProductSize: 6372
7) 1024 Bit mit KeyPairGenerator: 37ms PrivKeySize: 5070 PubKeySize: 1294 ProductSize: 6364
8) 1024 Bit mit KeyPairGenerator: 72ms PrivKeySize: 5078 PubKeySize: 1294 ProductSize: 6372
9) 1024 Bit mit KeyPairGenerator: 51ms PrivKeySize: 5062 PubKeySize: 1294 ProductSize: 6356
Durschnittliche Laufzeit: 88.5ms
-----------------------------------------------------
0) 2048 Bit mit KeyPairGenerator: 186ms PrivKeySize: 9742 PubKeySize: 2350 ProductSize: 12092
1) 2048 Bit mit KeyPairGenerator: 40ms PrivKeySize: 9750 PubKeySize: 2350 ProductSize: 12100
2) 2048 Bit mit KeyPairGenerator: 56ms PrivKeySize: 9734 PubKeySize: 2350 ProductSize: 12084
3) 2048 Bit mit KeyPairGenerator: 96ms PrivKeySize: 9734 PubKeySize: 2350 ProductSize: 12084
4) 2048 Bit mit KeyPairGenerator: 129ms PrivKeySize: 9734 PubKeySize: 2350 ProductSize: 12084
5) 2048 Bit mit KeyPairGenerator: 126ms PrivKeySize: 9726 PubKeySize: 2350 ProductSize: 12076
6) 2048 Bit mit KeyPairGenerator: 73ms PrivKeySize: 9726 PubKeySize: 2350 ProductSize: 12076
7) 2048 Bit mit KeyPairGenerator: 72ms PrivKeySize: 9734 PubKeySize: 2350 ProductSize: 12084
8) 2048 Bit mit KeyPairGenerator: 178ms PrivKeySize: 9726 PubKeySize: 2350 ProductSize: 12076
9) 2048 Bit mit KeyPairGenerator: 150ms PrivKeySize: 9734 PubKeySize: 2350 ProductSize: 12084
Durschnittliche Laufzeit: 110.6ms
-----------------------------------------------------
0) 4096 Bit mit KeyPairGenerator: 1428ms PrivKeySize: 18982 PubKeySize: 4398 ProductSize: 23380
1) 4096 Bit mit KeyPairGenerator: 478ms PrivKeySize: 18998 PubKeySize: 4398 ProductSize: 23396
2) 4096 Bit mit KeyPairGenerator: 335ms PrivKeySize: 18990 PubKeySize: 4398 ProductSize: 23388
3) 4096 Bit mit KeyPairGenerator: 2410ms PrivKeySize: 18990 PubKeySize: 4398 ProductSize: 23388
4) 4096 Bit mit KeyPairGenerator: 2583ms PrivKeySize: 18998 PubKeySize: 4398 ProductSize: 23396
5) 4096 Bit mit KeyPairGenerator: 1101ms PrivKeySize: 18990 PubKeySize: 4398 ProductSize: 23388
6) 4096 Bit mit KeyPairGenerator: 386ms PrivKeySize: 18982 PubKeySize: 4398 ProductSize: 23380
7) 4096 Bit mit KeyPairGenerator: 346ms PrivKeySize: 18998 PubKeySize: 4398 ProductSize: 23396
8) 4096 Bit mit KeyPairGenerator: 1657ms PrivKeySize: 18982 PubKeySize: 4398 ProductSize: 23380
9) 4096 Bit mit KeyPairGenerator: 1853ms PrivKeySize: 18998 PubKeySize: 4398 ProductSize: 23396
Durschnittliche Laufzeit: 1257.7ms
-----------------------------------------------------
0) 1024 Bit mit BigInteger: 105ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
1) 1024 Bit mit BigInteger: 679ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
2) 1024 Bit mit BigInteger: 130ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
3) 1024 Bit mit BigInteger: 255ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
4) 1024 Bit mit BigInteger: 475ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
5) 1024 Bit mit BigInteger: 179ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
6) 1024 Bit mit BigInteger: 127ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
7) 1024 Bit mit BigInteger: 167ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
8) 1024 Bit mit BigInteger: 657ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
9) 1024 Bit mit BigInteger: 19ms PrivKeySize: 512 PubKeySize: 512 ProductSize: 1024
Durschnittliche Laufzeit: 279.3ms
-----------------------------------------------------
0) 2048 Bit mit BigInteger: 3200ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
1) 2048 Bit mit BigInteger: 2789ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
2) 2048 Bit mit BigInteger: 8340ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
3) 2048 Bit mit BigInteger: 2868ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
4) 2048 Bit mit BigInteger: 426ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
5) 2048 Bit mit BigInteger: 1465ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
6) 2048 Bit mit BigInteger: 2577ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
7) 2048 Bit mit BigInteger: 666ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
8) 2048 Bit mit BigInteger: 430ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
9) 2048 Bit mit BigInteger: 2356ms PrivKeySize: 1024 PubKeySize: 1024 ProductSize: 2048
Durschnittliche Laufzeit: 2511.7ms
-----------------------------------------------------
0) 4096 Bit mit BigInteger: 10017ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
1) 4096 Bit mit BigInteger: 41918ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
2) 4096 Bit mit BigInteger: 60839ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
3) 4096 Bit mit BigInteger: 50729ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
4) 4096 Bit mit BigInteger: 40834ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
5) 4096 Bit mit BigInteger: 11870ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
6) 4096 Bit mit BigInteger: 67847ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
7) 4096 Bit mit BigInteger: 6077ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
8) 4096 Bit mit BigInteger: 74448ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
9) 4096 Bit mit BigInteger: 66870ms PrivKeySize: 2048 PubKeySize: 2048 ProductSize: 4096
Durschnittliche Laufzeit: 43144.9ms
-----------------------------------------------------

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: tech.loedige.Main

3
src/META-INF/MANIFEST.MF Normal file
View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: tech.loedige.Main

View File

@ -0,0 +1,98 @@
package tech.loedige;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.function.Function;
public class Main {
public static void main(String[] args) throws Exception {
final int numberOfRuns = 10;
long start=0;
long finish = 0;
List<Long> trialsList = new ArrayList<>();
long[] trials = new long[numberOfRuns];
BigInteger[] KeyPair;
//average the RSA key generation 10 times for 1024, 2048 and 4096 bit using the KeyPairGenerator
getTimesAndAverage(()->{return generateRSAWithLibrary(1024);},10,"1024 Bit mit KeyPairGenerator");
getTimesAndAverage(()->{return generateRSAWithLibrary(2048);},10,"2048 Bit mit KeyPairGenerator");
getTimesAndAverage(()->{return generateRSAWithLibrary(4096);},10,"4096 Bit mit KeyPairGenerator");
//average the RSA key generation 10 times for 1024, 2048 and 4096 bit using the sef written function
getTimesAndAverage(()->{return generateRSAWithBigInteger(1024);},10,"1024 Bit mit BigInteger");
getTimesAndAverage(()->{return generateRSAWithBigInteger(2048);},10,"2048 Bit mit BigInteger");
getTimesAndAverage(()->{return generateRSAWithBigInteger(4096);},10,"4096 Bit mit BigInteger");
}
private static BigInteger[] generateRSAWithLibrary(int bitLength){
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(bitLength);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return new BigInteger[]{new BigInteger(keyPair.getPrivate().getEncoded()), new BigInteger(keyPair.getPublic().getEncoded())};
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
private static void getTimesAndAverage(Callable<BigInteger[]> function, int numberOfRuns, String functionName) throws Exception {
long start=0;
long finish=0;
List<Long> trialTimesList = new ArrayList<>();
long[] trialTimes = new long[numberOfRuns];
BigInteger[] KeyPair;
for(int i = 0; i<numberOfRuns; i++){
start=System.currentTimeMillis();
KeyPair = function.call();
finish=System.currentTimeMillis();
System.out.println(i+") "+functionName+":\t"+
(finish-start) + "ms" +
"\tPrivKeySize: " + KeyPair[0].bitLength() +
"\tPubKeySize: " + KeyPair[1].bitLength() +
"\tProductSize: " + (KeyPair[0].multiply(KeyPair[1]).bitLength()));
trialTimesList.add(finish-start);
}
for(int i = 0; i < numberOfRuns; i++) trialTimes[i] = trialTimesList.get(i);
System.out.println("Durschnittliche Laufzeit: " + Arrays.stream(trialTimes).average().getAsDouble() + "ms");
System.out.println("-----------------------------------------------------");
}
private static BigInteger[] generateRSAWithBigInteger(int bitLength){
SecureRandom random = new SecureRandom();
BigInteger publicKey = BigInteger.TEN;
BigInteger privateKey = BigInteger.TEN;
BigInteger product = BigInteger.ZERO;
//verify that the resulting bit length is correct
while(product.bitLength()!=bitLength){
publicKey=BigInteger.TEN;
privateKey=BigInteger.TEN;
//find a public key
while(!publicKey.isProbablePrime(50)){
byte[] bytes = new byte[bitLength/16];
random.nextBytes(bytes);
publicKey = new BigInteger(1,bytes);
}
//find a private key
while(!privateKey.isProbablePrime(50)){
byte[] bytes = new byte[bitLength/16];
random.nextBytes(bytes);
privateKey = new BigInteger(1,bytes);
}
product = privateKey.multiply(publicKey);
}
return new BigInteger[]{privateKey,publicKey};
}
}