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


001:        package org.bouncycastle.crypto.generators;
002:
003:        import org.bouncycastle.crypto.CipherParameters;
004:        import org.bouncycastle.crypto.Mac;
005:        import org.bouncycastle.crypto.PBEParametersGenerator;
006:        import org.bouncycastle.crypto.digests.SHA1Digest;
007:        import org.bouncycastle.crypto.macs.HMac;
008:        import org.bouncycastle.crypto.params.KeyParameter;
009:        import org.bouncycastle.crypto.params.ParametersWithIV;
010:
011:        /**
012:         * Generator for PBE derived keys and ivs as defined by PKCS 5 V2.0 Scheme 2.
013:         * This generator uses a SHA-1 HMac as the calculation function.
014:         * <p>
015:         * The document this implementation is based on can be found at
016:         * <a href=http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/index.html>
017:         * RSA's PKCS5 Page</a>
018:         */
019:        public class PKCS5S2ParametersGenerator extends PBEParametersGenerator {
020:            private Mac hMac = new HMac(new SHA1Digest());
021:
022:            /**
023:             * construct a PKCS5 Scheme 2 Parameters generator.
024:             */
025:            public PKCS5S2ParametersGenerator() {
026:            }
027:
028:            private void F(byte[] P, byte[] S, int c, byte[] iBuf, byte[] out,
029:                    int outOff) {
030:                byte[] state = new byte[hMac.getMacSize()];
031:                CipherParameters param = new KeyParameter(P);
032:
033:                hMac.init(param);
034:
035:                if (S != null) {
036:                    hMac.update(S, 0, S.length);
037:                }
038:
039:                hMac.update(iBuf, 0, iBuf.length);
040:
041:                hMac.doFinal(state, 0);
042:
043:                System.arraycopy(state, 0, out, outOff, state.length);
044:
045:                if (c == 0) {
046:                    throw new IllegalArgumentException(
047:                            "iteration count must be at least 1.");
048:                }
049:
050:                for (int count = 1; count < c; count++) {
051:                    hMac.init(param);
052:                    hMac.update(state, 0, state.length);
053:                    hMac.doFinal(state, 0);
054:
055:                    for (int j = 0; j != state.length; j++) {
056:                        out[outOff + j] ^= state[j];
057:                    }
058:                }
059:            }
060:
061:            private void intToOctet(byte[] buf, int i) {
062:                buf[0] = (byte) (i >>> 24);
063:                buf[1] = (byte) (i >>> 16);
064:                buf[2] = (byte) (i >>> 8);
065:                buf[3] = (byte) i;
066:            }
067:
068:            private byte[] generateDerivedKey(int dkLen) {
069:                int hLen = hMac.getMacSize();
070:                int l = (dkLen + hLen - 1) / hLen;
071:                byte[] iBuf = new byte[4];
072:                byte[] out = new byte[l * hLen];
073:
074:                for (int i = 1; i <= l; i++) {
075:                    intToOctet(iBuf, i);
076:
077:                    F(password, salt, iterationCount, iBuf, out, (i - 1) * hLen);
078:                }
079:
080:                return out;
081:            }
082:
083:            /**
084:             * Generate a key parameter derived from the password, salt, and iteration
085:             * count we are currently initialised with.
086:             *
087:             * @param keySize the size of the key we want (in bits)
088:             * @return a KeyParameter object.
089:             */
090:            public CipherParameters generateDerivedParameters(int keySize) {
091:                keySize = keySize / 8;
092:
093:                byte[] dKey = generateDerivedKey(keySize);
094:
095:                return new KeyParameter(dKey, 0, keySize);
096:            }
097:
098:            /**
099:             * Generate a key with initialisation vector parameter derived from
100:             * the password, salt, and iteration count we are currently initialised
101:             * with.
102:             *
103:             * @param keySize the size of the key we want (in bits)
104:             * @param ivSize the size of the iv we want (in bits)
105:             * @return a ParametersWithIV object.
106:             */
107:            public CipherParameters generateDerivedParameters(int keySize,
108:                    int ivSize) {
109:                keySize = keySize / 8;
110:                ivSize = ivSize / 8;
111:
112:                byte[] dKey = generateDerivedKey(keySize + ivSize);
113:
114:                return new ParametersWithIV(new KeyParameter(dKey, 0, keySize),
115:                        dKey, keySize, ivSize);
116:            }
117:
118:            /**
119:             * Generate a key parameter for use with a MAC derived from the password,
120:             * salt, and iteration count we are currently initialised with.
121:             *
122:             * @param keySize the size of the key we want (in bits)
123:             * @return a KeyParameter object.
124:             */
125:            public CipherParameters generateDerivedMacParameters(int keySize) {
126:                return generateDerivedParameters(keySize);
127:            }
128:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.