-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignatureVerification.java
44 lines (36 loc) · 1.14 KB
/
SignatureVerification.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.Scanner;
public class SignatureVerification {
public static void main(String args[]) throws Exception{
//Creating KeyPair generator object
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DSA");
//Initializing the key pair generator
keyPairGen.initialize(2048);
//Generate the pair of keys
KeyPair pair = keyPairGen.generateKeyPair();
//Getting the privatekey from the key pair
PrivateKey privKey = pair.getPrivate();
//Creating a Signature object
Signature sign = Signature.getInstance("SHA256withDSA");
//Initializing the signature
sign.initSign(privKey);
byte[] bytes = "Hello how are you".getBytes();
//Adding data to the signature
sign.update(bytes);
//Calculating the signature
byte[] signature = sign.sign();
//Initializing the signature
sign.initVerify(pair.getPublic());
sign.update(bytes);
//Verifying the signature
boolean bool = sign.verify(signature);
if(bool) {
System.out.println("Signature verified");
} else {
System.out.println("Signature failed");
}
}
}