-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgpDecryption.java
More file actions
167 lines (135 loc) · 5.85 KB
/
PgpDecryption.java
File metadata and controls
167 lines (135 loc) · 5.85 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
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Iterator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPEncryptedDataList;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPOnePassSignature;
import org.bouncycastle.openpgp.PGPOnePassSignatureList;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil;
/**
* Simple routine to encrypt and decrypt using a Public and Private key with
* passphrase. This service routine provides the basic PGP services between byte
* arrays.
*
*/
public class PgpDecryption {
private static PGPPrivateKey findSecretKey(PGPSecretKeyRingCollection pgpSec, long keyID, char[] pass) throws PGPException, NoSuchProviderException {
PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID);
System.out.println("PGP Secret key :: " + pgpSecKey);
if (pgpSecKey == null) {
return null;
}
return pgpSecKey.extractPrivateKey(pass, "BC");
}
/**
* decrypt the passed in message stream
*
* @param encrypted
* The message to be decrypted.
* @param passPhrase
* Pass phrase (key)
*
* @return Clear text as a byte array. I18N considerations are not handled
* by this routine
* @exception IOException
* @exception PGPException
* @exception NoSuchProviderException
*/
public static byte[] decrypt(byte[] encrypted, InputStream keyIn, char[] password) throws IOException, PGPException, NoSuchProviderException {
InputStream in = new ByteArrayInputStream(encrypted);
in = PGPUtil.getDecoderStream(in);
System.out.println("Input stream of encrypted data :: " + in);
PGPObjectFactory pgpF = new PGPObjectFactory(in);
System.out.println("PGPObjectFactory :: " + pgpF);
PGPEncryptedDataList enc = null;
Object o = pgpF.nextObject();
System.out.println("PGPObjectFactory object :: " + o);
enc = o instanceof PGPEncryptedDataList ? (PGPEncryptedDataList) o : (PGPEncryptedDataList) pgpF.nextObject();
System.out.println("PGPEncryptedDataList :: " + enc);
// find the secret key
@SuppressWarnings("rawtypes")
Iterator it = enc.getEncryptedDataObjects();
PGPPrivateKey sKey = null;
PGPPublicKeyEncryptedData pbe = null;
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn));
System.out.println("Encrypted data object :: " + it);
while (sKey == null && it.hasNext()) {
pbe = (PGPPublicKeyEncryptedData) it.next();
sKey = findSecretKey(pgpSec, pbe.getKeyID(), password);
}
if (sKey == null) {
throw new IllegalArgumentException("secret key for message not found.");
}
System.out.println("PGPPublicKeyEncryptedData :: " + pbe);
System.out.println("PGPPrivateKey :: " + sKey);
InputStream clear = pbe.getDataStream(sKey, "BC");
System.out.println("InputStream from PGPPrivateKey :: " + clear);
PGPObjectFactory pgpFact = new PGPObjectFactory(clear);
System.out.println("PGPObjectFactory :: " + pgpFact);
PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact.nextObject();
System.out.println("PGPOnePassSignatureList :: " + p1);
PGPOnePassSignature ops = p1.get(0);
System.out.println("PGPOnePassSignature :: " + ops);
PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
System.out.println("PGPLiteralData :: " + p2);
InputStream dIn = p2.getInputStream();
System.out.println("Input stream of PGPLiteralData :: " + dIn);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int ch = 0;
System.out.println("Read input stream :: " + dIn.read());
while ((ch = dIn.read()) >= 0) {
out.write(ch);
}
byte[] returnBytes = out.toByteArray();
out.close();
System.out.println("Final data in bytes :: " + returnBytes);
return returnBytes;
}
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file " + file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
System.out.println("Added Security provider - BC");
for (int i = 0; i < Security.getProviders().length; i++) {
System.out.println(Security.getProviders()[i].getName() + " - " + Security.getProviders()[i].getInfo() + " - " + Security.getProviders()[i].getVersion());
}
byte[] encFromFile = getBytesFromFile(new File("C:/Users/achhabra/Desktop/TRP/TCContactsWorkday040616110200.csv.pgp"));
FileInputStream secKey = new FileInputStream("C:/Users/achhabra/Desktop/TRP/trowekeyP.pgp.asc");
byte[] decrypted = decrypt(encFromFile, secKey, "T@o3eM!t&a*e$hDev".toCharArray());
System.out.println("\ndecrypted data = '" + new String(decrypted) + "'");
}
}