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


001:        package org.bouncycastle.openpgp;
002:
003:        import org.bouncycastle.bcpg.BCPGInputStream;
004:        import org.bouncycastle.bcpg.HashAlgorithmTags;
005:        import org.bouncycastle.bcpg.InputStreamPacket;
006:        import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
007:        import org.bouncycastle.bcpg.SymmetricKeyEncSessionPacket;
008:
009:        import java.io.EOFException;
010:        import java.io.InputStream;
011:        import java.security.DigestInputStream;
012:        import java.security.MessageDigest;
013:        import java.security.NoSuchAlgorithmException;
014:        import java.security.NoSuchProviderException;
015:
016:        import javax.crypto.Cipher;
017:        import javax.crypto.CipherInputStream;
018:        import javax.crypto.NoSuchPaddingException;
019:        import javax.crypto.SecretKey;
020:        import javax.crypto.spec.IvParameterSpec;
021:        import javax.crypto.spec.SecretKeySpec;
022:
023:        /**
024:         * A password based encryption object.
025:         */
026:        public class PGPPBEEncryptedData extends PGPEncryptedData {
027:            SymmetricKeyEncSessionPacket keyData;
028:
029:            PGPPBEEncryptedData(SymmetricKeyEncSessionPacket keyData,
030:                    InputStreamPacket encData) {
031:                super (encData);
032:
033:                this .keyData = keyData;
034:            }
035:
036:            /**
037:             * Return the raw input stream for the data stream.
038:             * 
039:             * @return InputStream
040:             */
041:            public InputStream getInputStream() {
042:                return encData.getInputStream();
043:            }
044:
045:            /**
046:             * Return the decrypted input stream, using the passed in passPhrase.
047:             * 
048:             * @param passPhrase
049:             * @param provider
050:             * @return InputStream
051:             * @throws PGPException
052:             * @throws NoSuchProviderException
053:             */
054:            public InputStream getDataStream(char[] passPhrase, String provider)
055:                    throws PGPException, NoSuchProviderException {
056:                try {
057:                    int keyAlgorithm = keyData.getEncAlgorithm();
058:                    SecretKey key = PGPUtil.makeKeyFromPassPhrase(keyAlgorithm,
059:                            keyData.getS2K(), passPhrase, provider);
060:
061:                    byte[] secKeyData = keyData.getSecKeyData();
062:                    if (secKeyData != null) {
063:                        Cipher keyCipher = Cipher.getInstance(PGPUtil
064:                                .getSymmetricCipherName(keyAlgorithm)
065:                                + "/CFB/NoPadding", provider);
066:
067:                        keyCipher.init(Cipher.DECRYPT_MODE, key,
068:                                new IvParameterSpec(new byte[keyCipher
069:                                        .getBlockSize()]));
070:
071:                        byte[] keyBytes = keyCipher.doFinal(secKeyData);
072:
073:                        keyAlgorithm = keyBytes[0];
074:                        key = new SecretKeySpec(keyBytes, 1,
075:                                keyBytes.length - 1, PGPUtil
076:                                        .getSymmetricCipherName(keyAlgorithm));
077:                    }
078:
079:                    Cipher c = createStreamCipher(keyAlgorithm, provider);
080:
081:                    byte[] iv = new byte[c.getBlockSize()];
082:
083:                    c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
084:
085:                    encStream = new BCPGInputStream(new CipherInputStream(
086:                            encData.getInputStream(), c));
087:
088:                    if (encData instanceof  SymmetricEncIntegrityPacket) {
089:                        truncStream = new TruncatedStream(encStream);
090:
091:                        String digestName = PGPUtil
092:                                .getDigestName(HashAlgorithmTags.SHA1);
093:                        MessageDigest digest = MessageDigest.getInstance(
094:                                digestName, provider);
095:
096:                        encStream = new DigestInputStream(truncStream, digest);
097:                    }
098:
099:                    for (int i = 0; i != iv.length; i++) {
100:                        int ch = encStream.read();
101:
102:                        if (ch < 0) {
103:                            throw new EOFException("unexpected end of stream.");
104:                        }
105:
106:                        iv[i] = (byte) ch;
107:                    }
108:
109:                    int v1 = encStream.read();
110:                    int v2 = encStream.read();
111:
112:                    if (v1 < 0 || v2 < 0) {
113:                        throw new EOFException("unexpected end of stream.");
114:                    }
115:
116:                    // Note: the oracle attack on "quick check" bytes is not deemed
117:                    // a security risk for PBE (see PGPPublicKeyEncryptedData)
118:
119:                    boolean repeatCheckPassed = iv[iv.length - 2] == (byte) v1
120:                            && iv[iv.length - 1] == (byte) v2;
121:
122:                    // Note: some versions of PGP appear to produce 0 for the extra
123:                    // bytes rather than repeating the two previous bytes
124:                    boolean zeroesCheckPassed = v1 == 0 && v2 == 0;
125:
126:                    if (!repeatCheckPassed && !zeroesCheckPassed) {
127:                        throw new PGPDataValidationException(
128:                                "data check failed.");
129:                    }
130:
131:                    return encStream;
132:                } catch (PGPException e) {
133:                    throw e;
134:                } catch (Exception e) {
135:                    throw new PGPException("Exception creating cipher", e);
136:                }
137:            }
138:
139:            private Cipher createStreamCipher(int keyAlgorithm, String provider)
140:                    throws NoSuchAlgorithmException, NoSuchPaddingException,
141:                    NoSuchProviderException, PGPException {
142:                String mode = (encData instanceof  SymmetricEncIntegrityPacket) ? "CFB"
143:                        : "OpenPGPCFB";
144:
145:                String cName = PGPUtil.getSymmetricCipherName(keyAlgorithm)
146:                        + "/" + mode + "/NoPadding";
147:
148:                return Cipher.getInstance(cName, provider);
149:            }
150:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.