diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/DigitalSignature.java | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/DigitalSignature.java')
| -rw-r--r-- | Master/Reference Architectures and Patterns/hjp5/examples/DigitalSignature.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/DigitalSignature.java b/Master/Reference Architectures and Patterns/hjp5/examples/DigitalSignature.java new file mode 100644 index 0000000..7a7b809 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/DigitalSignature.java @@ -0,0 +1,47 @@ +/* DigitalSignature.java */
+
+import java.io.*;
+import java.security.cert.Certificate;
+import java.security.*;
+
+public class DigitalSignature
+{
+ static final String KEYSTORE = "c:\\windows\\.keystore";
+ static final char[] KSPASS = {'h','j','p','3','k','s'};
+ static final String ALIAS = "hjp3";
+ static final char[] KEYPASS = {'h','j','p','3','k','e','y'};
+
+ public static void main(String[] args)
+ {
+ try {
+ //Laden der Schlüsseldatenbank
+ KeyStore ks = KeyStore.getInstance("JKS");
+ FileInputStream ksin = new FileInputStream(KEYSTORE);
+ ks.load(ksin, KSPASS);
+ ksin.close();
+ //Privaten Schlüssel "hjp3" lesen
+ Key key = ks.getKey(ALIAS, KEYPASS);
+ //Signatur-Objekt erstellen
+ Signature signature = Signature.getInstance("SHA/DSA");
+ signature.initSign((PrivateKey)key);
+ //Eingabedatei einlesen
+ FileInputStream in = new FileInputStream(args[0]);
+ int len;
+ byte[] data = new byte[1024];
+ while ((len = in.read(data)) > 0) {
+ //Signatur updaten
+ signature.update(data, 0, len);
+ }
+ in.close();
+ //Signatur berechnen
+ byte[] result = signature.sign();
+ //Signatur ausgeben
+ FileOutputStream out = new FileOutputStream(args[1]);
+ out.write(result, 0, result.length);
+ out.close();
+ } catch (Exception e) {
+ System.err.println(e.toString());
+ System.exit(1);
+ }
+ }
+}
\ No newline at end of file |
