-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignatureTest.java
More file actions
143 lines (120 loc) · 5.1 KB
/
SignatureTest.java
File metadata and controls
143 lines (120 loc) · 5.1 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.DSAParams;
import sun.security.provider.DSAPrivateKey;
import sun.security.provider.DSAPublicKey;
public class SignatureTest
{
public static void main(String[] args)
{
try
{
/* To generate the key pair use it */
if (args[0].equals("-genkeypair"))
{
KeyPairGenerator pairgen = KeyPairGenerator.getInstance("DSA","SUN");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN");
pairgen.initialize(KEYSIZE, random);
KeyPair keyPair = pairgen.generateKeyPair();
DSAPrivateKey priv = (DSAPrivateKey) keyPair.getPrivate();
DSAPublicKey pub = (DSAPublicKey) keyPair.getPublic();
/* The code to print the parameters of the public and private key */
DSAParams dsaParams = priv.getParams();
BigInteger prime = dsaParams.getP();
BigInteger subPrime = dsaParams.getQ();
BigInteger base = dsaParams.getG();
BigInteger x = priv.getX();
BigInteger y = pub.getY();
System.out.println("The p is : " + prime);
System.out.println("The q is : " + subPrime);
System.out.println("The g is : " + base);
System.out.println("The x is : " + x);
System.out.println("The y is : " + y);
/* to print the key pairs to the respective files
Provide the file name in args[1] toprint the public key file
Provide the file name in args[2] toprint the private key file
*/
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
out.writeObject(keyPair.getPublic());
out.close();
out = new ObjectOutputStream(new FileOutputStream(args[2]));
out.writeObject(keyPair.getPrivate());
out.close();
}
/* To sign the document use this part
Provide the name of message file in args[1]
Provide the name of Signed data file in args[2]
Provide the name of private key file in args[3]
*/
else if (args[0].equals("-sign"))
{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
PrivateKey privkey = (PrivateKey) keyIn.readObject();
keyIn.close();
Signature signalg = Signature.getInstance("DSA");
signalg.initSign(privkey);
File infile = new File(args[1]);
InputStream in = new FileInputStream(infile);
int length = (int) infile.length();
byte[] message = new byte[length];
in.read(message, 0, length);
in.close();
signalg.update(message);
byte[] signature = signalg.sign();
DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
int signlength = signature.length;
out.writeInt(signlength);
out.write(signature, 0, signlength);
out.write(message, 0, length);
out.close();
}
/* To verify the document use this part
Provide the name of Signed message file in args[1]
Provide the name of public key file in args[2]
Provide the name of private key file in args[3]
*/
else if (args[0].equals("-verify"))
{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[2]));
PublicKey pubkey = (PublicKey) keyIn.readObject();
keyIn.close();
Signature verifyalg = Signature.getInstance("DSA");
verifyalg.initVerify(pubkey);
File infile = new File(args[1]);
DataInputStream in = new DataInputStream(new FileInputStream(infile));
int signlength = in.readInt();
byte[] signature = new byte[signlength];
in.read(signature, 0, signlength);
int length = (int) infile.length() - signlength - 4;
byte[] message = new byte[length];
in.read(message, 0, length);
in.close();
verifyalg.update(message);
if (!verifyalg.verify(signature))
System.out.print("not ");
System.out.println("verified");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static final int KEYSIZE = 1024;
}