-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetVerExistingKeys.java
More file actions
218 lines (164 loc) · 5.89 KB
/
GetVerExistingKeys.java
File metadata and controls
218 lines (164 loc) · 5.89 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package stored_keys;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.Base64;
public class GetVerExistingKeys {
/*
* sign file with existing key in keystore with given alias
* and store sign in signedFile
*
* @param keyStore
* keyStore of the keys
* @param pathToCert
* path of the public key certificate
* @param signFile
* file where signed content is stored
* @param dataFile
* file which needs to be signed
*
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws InvalidKeyException
* @throws IOException
* @throws SignatureException
* @throws CertificateException
*/
void verUsingExistKeys(KeyStore keyStore, String pathToCert, byte realsgn[], String dataFile)
throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, NoSuchProviderException,
InvalidKeyException, SignatureException{
BufferedInputStream buf_in = new BufferedInputStream(new FileInputStream(pathToCert));
BufferedOutputStream buf_out = new BufferedOutputStream(new FileOutputStream("/home/varun/Documents/Example.txt"));
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
int len;
byte buffer[] = new byte[1024];
while(buf_in.available() > 0){
Certificate cert = certificateFactory.generateCertificate(buf_in);
keyStore.setCertificateEntry("Example", cert);
// X509Certificate c = (X509Certificate)certificateFactory.generateCertificate(buf_in);
}
buf_in.close();
buf_out.close();
System.out.println("Imported in keystore");
System.out.println("Certificate Exists: "+keyStore.isCertificateEntry("Example"));
Certificate certificate = keyStore.getCertificate("Example");
System.out.println(certificate.toString());
System.out.println("Certificate Type: "+certificate.getType());
PublicKey publicKey = keyStore.getCertificate("Example").getPublicKey();
Signature signature = Signature.getInstance("SHA1withDSA", "SUN");
signature.initVerify(publicKey);
FileInputStream fin_data = new FileInputStream(dataFile);
BufferedInputStream buf_data_in = new BufferedInputStream(fin_data);
buffer = new byte[1024];
while(buf_data_in.available()>0){
len = buf_data_in.read(buffer);
signature.update(buffer, 0, len);
}
boolean verified = signature.verify(realsgn);
System.out.println("File verified: "+verified);
buf_data_in.close();
}
public boolean verSignature(byte[] sign, File dataFile, Certificate cert) throws FileNotFoundException{
return verSignature(sign, new BufferedInputStream(new FileInputStream(dataFile)), cert);
}
public boolean verSignature(byte[] sign, String content, Certificate cert){
return verSignature(sign, new BufferedInputStream(new ByteArrayInputStream(content.getBytes())), cert);
}
public boolean verSignature(byte[] sign, BufferedInputStream buf_data_in, Certificate cert){
byte[] buffer;
int len;
boolean verified = false;
try{
Signature signature = Signature.getInstance("SHA1withDSA", "SUN");
PublicKey publicKey = cert.getPublicKey();
signature.initVerify(publicKey);
buffer = new byte[1024];
while(buf_data_in.available()>0){
len = buf_data_in.read(buffer);
signature.update(buffer, 0, len);
}
verified = signature.verify(sign);
// System.out.println("File verified: "+verified);
buf_data_in.close();
return verified;
} catch (SignatureException | NoSuchAlgorithmException | NoSuchProviderException | InvalidKeyException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public Certificate importCertFromFile(String pathToCert){
Certificate cert = null;
try(BufferedInputStream buf_in = new BufferedInputStream(new FileInputStream(pathToCert))){
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
while(buf_in.available() > 0){
cert = certificateFactory.generateCertificate(buf_in);
}
return cert;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (CertificateException e) {
e.printStackTrace();
return null;
}
}
byte[] getSignatureFromBytesFile(String signedFile){
try( FileInputStream fin_sgn = new FileInputStream(signedFile)){
byte realsgn[] = new byte[fin_sgn.available()];
fin_sgn.read(realsgn);
return realsgn;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
String getSignatureFromStringFile(String signedFile){
try(BufferedReader br = new BufferedReader(new FileReader(signedFile))){
StringBuffer strBuf = new StringBuffer();
int c;
while((c=br.read())!=-1){
strBuf.append((char)c);
}
return strBuf.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public byte[] convertBase64ToByte(String str){
Base64.Decoder decode = Base64.getDecoder();
return decode.decode(str);
}
}