Compare commits
11 Commits
84a9c4e576
...
sheet02-su
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3aa96fa84 | ||
|
|
9c2b167d3a | ||
|
|
4784a15866 | ||
|
|
76d4003957 | ||
|
|
3b9e9034ba | ||
|
|
a2363e3f51 | ||
|
|
4464bf55a9 | ||
|
|
7da7d4e51d | ||
|
|
6051b5f720 | ||
|
|
b8d311adf5 | ||
|
|
1eeefff2c3 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
*.pdf
|
*.pdf
|
||||||
|
sheet01/a2/Hash.java
|
||||||
|
*.class
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
## java.util.Random
|
||||||
|
|
||||||
|
a) Numbers are generated using a linear congruential generator. They are pseudorandom and the algorithm accepts a 48-bit seed.
|
||||||
|
|
||||||
|
b) `java.util.Random` produces uniformly distributed pseudorandom numbers.
|
||||||
|
|
||||||
|
c) No, in the documentation `SecureRandom` is recommended as a good alternative for cryptographic use-cases.
|
||||||
|
|
||||||
|
## java.lang.Math.random()
|
||||||
|
|
||||||
|
java.lang.Math.random() is a wrapper for java.util.Random (therefore same as above).
|
||||||
|
|
||||||
|
## java.security.SecureRandom
|
||||||
|
|
||||||
|
a) A unpredictable source of randomness should be used, but the algorithm in use can vary since `SecureRandom.getInstance("algorithm")` exists. Multiple ones are available.
|
||||||
|
|
||||||
|
b) The distribution of numbers is completely random and unpredictable.
|
||||||
|
|
||||||
|
c) Yes, hence the name, `SecureRandom`.
|
||||||
|
|
||||||
|
## /dev/random
|
||||||
|
|
||||||
|
a) The random number generator gathers environmental noise from device drivers and other sources into an entropy pool. It keeps an estimate of the number of bits of noise in the entropy pool. From this entropy pool, random numbers are created.
|
||||||
|
|
||||||
|
b) The distribution of numbers is completely random and unpredictable.
|
||||||
|
|
||||||
|
c) Yes, all use cases are fine.
|
||||||
|
|
||||||
|
## /dev/urandom
|
||||||
|
|
||||||
|
Preferred to random except for applications that require randomness at early boot time since it is not available there. The rest is the same as for `dev/random`.
|
||||||
1
sheet01/a2/b-sha2.txt
Normal file
1
sheet01/a2/b-sha2.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SHA256 (plaintext.txt) = 95f3afed37754b0aaec0c57bea62389621f6ce2a299f64b849acd43651e3668c
|
||||||
1
sheet01/a2/b-sha3.txt
Normal file
1
sheet01/a2/b-sha3.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SHA3-256 (plaintext.txt) = 8cdcfd9d0414adbba1bb146c214e181d7e5d7c8edde90b79dcb27df005fcc298
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
cksum -a sha2 --length 256 plaintext.txt > b-sha2.txt
|
||||||
|
reads the plaintext file and calculates a unique hash string using the SHA2-256 cryptographic algorithm and writes it into b-sha2.txt
|
||||||
|
cksum -a sha3 --length 256 plaintext.txt > b-sha3.txt
|
||||||
|
reads the plaintext file and calculates a unique hash string using the SHA3-256 cryptographic algorithm and writes it into b-sha3.txt
|
||||||
1
sheet01/a2/c-hash.txt
Normal file
1
sheet01/a2/c-hash.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
03087115
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
javac Hash.java (Compiles the java class)
|
||||||
|
java Hash plaintext.txt (runs the custom Hash function on the text file)
|
||||||
|
Output: 03087115
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
The custom hash is adding the unicode values of each character, the amount of rows and the amount of characters each multiplied with a hex value.
|
||||||
|
This leads to it giving the same output when switching around rows, or even putting all characters into one line and adding the required amount of empty lines behind that.
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
LJVeuiolknitaaornrdia
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
is producing the same hash as
|
||||||
|
|
||||||
|
"""
|
||||||
|
Leonard
|
||||||
|
Viktoria
|
||||||
|
Julian
|
||||||
|
"""
|
||||||
|
|
||||||
|
and the same as
|
||||||
|
|
||||||
|
"""
|
||||||
|
Julian
|
||||||
|
Leonard
|
||||||
|
Viktoria
|
||||||
|
"""
|
||||||
|
|
||||||
|
It has a good enough "first pre-image resistance" since it is not really possible to find the correct characters in the same order from the hash value that give the same original file
|
||||||
|
The "second pre-image resistance" is weak since you can easily calculate a different file which gives the same output.
|
||||||
|
It has no collision resistance since you can just switch around characters and get the same hash.
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Julian
|
||||||
|
Leonard
|
||||||
|
Viktoria
|
||||||
2
sheet02/a1/b-aeskey.txt
Normal file
2
sheet02/a1/b-aeskey.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
AES Key: cddf0c7f664624fd0b604c622a2c670dffc3fdd7859a7cdd2377d1ba2ab86f89
|
||||||
|
IV: 30579741743af76bdd06ec45b12ee6d7
|
||||||
3
sheet02/a1/b-dec-aes.txt
Normal file
3
sheet02/a1/b-dec-aes.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Viktoria
|
||||||
|
Leo
|
||||||
|
Julian
|
||||||
1
sheet02/a1/b-enc-aes.txt
Normal file
1
sheet02/a1/b-enc-aes.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
o5Ö¦Íß*òþk‚¼¾I¦BîÜr<C39C>Œ´<C592>6@hün5Ø
|
||||||
5
sheet02/a1/b.txt
Normal file
5
sheet02/a1/b.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
Generate 256bit aes key and 128 bit iv:
|
||||||
|
openssl rand -hex 32
|
||||||
|
openssl rand -hex 16
|
||||||
|
Encrypt the file plaintext.txt with the key and iv
|
||||||
|
openssl enc -aes-256-cbc -in plaintext.txt -out b-enc-aes.txt -K cddf0c7f664624fd0b604c622a2c670dffc3fdd7859a7cdd2377d1ba2ab86f89 -iv 30579741743af76bdd06ec45b12ee6d7
|
||||||
3
sheet02/a1/c-dec-rsa.txt
Normal file
3
sheet02/a1/c-dec-rsa.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Viktoria
|
||||||
|
Leo
|
||||||
|
Julian
|
||||||
2
sheet02/a1/c-enc-rsa.txt
Normal file
2
sheet02/a1/c-enc-rsa.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
’Ìnç‰á~Á>hì`£aÿ*‘Ú#ÒÒh‹å4ò,>«ôÓ„ì÷9&†…%´c,¤tNØR‰“5sŠq
|
||||||
|
˜°+$úd!E½Í·‘3£y£<79>3Ÿ|#Ù2ÒÈ„¬ÜºÕLÍ^é¢L9ÊApØîfDV<44>kX´¨^æeh\É£H«~y ƵýdÀK½I\€óf>I5EÿcÛ,‹I+s<>
|
||||||
28
sheet02/a1/c-rsa-priv.key
Normal file
28
sheet02/a1/c-rsa-priv.key
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
-----BEGIN PRIVATE KEY-----
|
||||||
|
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC1S64EHWciPKk+
|
||||||
|
6kTV3QGe97oGsO4pAbeAvqqzFtOulkIy1cEYlQIP+Ie9LL1i1tOjkx8v3j0Foq7T
|
||||||
|
DQkwCpTodJRl50hOdjOdhcjENU1GoZ2xHYb+sNwuVo7Rzl8rEP99p1N9slaq/d4L
|
||||||
|
jV4c6PWDjd2vGxcnPpihYtZOgq7XNN026Iu+E3v7z8MAcMxy2RAQyw2PeqgxJwyh
|
||||||
|
Pg1E6sz4OjnUQOhS0nwqz2XwG+XPfH8ytf3XjQPLnvUtmIscP1W4wjeJDJuvym5p
|
||||||
|
qr8Nr71yjYLm9tefQLONHxXUOJuxiJatJj9M5yKs6Z/gB2RRA26as6wyIuQ6WFu4
|
||||||
|
HTrUrgdrAgMBAAECggEABef0QFAQ5w2g2drZ6Tms7tfVKJik+ZMDvl73wqPQu5RL
|
||||||
|
jcpm4v0ftxN6oJAPj2O+O0r0riOIS2G0Xk3Dadw+Y6BAoV06fsvc/Jm6I6I05UMo
|
||||||
|
lveGSU/LrHrHZlBZy1ZfbGGCF8syhZmFnUy6ryhr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JyCbbkHHyuGi44Ul6CsX7Ib6vYyApI9+0wJHDddspFGve+sGdzQ3yc79/S
|
||||||
|
QcaOMQC+/dEG3oOY+j76lSDM5Xa4Ie5ekn6dwYU96iiPBK0D+wcjiZv/qott2xsr
|
||||||
|
dOPUdEes0Tj6vqVvDQLyXi1R
|
||||||
|
-----END PRIVATE KEY-----
|
||||||
9
sheet02/a1/c-rsa-pub.key
Normal file
9
sheet02/a1/c-rsa-pub.key
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtUuuBB1nIjypPupE1d0B
|
||||||
|
nve6BrDuKQG3gL6qsxbTrpZCMtXBGJUCD/iHvSy9YtbTo5MfL949BaKu0w0JMAqU
|
||||||
|
6HSUZedITnYznYXIxDVNRqGdsR2G/rDcLlaO0c5fKxD/fadTfbJWqv3eC41eHOj1
|
||||||
|
g43drxsXJz6YoWLWToKu1zTdNuiLvhN7+8/DAHDMctkQEMsNj3qoMScMoT4NROrM
|
||||||
|
+Do51EDoUtJ8Ks9l8Bvlz3x/MrX9140Dy571LZiLHD9VuMI3iQybr8puaaq/Da+9
|
||||||
|
co2C5vbXn0CzjR8V1DibsYiWrSY/TOcirOmf4AdkUQNumrOsMiLkOlhbuB061K4H
|
||||||
|
awIDAQAB
|
||||||
|
-----END PUBLIC KEY-----
|
||||||
8
sheet02/a1/c.txt
Normal file
8
sheet02/a1/c.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
Generate rsa private key:
|
||||||
|
openssl genpkey -algorithm RSA -out c-rsa-priv.key -pkeyopt rsa_keygen_bits:2048
|
||||||
|
|
||||||
|
Extract the public key from the private key
|
||||||
|
openssl pkey -in c-rsa-priv.key -pubout -out c-rsa-pub.key
|
||||||
|
|
||||||
|
Encrypt the file using the rsa pubkey
|
||||||
|
openssl pkeyutl -encrypt -pubin -inkey c-rsa-pub.key -in plaintext.txt -out c-enc-rsa.txt
|
||||||
0
sheet02/a1/d.txt
Normal file
0
sheet02/a1/d.txt
Normal file
9
sheet02/a1/e.txt
Normal file
9
sheet02/a1/e.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
Decrypt the aes-encrypted key with the aes-key and iv:
|
||||||
|
openssl enc -d -aes-256-cbc -in b-enc-aes.txt -out b-dec-aes.txt -K cddf0c7f664624fd0b604c622a2c670dffc3fdd7859a7cdd2377d1ba2ab86f89 -iv 30579741743af76bdd06ec45b12ee6d7
|
||||||
|
|
||||||
|
Decrypt encrypted file using the rsa private key
|
||||||
|
openssl pkeyutl -decrypt -inkey a1/c-rsa-priv.key -in a1/c-enc-rsa.txt -out a1/c-dec-rsa.txt
|
||||||
|
|
||||||
|
Verify the same files with the tool diff
|
||||||
|
diff plaintext.txt b-dec-aes.txt -> Prints nothing, so its the same
|
||||||
|
diff plaintext.txt c-dec-rsa.txt -> Prints nothing, so its the same
|
||||||
3
sheet02/a1/plaintext.txt
Normal file
3
sheet02/a1/plaintext.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Viktoria
|
||||||
|
Leo
|
||||||
|
Julian
|
||||||
23
sheet02/a2/SignECDSA.java
Normal file
23
sheet02/a2/SignECDSA.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.Signature;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class SignECDSA {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
final var gen = KeyPairGenerator.getInstance("EC");
|
||||||
|
final var kp = gen.genKeyPair();
|
||||||
|
|
||||||
|
final var bytes = Files.readAllBytes(Path.of("plaintext.txt"));
|
||||||
|
final var sig = Signature.getInstance("SHA256withECDSA");
|
||||||
|
sig.initVerify(kp.getPublic());
|
||||||
|
sig.initSign(kp.getPrivate());
|
||||||
|
sig.update(bytes);
|
||||||
|
final var signature = sig.sign();
|
||||||
|
|
||||||
|
Files.write(Path.of("b-signature.txt"), Base64.getEncoder().encode(signature));
|
||||||
|
Files.write(Path.of("b-publickey.txt"), Base64.getEncoder().encode(kp.getPublic().getEncoded()));
|
||||||
|
Files.write(Path.of("b-privatekey.txt"), Base64.getEncoder().encode(kp.getPrivate().getEncoded()));
|
||||||
|
}
|
||||||
|
}
|
||||||
26
sheet02/a2/VerifyECDSA.java
Normal file
26
sheet02/a2/VerifyECDSA.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import java.security.KeyFactory;
|
||||||
|
import java.security.Signature;
|
||||||
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class VerifyECDSA {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
if (args.length < 3) {
|
||||||
|
System.out.println(
|
||||||
|
"Please specify in the following order: Public Key (X509), Signature (Base64), Message (plain text)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String PUBLIC_KEY = args[0];
|
||||||
|
final String SIGNATURE = args[1];
|
||||||
|
final String MESSAGE = args[2];
|
||||||
|
|
||||||
|
final var kf = KeyFactory.getInstance("EC");
|
||||||
|
final var pub = kf.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(PUBLIC_KEY)));
|
||||||
|
|
||||||
|
final var sig = Signature.getInstance("SHA256withECDSA");
|
||||||
|
sig.initVerify(pub);
|
||||||
|
sig.update(MESSAGE.getBytes());
|
||||||
|
System.out.println("Correct: " + sig.verify(Base64.getDecoder().decode(SIGNATURE)));
|
||||||
|
}
|
||||||
|
}
|
||||||
1
sheet02/a2/b-privatekey.txt
Normal file
1
sheet02/a2/b-privatekey.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ME4CAQAwEAYHKoZIzj0CAQYFK4EEACIENzA1AgEBBDA/BgxBUzJUc3MfZVBIdPTbQfIdf4SABYZJlno8rUPkhBRrK3VkpEhyGdviYUPViz4=
|
||||||
1
sheet02/a2/b-publickey.txt
Normal file
1
sheet02/a2/b-publickey.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEVHteuMnscItIHycwEZ80P44WyIWmL7PmeN6HLRAu8IJsLKX6BTDawN0MezskF68oQ9Ft7zy2u4v2aaYB4tgUv+0YG7zEwFr0ofqSqFFPSLr0qrVRvXi/H7Bux8tYVgaN
|
||||||
1
sheet02/a2/b-signature.txt
Normal file
1
sheet02/a2/b-signature.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
MGQCMHq04CPgDiSaWPXb3LiuBrb97aV4JjinA5gIuEIFjFYLD1jYdECBdV/aHLLYsyMYnAIwedsP6qJKK4Y2uLgn3XLxbHgDKxf+eq07Rptmn/LttHTpWjtTfaLERM+Uh2BUGmMt
|
||||||
5
sheet02/a2/c.txt
Normal file
5
sheet02/a2/c.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
copy the publickey into a .pem file that starts with -----BEGIN PUBLIC KEY----- and ends with -----END PUBLIC KEY----- and has linebreaks after 64 chars.
|
||||||
|
then decode the base64 signature
|
||||||
|
base64 -d b-signature.txt > signature.bin
|
||||||
|
verify using openssl
|
||||||
|
openssl dgst -sha256 -verify c-pubkey.pem -signature signature.bin plaintext.txt
|
||||||
3
sheet02/a2/plaintext.txt
Normal file
3
sheet02/a2/plaintext.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Julian
|
||||||
|
Leonard
|
||||||
|
Viktoria
|
||||||
1
sheet02/a2/verify-example.sh
Executable file
1
sheet02/a2/verify-example.sh
Executable file
@@ -0,0 +1 @@
|
|||||||
|
java VerifyECDSA.java "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEnSgO/uuSq9QHH/rrnSgXeQTKPMI8tjLhyJzevl9fLQZW/zXuHk9iThiKY/c2k52quUby3czAwL2l2HaRqcBXEg==" "MEUCIAzRvK3CNFqMTZxIgMd1uQ/XybtRKXFht9S4q8qUy/IbAiEA7EcFYuRbViVC/zzIRW78HekjRHZVn3DjHVUcKyJ5sXw=" "hello world"
|
||||||
Reference in New Issue
Block a user