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
|
package org.spongycastle.openpgp.examples;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.SignatureException;
import java.security.Security;
import org.spongycastle.bcpg.ArmoredOutputStream;
import org.spongycastle.bcpg.BCPGOutputStream;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.openpgp.PGPCompressedData;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPObjectFactory;
import org.spongycastle.openpgp.PGPPrivateKey;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRingCollection;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSignature;
import org.spongycastle.openpgp.PGPSignatureGenerator;
import org.spongycastle.openpgp.PGPSignatureList;
import org.spongycastle.openpgp.PGPUtil;
import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider;
import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
/**
* A simple utility class that creates seperate signatures for files and verifies them.
* <p>
* To sign a file: DetachedSignatureProcessor -s [-a] fileName secretKey passPhrase.<br>
* If -a is specified the output file will be "ascii-armored".
* <p>
* To decrypt: DetachedSignatureProcessor -v fileName signatureFile publicKeyFile.
* <p>
* Note: this example will silently overwrite files.
* It also expects that a single pass phrase
* will have been used.
*/
public class DetachedSignatureProcessor
{
private static void verifySignature(
String fileName,
String inputFileName,
String keyFileName)
throws GeneralSecurityException, IOException, PGPException, SignatureException
{
InputStream in = new BufferedInputStream(new FileInputStream(inputFileName));
InputStream keyIn = new BufferedInputStream(new FileInputStream(keyFileName));
verifySignature(fileName, in, keyIn);
keyIn.close();
in.close();
}
/*
* verify the signature in in against the file fileName.
*/
private static void verifySignature(
String fileName,
InputStream in,
InputStream keyIn)
throws GeneralSecurityException, IOException, PGPException, SignatureException
{
in = PGPUtil.getDecoderStream(in);
PGPObjectFactory pgpFact = new PGPObjectFactory(in);
PGPSignatureList p3;
Object o = pgpFact.nextObject();
if (o instanceof PGPCompressedData)
{
PGPCompressedData c1 = (PGPCompressedData)o;
pgpFact = new PGPObjectFactory(c1.getDataStream());
p3 = (PGPSignatureList)pgpFact.nextObject();
}
else
{
p3 = (PGPSignatureList)o;
}
PGPPublicKeyRingCollection pgpPubRingCollection = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn));
InputStream dIn = new BufferedInputStream(new FileInputStream(fileName));
PGPSignature sig = p3.get(0);
PGPPublicKey key = pgpPubRingCollection.getPublicKey(sig.getKeyID());
sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("SC"), key);
int ch;
while ((ch = dIn.read()) >= 0)
{
sig.update((byte)ch);
}
dIn.close();
if (sig.verify())
{
System.out.println("signature verified.");
}
else
{
System.out.println("signature verification failed.");
}
}
private static void createSignature(
String inputFileName,
String keyFileName,
String outputFileName,
char[] pass,
boolean armor)
throws GeneralSecurityException, IOException, PGPException, SignatureException
{
InputStream keyIn = new BufferedInputStream(new FileInputStream(keyFileName));
OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFileName));
createSignature(inputFileName, keyIn, out, pass, armor);
out.close();
keyIn.close();
}
private static void createSignature(
String fileName,
InputStream keyIn,
OutputStream out,
char[] pass,
boolean armor)
throws GeneralSecurityException, IOException, PGPException, SignatureException
{
if (armor)
{
out = new ArmoredOutputStream(out);
}
PGPSecretKey pgpSec = PGPExampleUtil.readSecretKey(keyIn);
PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("SC").build(pass));
PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), PGPUtil.SHA1).setProvider("SC"));
sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
BCPGOutputStream bOut = new BCPGOutputStream(out);
InputStream fIn = new BufferedInputStream(new FileInputStream(fileName));
int ch;
while ((ch = fIn.read()) >= 0)
{
sGen.update((byte)ch);
}
fIn.close();
sGen.generate().encode(bOut);
if (armor)
{
out.close();
}
}
public static void main(
String[] args)
throws Exception
{
Security.addProvider(new BouncyCastleProvider());
if (args[0].equals("-s"))
{
if (args[1].equals("-a"))
{
createSignature(args[2], args[3], args[2] + ".asc", args[4].toCharArray(), true);
}
else
{
createSignature(args[1], args[2], args[1] + ".bpg", args[3].toCharArray(), false);
}
}
else if (args[0].equals("-v"))
{
verifySignature(args[1], args[2], args[3]);
}
else
{
System.err.println("usage: DetachedSignatureProcessor [-s [-a] file keyfile passPhrase]|[-v file sigFile keyFile]");
}
}
}
|