Source Code Cross Referenced for DSAElGamalKeyRingGenerator.java in  » Security » Bouncy-Castle » org » bouncycastle » openpgp » examples » 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.examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.openpgp.examples;
002:
003:        import java.io.FileOutputStream;
004:        import java.io.IOException;
005:        import java.io.OutputStream;
006:        import java.math.BigInteger;
007:        import java.security.InvalidKeyException;
008:        import java.security.KeyPair;
009:        import java.security.KeyPairGenerator;
010:        import java.security.NoSuchProviderException;
011:        import java.security.SecureRandom;
012:        import java.security.Security;
013:        import java.security.SignatureException;
014:        import java.util.Date;
015:
016:        import org.bouncycastle.bcpg.ArmoredOutputStream;
017:        import org.bouncycastle.jce.provider.BouncyCastleProvider;
018:        import org.bouncycastle.jce.spec.ElGamalParameterSpec;
019:        import org.bouncycastle.openpgp.PGPEncryptedData;
020:        import org.bouncycastle.openpgp.PGPException;
021:        import org.bouncycastle.openpgp.PGPKeyPair;
022:        import org.bouncycastle.openpgp.PGPKeyRingGenerator;
023:        import org.bouncycastle.openpgp.PGPPublicKey;
024:        import org.bouncycastle.openpgp.PGPSignature;
025:
026:        /**
027:         * A simple utility class that generates a public/secret keyring containing a DSA signing
028:         * key and an El Gamal key for encryption.
029:         * <p>
030:         * usage: DSAElGamalKeyRingGenerator [-a] identity passPhrase
031:         * <p>
032:         * Where identity is the name to be associated with the public key. The keys are placed 
033:         * in the files pub.[asc|bpg] and secret.[asc|bpg].
034:         * <p>
035:         * <b>Note</b>: this example encrypts the secret key using AES_256, many PGP products still
036:         * do not support this, if you are having problems importing keys try changing the algorithm
037:         * id to PGPEncryptedData.CAST5. CAST5 is more widelysupported.
038:         */
039:        public class DSAElGamalKeyRingGenerator {
040:            private static void exportKeyPair(OutputStream secretOut,
041:                    OutputStream publicOut, KeyPair dsaKp, KeyPair elgKp,
042:                    String identity, char[] passPhrase, boolean armor)
043:                    throws IOException, InvalidKeyException,
044:                    NoSuchProviderException, SignatureException, PGPException {
045:                if (armor) {
046:                    secretOut = new ArmoredOutputStream(secretOut);
047:                }
048:
049:                PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp,
050:                        new Date(), "BC");
051:                PGPKeyPair elgKeyPair = new PGPKeyPair(
052:                        PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date(), "BC");
053:
054:                PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(
055:                        PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair,
056:                        identity, PGPEncryptedData.AES_256, passPhrase, true,
057:                        null, null, new SecureRandom(), "BC");
058:
059:                keyRingGen.addSubKey(elgKeyPair);
060:
061:                keyRingGen.generateSecretKeyRing().encode(secretOut);
062:
063:                secretOut.close();
064:
065:                if (armor) {
066:                    publicOut = new ArmoredOutputStream(publicOut);
067:                }
068:
069:                keyRingGen.generatePublicKeyRing().encode(publicOut);
070:
071:                publicOut.close();
072:            }
073:
074:            public static void main(String[] args) throws Exception {
075:                Security.addProvider(new BouncyCastleProvider());
076:
077:                if (args.length < 2) {
078:                    System.out
079:                            .println("DSAElGamalKeyRingGenerator [-a] identity passPhrase");
080:                    System.exit(0);
081:                }
082:
083:                KeyPairGenerator dsaKpg = KeyPairGenerator.getInstance("DSA",
084:                        "BC");
085:
086:                dsaKpg.initialize(1024);
087:
088:                //
089:                // this takes a while as the key generator has to generate some DSA params
090:                // before it generates the key.
091:                //
092:                KeyPair dsaKp = dsaKpg.generateKeyPair();
093:
094:                KeyPairGenerator elgKpg = KeyPairGenerator.getInstance(
095:                        "ELGAMAL", "BC");
096:                BigInteger g = new BigInteger(
097:                        "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc",
098:                        16);
099:                BigInteger p = new BigInteger(
100:                        "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b",
101:                        16);
102:
103:                ElGamalParameterSpec elParams = new ElGamalParameterSpec(p, g);
104:
105:                elgKpg.initialize(elParams);
106:
107:                //
108:                // this is quicker because we are using pregenerated parameters.
109:                //
110:                KeyPair elgKp = elgKpg.generateKeyPair();
111:
112:                if (args[0].equals("-a")) {
113:                    if (args.length < 3) {
114:                        System.out
115:                                .println("DSAElGamalKeyRingGenerator [-a] identity passPhrase");
116:                        System.exit(0);
117:                    }
118:
119:                    FileOutputStream out1 = new FileOutputStream("secret.asc");
120:                    FileOutputStream out2 = new FileOutputStream("pub.asc");
121:
122:                    exportKeyPair(out1, out2, dsaKp, elgKp, args[1], args[2]
123:                            .toCharArray(), true);
124:                } else {
125:                    FileOutputStream out1 = new FileOutputStream("secret.bpg");
126:                    FileOutputStream out2 = new FileOutputStream("pub.bpg");
127:
128:                    exportKeyPair(out1, out2, dsaKp, elgKp, args[0], args[1]
129:                            .toCharArray(), false);
130:                }
131:            }
132:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.