Compare commits
2 Commits
sheet01-su
...
76d4003957
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76d4003957 | ||
|
|
3b9e9034ba |
0
sheet02/a1/b.txt
Normal file
0
sheet02/a1/b.txt
Normal file
0
sheet02/a1/c.txt
Normal file
0
sheet02/a1/c.txt
Normal file
0
sheet02/a1/d.txt
Normal file
0
sheet02/a1/d.txt
Normal file
0
sheet02/a1/e.txt
Normal file
0
sheet02/a1/e.txt
Normal file
0
sheet02/a1/plaintext.txt
Normal file
0
sheet02/a1/plaintext.txt
Normal file
25
sheet02/a2/SignECDSA.java
Normal file
25
sheet02/a2/SignECDSA.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package sheet02.a2;
|
||||||
|
|
||||||
|
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("signature.txt"), Base64.getEncoder().encode(signature));
|
||||||
|
Files.write(Path.of("sign-key.pub"), Base64.getEncoder().encode(kp.getPublic().getEncoded()));
|
||||||
|
Files.write(Path.of("sign-key"), Base64.getEncoder().encode(kp.getPrivate().getEncoded()));
|
||||||
|
}
|
||||||
|
}
|
||||||
28
sheet02/a2/VerifyECDSA.java
Normal file
28
sheet02/a2/VerifyECDSA.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package sheet02.a2;
|
||||||
|
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
0
sheet02/a2/c.txt
Normal file
0
sheet02/a2/c.txt
Normal file
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