Source Code Cross Referenced for PGPDSAElGamalTest.java in  » Security » Bouncy-Castle » org » bouncycastle » openpgp » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Security » Bouncy Castle » org.bouncycastle.openpgp.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.openpgp.test;
002:
003:        import org.bouncycastle.bcpg.BCPGOutputStream;
004:        import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
005:        import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
006:        import org.bouncycastle.jce.provider.BouncyCastleProvider;
007:        import org.bouncycastle.jce.spec.ElGamalParameterSpec;
008:        import org.bouncycastle.openpgp.PGPCompressedData;
009:        import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
010:        import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
011:        import org.bouncycastle.openpgp.PGPEncryptedDataList;
012:        import org.bouncycastle.openpgp.PGPException;
013:        import org.bouncycastle.openpgp.PGPKeyPair;
014:        import org.bouncycastle.openpgp.PGPLiteralData;
015:        import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
016:        import org.bouncycastle.openpgp.PGPObjectFactory;
017:        import org.bouncycastle.openpgp.PGPOnePassSignature;
018:        import org.bouncycastle.openpgp.PGPOnePassSignatureList;
019:        import org.bouncycastle.openpgp.PGPPrivateKey;
020:        import org.bouncycastle.openpgp.PGPPublicKey;
021:        import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
022:        import org.bouncycastle.openpgp.PGPPublicKeyRing;
023:        import org.bouncycastle.openpgp.PGPSecretKeyRing;
024:        import org.bouncycastle.openpgp.PGPSignature;
025:        import org.bouncycastle.openpgp.PGPSignatureGenerator;
026:        import org.bouncycastle.openpgp.PGPSignatureList;
027:        import org.bouncycastle.openpgp.PGPUtil;
028:        import org.bouncycastle.util.encoders.Base64;
029:        import org.bouncycastle.util.test.SimpleTest;
030:        import org.bouncycastle.util.test.UncloseableOutputStream;
031:
032:        import javax.crypto.Cipher;
033:        import javax.crypto.spec.DHParameterSpec;
034:        import java.io.ByteArrayInputStream;
035:        import java.io.ByteArrayOutputStream;
036:        import java.io.InputStream;
037:        import java.io.OutputStream;
038:        import java.math.BigInteger;
039:        import java.security.AlgorithmParameterGenerator;
040:        import java.security.AlgorithmParameters;
041:        import java.security.KeyPair;
042:        import java.security.KeyPairGenerator;
043:        import java.security.PublicKey;
044:        import java.security.SecureRandom;
045:        import java.security.Security;
046:        import java.util.Date;
047:        import java.util.Iterator;
048:
049:        public class PGPDSAElGamalTest extends SimpleTest {
050:
051:            byte[] testPubKeyRing = Base64
052:                    .decode("mQGiBEAR8jYRBADNifuSopd20JOQ5x30ljIaY0M6927+vo09NeNxS3KqItba"
053:                            + "nz9o5e2aqdT0W1xgdHYZmdElOHTTsugZxdXTEhghyxoo3KhVcNnTABQyrrvX"
054:                            + "qouvmP2fEDEw0Vpyk+90BpyY9YlgeX/dEA8OfooRLCJde/iDTl7r9FT+mts8"
055:                            + "g3azjwCgx+pOLD9LPBF5E4FhUOdXISJ0f4EEAKXSOi9nZzajpdhe8W2ZL9gc"
056:                            + "BpzZi6AcrRZBHOEMqd69gtUxA4eD8xycUQ42yH89imEcwLz8XdJ98uHUxGJi"
057:                            + "qp6hq4oakmw8GQfiL7yQIFgaM0dOAI9Afe3m84cEYZsoAFYpB4/s9pVMpPRH"
058:                            + "NsVspU0qd3NHnSZ0QXs8L8DXGO1uBACjDUj+8GsfDCIP2QF3JC+nPUNa0Y5t"
059:                            + "wKPKl+T8hX/0FBD7fnNeC6c9j5Ir/Fp/QtdaDAOoBKiyNLh1JaB1NY6US5zc"
060:                            + "qFks2seZPjXEiE6OIDXYra494mjNKGUobA4hqT2peKWXt/uBcuL1mjKOy8Qf"
061:                            + "JxgEd0MOcGJO+1PFFZWGzLQ3RXJpYyBILiBFY2hpZG5hICh0ZXN0IGtleSBv"
062:                            + "bmx5KSA8ZXJpY0Bib3VuY3ljYXN0bGUub3JnPohZBBMRAgAZBQJAEfI2BAsH"
063:                            + "AwIDFQIDAxYCAQIeAQIXgAAKCRAOtk6iUOgnkDdnAKC/CfLWikSBdbngY6OK"
064:                            + "5UN3+o7q1ACcDRqjT3yjBU3WmRUNlxBg3tSuljmwAgAAuQENBEAR8jgQBAC2"
065:                            + "kr57iuOaV7Ga1xcU14MNbKcA0PVembRCjcVjei/3yVfT/fuCVtGHOmYLEBqH"
066:                            + "bn5aaJ0P/6vMbLCHKuN61NZlts+LEctfwoya43RtcubqMc7eKw4k0JnnoYgB"
067:                            + "ocLXOtloCb7jfubOsnfORvrUkK0+Ne6anRhFBYfaBmGU75cQgwADBQP/XxR2"
068:                            + "qGHiwn+0YiMioRDRiIAxp6UiC/JQIri2AKSqAi0zeAMdrRsBN7kyzYVVpWwN"
069:                            + "5u13gPdQ2HnJ7d4wLWAuizUdKIQxBG8VoCxkbipnwh2RR4xCXFDhJrJFQUm+"
070:                            + "4nKx9JvAmZTBIlI5Wsi5qxst/9p5MgP3flXsNi1tRbTmRhqIRgQYEQIABgUC"
071:                            + "QBHyOAAKCRAOtk6iUOgnkBStAJoCZBVM61B1LG2xip294MZecMtCwQCbBbsk"
072:                            + "JVCXP0/Szm05GB+WN+MOCT2wAgAA");
073:
074:            byte[] testPrivKeyRing = Base64
075:                    .decode("lQHhBEAR8jYRBADNifuSopd20JOQ5x30ljIaY0M6927+vo09NeNxS3KqItba"
076:                            + "nz9o5e2aqdT0W1xgdHYZmdElOHTTsugZxdXTEhghyxoo3KhVcNnTABQyrrvX"
077:                            + "qouvmP2fEDEw0Vpyk+90BpyY9YlgeX/dEA8OfooRLCJde/iDTl7r9FT+mts8"
078:                            + "g3azjwCgx+pOLD9LPBF5E4FhUOdXISJ0f4EEAKXSOi9nZzajpdhe8W2ZL9gc"
079:                            + "BpzZi6AcrRZBHOEMqd69gtUxA4eD8xycUQ42yH89imEcwLz8XdJ98uHUxGJi"
080:                            + "qp6hq4oakmw8GQfiL7yQIFgaM0dOAI9Afe3m84cEYZsoAFYpB4/s9pVMpPRH"
081:                            + "NsVspU0qd3NHnSZ0QXs8L8DXGO1uBACjDUj+8GsfDCIP2QF3JC+nPUNa0Y5t"
082:                            + "wKPKl+T8hX/0FBD7fnNeC6c9j5Ir/Fp/QtdaDAOoBKiyNLh1JaB1NY6US5zc"
083:                            + "qFks2seZPjXEiE6OIDXYra494mjNKGUobA4hqT2peKWXt/uBcuL1mjKOy8Qf"
084:                            + "JxgEd0MOcGJO+1PFFZWGzP4DAwLeUcsVxIC2s2Bb9ab2XD860TQ2BI2rMD/r"
085:                            + "7/psx9WQ+Vz/aFAT3rXkEJ97nFeqEACgKmUCAEk9939EwLQ3RXJpYyBILiBF"
086:                            + "Y2hpZG5hICh0ZXN0IGtleSBvbmx5KSA8ZXJpY0Bib3VuY3ljYXN0bGUub3Jn"
087:                            + "PohZBBMRAgAZBQJAEfI2BAsHAwIDFQIDAxYCAQIeAQIXgAAKCRAOtk6iUOgn"
088:                            + "kDdnAJ9Ala3OcwEV1DbK906CheYWo4zIQwCfUqUOLMp/zj6QAk02bbJAhV1r"
089:                            + "sAewAgAAnQFYBEAR8jgQBAC2kr57iuOaV7Ga1xcU14MNbKcA0PVembRCjcVj"
090:                            + "ei/3yVfT/fuCVtGHOmYLEBqHbn5aaJ0P/6vMbLCHKuN61NZlts+LEctfwoya"
091:                            + "43RtcubqMc7eKw4k0JnnoYgBocLXOtloCb7jfubOsnfORvrUkK0+Ne6anRhF"
092:                            + "BYfaBmGU75cQgwADBQP/XxR2qGHiwn+0YiMioRDRiIAxp6UiC/JQIri2AKSq"
093:                            + "Ai0zeAMdrRsBN7kyzYVVpWwN5u13gPdQ2HnJ7d4wLWAuizUdKIQxBG8VoCxk"
094:                            + "bipnwh2RR4xCXFDhJrJFQUm+4nKx9JvAmZTBIlI5Wsi5qxst/9p5MgP3flXs"
095:                            + "Ni1tRbTmRhr+AwMC3lHLFcSAtrNg/EiWFLAnKNXH27zjwuhje8u2r+9iMTYs"
096:                            + "GjbRxaxRY0GKRhttCwqe2BC0lHhzifdlEcc9yjIjuKfepG2fnnSIRgQYEQIA"
097:                            + "BgUCQBHyOAAKCRAOtk6iUOgnkBStAJ9HFejVtVJ/A9LM/mDPe0ExhEXt/QCg"
098:                            + "m/KM7hJ/JrfnLQl7IaZsdg1F6vCwAgAA");
099:
100:            byte[] encMessage = Base64
101:                    .decode("hQEOAynbo4lhNjcHEAP/dgCkMtPB6mIgjFvNiotjaoh4sAXf4vFNkSeehQ2c"
102:                            + "r+IMt9CgIYodJI3FoJXxOuTcwesqTp5hRzgUBJS0adLDJwcNubFMy0M2tp5o"
103:                            + "KTWpXulIiqyO6f5jI/oEDHPzFoYgBmR4x72l/YpMy8UoYGtNxNvR7LVOfqJv"
104:                            + "uDY/71KMtPQEAIadOWpf1P5Td+61Zqn2VH2UV7H8eI6hGa6Lsy4sb9iZNE7f"
105:                            + "c+spGJlgkiOt8TrQoq3iOK9UN9nHZLiCSIEGCzsEn3uNuorD++Qs065ij+Oy"
106:                            + "36TKeuJ+38CfT7u47dEshHCPqWhBKEYrxZWHUJU/izw2Q1Yxd2XRxN+nafTL"
107:                            + "X1fQ0lABQUASa18s0BkkEERIdcKQXVLEswWcGqWNv1ZghC7xO2VDBX4HrPjp"
108:                            + "drjL63p2UHzJ7/4gPWGGtnqq1Xita/1mrImn7pzLThDWiT55vjw6Hw==");
109:
110:            byte[] signedAndEncMessage = Base64
111:                    .decode("hQEOAynbo4lhNjcHEAP+K20MVhzdX57hf/cU8TH0prP0VePr9mmeBedzqqMn"
112:                            + "fp2p8Zb68zmcMlI/WiL5XMNLYRmCgEcXyWbKdP/XV9m9LDBe1CMAGrkCeGBy"
113:                            + "je69IQQ5LS9vDPyEMF4iAAv/EqACjqHkizdY/a/FRx/t2ioXYdEC2jA6kS9C"
114:                            + "McpsNz16DE8EAIk3uKn4bGo/+15TXkyFYzW5Cf71SfRoHNmU2zAI93zhjN+T"
115:                            + "B7mGJwWXzsMkIO6FkMU5TCSrwZS3DBWCIaJ6SYoaawE/C/2j9D7bX1Jv8kum"
116:                            + "4cq+eZM7z6JYs6xend+WAwittpUxbEiyC2AJb3fBSXPAbLqWd6J6xbZZ7GDK"
117:                            + "r2Ca0pwBxwGhbMDyi2zpHLzw95H7Ah2wMcGU6kMLB+hzBSZ6mSTGFehqFQE3"
118:                            + "2BnAj7MtnbghiefogacJ891jj8Y2ggJeKDuRz8j2iICaTOy+Y2rXnnJwfYzm"
119:                            + "BMWcd2h1C5+UeBJ9CrrLniCCI8s5u8z36Rno3sfhBnXdRmWSxExXtocbg1Ht"
120:                            + "dyiThf6TK3W29Yy/T6x45Ws5zOasaJdsFKM=");
121:            char[] pass = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
122:                    'd' };
123:
124:            public void performTest() throws Exception {
125:                try {
126:                    PGPPublicKey pubKey = null;
127:
128:                    PGPUtil.setDefaultProvider("BC");
129:
130:                    //
131:                    // Read the public key
132:                    //
133:                    PGPObjectFactory pgpFact = new PGPObjectFactory(
134:                            testPubKeyRing);
135:
136:                    PGPPublicKeyRing pgpPub = (PGPPublicKeyRing) pgpFact
137:                            .nextObject();
138:
139:                    pubKey = pgpPub.getPublicKey();
140:
141:                    if (pubKey.getBitStrength() != 1024) {
142:                        fail("failed - key strength reported incorrectly.");
143:                    }
144:
145:                    //
146:                    // Read the private key
147:                    //
148:                    PGPSecretKeyRing sKey = new PGPSecretKeyRing(
149:                            testPrivKeyRing);
150:                    PGPPrivateKey pgpPrivKey = sKey.getSecretKey()
151:                            .extractPrivateKey(pass, "BC");
152:
153:                    //
154:                    // signature generation
155:                    //
156:                    String data = "hello world!";
157:                    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
158:                    ByteArrayInputStream testIn = new ByteArrayInputStream(data
159:                            .getBytes());
160:                    PGPSignatureGenerator sGen = new PGPSignatureGenerator(
161:                            PGPPublicKey.DSA, PGPUtil.SHA1, "BC");
162:
163:                    sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
164:
165:                    PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(
166:                            PGPCompressedData.ZIP);
167:
168:                    BCPGOutputStream bcOut = new BCPGOutputStream(cGen
169:                            .open(new UncloseableOutputStream(bOut)));
170:
171:                    sGen.generateOnePassVersion(false).encode(bcOut);
172:
173:                    PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
174:
175:                    Date testDate = new Date(
176:                            (System.currentTimeMillis() / 1000) * 1000);
177:                    OutputStream lOut = lGen.open(new UncloseableOutputStream(
178:                            bcOut), PGPLiteralData.BINARY, "_CONSOLE", data
179:                            .getBytes().length, testDate);
180:
181:                    int ch;
182:                    while ((ch = testIn.read()) >= 0) {
183:                        lOut.write(ch);
184:                        sGen.update((byte) ch);
185:                    }
186:
187:                    sGen.generate().encode(bcOut);
188:
189:                    lGen.close();
190:
191:                    cGen.close();
192:
193:                    //
194:                    // verify generated signature
195:                    //
196:                    pgpFact = new PGPObjectFactory(bOut.toByteArray());
197:
198:                    PGPCompressedData c1 = (PGPCompressedData) pgpFact
199:                            .nextObject();
200:
201:                    pgpFact = new PGPObjectFactory(c1.getDataStream());
202:
203:                    PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact
204:                            .nextObject();
205:
206:                    PGPOnePassSignature ops = p1.get(0);
207:
208:                    PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
209:                    if (!p2.getModificationTime().equals(testDate)) {
210:                        fail("Modification time not preserved");
211:                    }
212:
213:                    InputStream dIn = p2.getInputStream();
214:
215:                    ops.initVerify(pubKey, "BC");
216:
217:                    while ((ch = dIn.read()) >= 0) {
218:                        ops.update((byte) ch);
219:                    }
220:
221:                    PGPSignatureList p3 = (PGPSignatureList) pgpFact
222:                            .nextObject();
223:
224:                    if (!ops.verify(p3.get(0))) {
225:                        fail("Failed generated signature check");
226:                    }
227:
228:                    //
229:                    // test encryption
230:                    //
231:
232:                    //
233:                    // find a key sutiable for encryption
234:                    //
235:                    long pgpKeyID = 0;
236:                    PublicKey pKey = null;
237:
238:                    Iterator it = pgpPub.getPublicKeys();
239:                    while (it.hasNext()) {
240:                        PGPPublicKey pgpKey = (PGPPublicKey) it.next();
241:
242:                        if (pgpKey.getAlgorithm() == PGPPublicKey.ELGAMAL_ENCRYPT
243:                                || pgpKey.getAlgorithm() == PGPPublicKey.ELGAMAL_GENERAL) {
244:                            pKey = pgpKey.getKey("BC");
245:                            pgpKeyID = pgpKey.getKeyID();
246:                            if (pgpKey.getBitStrength() != 1024) {
247:                                fail("failed - key strength reported incorrectly.");
248:                            }
249:
250:                            //
251:                            // verify the key
252:                            //
253:
254:                        }
255:                    }
256:
257:                    Cipher c = Cipher.getInstance("ElGamal/None/PKCS1Padding",
258:                            "BC");
259:
260:                    c.init(Cipher.ENCRYPT_MODE, pKey);
261:
262:                    byte[] in = "hello world".getBytes();
263:
264:                    byte[] out = c.doFinal(in);
265:
266:                    pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(
267:                            pass, "BC");
268:
269:                    c.init(Cipher.DECRYPT_MODE, pgpPrivKey.getKey());
270:
271:                    out = c.doFinal(out);
272:
273:                    if (!areEqual(in, out)) {
274:                        fail("decryption failed.");
275:                    }
276:
277:                    //
278:                    // encrypted message
279:                    //
280:                    byte[] text = { (byte) 'h', (byte) 'e', (byte) 'l',
281:                            (byte) 'l', (byte) 'o', (byte) ' ', (byte) 'w',
282:                            (byte) 'o', (byte) 'r', (byte) 'l', (byte) 'd',
283:                            (byte) '!', (byte) '\n' };
284:
285:                    PGPObjectFactory pgpF = new PGPObjectFactory(encMessage);
286:
287:                    PGPEncryptedDataList encList = (PGPEncryptedDataList) pgpF
288:                            .nextObject();
289:
290:                    PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData) encList
291:                            .get(0);
292:
293:                    InputStream clear = encP.getDataStream(pgpPrivKey, "BC");
294:
295:                    pgpFact = new PGPObjectFactory(clear);
296:
297:                    c1 = (PGPCompressedData) pgpFact.nextObject();
298:
299:                    pgpFact = new PGPObjectFactory(c1.getDataStream());
300:
301:                    PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();
302:
303:                    bOut = new ByteArrayOutputStream();
304:
305:                    if (!ld.getFileName().equals("test.txt")) {
306:                        throw new RuntimeException("wrong filename in packet");
307:                    }
308:
309:                    InputStream inLd = ld.getDataStream();
310:
311:                    while ((ch = inLd.read()) >= 0) {
312:                        bOut.write(ch);
313:                    }
314:
315:                    if (!areEqual(bOut.toByteArray(), text)) {
316:                        fail("wrong plain text in decrypted packet");
317:                    }
318:
319:                    //
320:                    // signed and encrypted message
321:                    //
322:                    pgpF = new PGPObjectFactory(signedAndEncMessage);
323:
324:                    encList = (PGPEncryptedDataList) pgpF.nextObject();
325:
326:                    encP = (PGPPublicKeyEncryptedData) encList.get(0);
327:
328:                    clear = encP.getDataStream(pgpPrivKey, "BC");
329:
330:                    pgpFact = new PGPObjectFactory(clear);
331:
332:                    c1 = (PGPCompressedData) pgpFact.nextObject();
333:
334:                    pgpFact = new PGPObjectFactory(c1.getDataStream());
335:
336:                    p1 = (PGPOnePassSignatureList) pgpFact.nextObject();
337:
338:                    ops = p1.get(0);
339:
340:                    ld = (PGPLiteralData) pgpFact.nextObject();
341:
342:                    bOut = new ByteArrayOutputStream();
343:
344:                    if (!ld.getFileName().equals("test.txt")) {
345:                        throw new RuntimeException("wrong filename in packet");
346:                    }
347:
348:                    inLd = ld.getDataStream();
349:
350:                    //
351:                    // note: we use the DSA public key here.
352:                    //
353:                    ops.initVerify(pgpPub.getPublicKey(), "BC");
354:
355:                    while ((ch = inLd.read()) >= 0) {
356:                        ops.update((byte) ch);
357:                        bOut.write(ch);
358:                    }
359:
360:                    p3 = (PGPSignatureList) pgpFact.nextObject();
361:
362:                    if (!ops.verify(p3.get(0))) {
363:                        fail("Failed signature check");
364:                    }
365:
366:                    if (!areEqual(bOut.toByteArray(), text)) {
367:                        fail("wrong plain text in decrypted packet");
368:                    }
369:
370:                    //
371:                    // encrypt
372:                    //
373:                    ByteArrayOutputStream cbOut = new ByteArrayOutputStream();
374:                    PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(
375:                            SymmetricKeyAlgorithmTags.TRIPLE_DES,
376:                            new SecureRandom(), "BC");
377:                    PGPPublicKey puK = sKey.getSecretKey(pgpKeyID)
378:                            .getPublicKey();
379:
380:                    cPk.addMethod(puK);
381:
382:                    OutputStream cOut = cPk.open(new UncloseableOutputStream(
383:                            cbOut), bOut.toByteArray().length);
384:
385:                    cOut.write(text);
386:
387:                    cOut.close();
388:
389:                    pgpF = new PGPObjectFactory(cbOut.toByteArray());
390:
391:                    encList = (PGPEncryptedDataList) pgpF.nextObject();
392:
393:                    encP = (PGPPublicKeyEncryptedData) encList.get(0);
394:
395:                    pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(
396:                            pass, "BC");
397:
398:                    clear = encP.getDataStream(pgpPrivKey, "BC");
399:
400:                    bOut.reset();
401:
402:                    while ((ch = clear.read()) >= 0) {
403:                        bOut.write(ch);
404:                    }
405:
406:                    out = bOut.toByteArray();
407:
408:                    if (!areEqual(out, text)) {
409:                        fail("wrong plain text in generated packet");
410:                    }
411:
412:                    //
413:                    // use of PGPKeyPair
414:                    //
415:                    BigInteger g = new BigInteger(
416:                            "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc",
417:                            16);
418:                    BigInteger p = new BigInteger(
419:                            "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b",
420:                            16);
421:
422:                    KeyPairGenerator kpg = KeyPairGenerator.getInstance(
423:                            "ElGamal", "BC");
424:
425:                    ElGamalParameterSpec elParams = new ElGamalParameterSpec(p,
426:                            g);
427:
428:                    kpg.initialize(elParams);
429:
430:                    KeyPair kp = kpg.generateKeyPair();
431:
432:                    PGPKeyPair pgpKp = new PGPKeyPair(
433:                            PGPPublicKey.ELGAMAL_GENERAL, kp.getPublic(), kp
434:                                    .getPrivate(), new Date(), "BC");
435:
436:                    PGPPublicKey k1 = pgpKp.getPublicKey();
437:
438:                    PGPPrivateKey k2 = pgpKp.getPrivateKey();
439:
440:                    // Test bug with ElGamal P size != 0 mod 8 (don't use these sizes at home!)
441:                    SecureRandom random = new SecureRandom();
442:                    for (int pSize = 257; pSize < 264; ++pSize) {
443:                        // Generate some parameters of the given size
444:                        AlgorithmParameterGenerator a = AlgorithmParameterGenerator
445:                                .getInstance("ElGamal", "BC");
446:                        a.init(pSize, new SecureRandom());
447:                        AlgorithmParameters params = a.generateParameters();
448:
449:                        DHParameterSpec elP = (DHParameterSpec) params
450:                                .getParameterSpec(DHParameterSpec.class);
451:                        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(
452:                                "ElGamal", "BC");
453:
454:                        keyGen.initialize(elP);
455:
456:                        // Run a short encrypt/decrypt test with random key for the given parameters
457:                        kp = keyGen.generateKeyPair();
458:
459:                        PGPKeyPair elGamalKeyPair = new PGPKeyPair(
460:                                PublicKeyAlgorithmTags.ELGAMAL_GENERAL, kp,
461:                                new Date(), "BC");
462:
463:                        cPk = new PGPEncryptedDataGenerator(
464:                                SymmetricKeyAlgorithmTags.CAST5, random, "BC");
465:
466:                        puK = elGamalKeyPair.getPublicKey();
467:
468:                        cPk.addMethod(puK);
469:
470:                        cbOut = new ByteArrayOutputStream();
471:
472:                        cOut = cPk.open(cbOut, text.length);
473:
474:                        cOut.write(text);
475:
476:                        cOut.close();
477:
478:                        pgpF = new PGPObjectFactory(cbOut.toByteArray());
479:
480:                        encList = (PGPEncryptedDataList) pgpF.nextObject();
481:
482:                        encP = (PGPPublicKeyEncryptedData) encList.get(0);
483:
484:                        pgpPrivKey = elGamalKeyPair.getPrivateKey();
485:
486:                        // Note: This is where an exception would be expected if the P size causes problems
487:                        clear = encP.getDataStream(pgpPrivKey, "BC");
488:
489:                        ByteArrayOutputStream dec = new ByteArrayOutputStream();
490:
491:                        int b;
492:                        while ((b = clear.read()) >= 0) {
493:                            dec.write(b);
494:                        }
495:
496:                        byte[] decText = dec.toByteArray();
497:
498:                        if (!areEqual(text, decText)) {
499:                            fail("decrypted message incorrect");
500:                        }
501:                    }
502:
503:                } catch (PGPException e) {
504:                    fail("exception: " + e.getMessage(), e
505:                            .getUnderlyingException());
506:                }
507:            }
508:
509:            public String getName() {
510:                return "PGPDSAElGamalTest";
511:            }
512:
513:            public static void main(String[] args) {
514:                Security.addProvider(new BouncyCastleProvider());
515:
516:                runTest(new PGPDSAElGamalTest());
517:            }
518:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.