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


001:        package org.bouncycastle.crypto.engines;
002:
003:        import org.bouncycastle.crypto.CipherParameters;
004:        import org.bouncycastle.crypto.DataLengthException;
005:        import org.bouncycastle.crypto.StreamCipher;
006:        import org.bouncycastle.crypto.params.KeyParameter;
007:
008:        /**
009:         * Implementation of Bob Jenkin's ISAAC (Indirection Shift Accumulate Add and Count).
010:         * see: http://www.burtleburtle.net/bob/rand/isaacafa.html
011:         */
012:        public class ISAACEngine implements  StreamCipher {
013:            // Constants
014:            private final int sizeL = 8, stateArraySize = sizeL << 5; // 256
015:
016:            // Cipher's internal state
017:            private int[] engineState = null, // mm                
018:                    results = null; // randrsl
019:            private int a = 0, b = 0, c = 0;
020:
021:            // Engine state
022:            private int index = 0;
023:            private byte[] keyStream = new byte[stateArraySize << 2], // results expanded into bytes
024:                    workingKey = null;
025:            private boolean initialised = false;
026:
027:            /**
028:             * initialise an ISAAC cipher.
029:             *
030:             * @param forEncryption whether or not we are for encryption.
031:             * @param params the parameters required to set up the cipher.
032:             * @exception IllegalArgumentException if the params argument is
033:             * inappropriate.
034:             */
035:            public void init(boolean forEncryption, CipherParameters params) {
036:                if (!(params instanceof  KeyParameter)) {
037:                    throw new IllegalArgumentException(
038:                            "invalid parameter passed to ISAAC init - "
039:                                    + params.getClass().getName());
040:                }
041:                /* 
042:                 * ISAAC encryption and decryption is completely
043:                 * symmetrical, so the 'forEncryption' is 
044:                 * irrelevant.
045:                 */
046:                KeyParameter p = (KeyParameter) params;
047:                setKey(p.getKey());
048:
049:                return;
050:            }
051:
052:            public byte returnByte(byte in) {
053:                if (index == 0) {
054:                    isaac();
055:                    keyStream = intToByteLittle(results);
056:                }
057:                byte out = (byte) (keyStream[index] ^ in);
058:                index = (index + 1) & 1023;
059:
060:                return out;
061:            }
062:
063:            public void processBytes(byte[] in, int inOff, int len, byte[] out,
064:                    int outOff) {
065:                if (!initialised) {
066:                    throw new IllegalStateException(getAlgorithmName()
067:                            + " not initialised");
068:                }
069:
070:                if ((inOff + len) > in.length) {
071:                    throw new DataLengthException("input buffer too short");
072:                }
073:
074:                if ((outOff + len) > out.length) {
075:                    throw new DataLengthException("output buffer too short");
076:                }
077:
078:                for (int i = 0; i < len; i++) {
079:                    if (index == 0) {
080:                        isaac();
081:                        keyStream = intToByteLittle(results);
082:                    }
083:                    out[i + outOff] = (byte) (keyStream[index] ^ in[i + inOff]);
084:                    index = (index + 1) & 1023;
085:                }
086:            }
087:
088:            public String getAlgorithmName() {
089:                return "ISAAC";
090:            }
091:
092:            public void reset() {
093:                setKey(workingKey);
094:            }
095:
096:            // Private implementation
097:            private void setKey(byte[] keyBytes) {
098:                workingKey = keyBytes;
099:
100:                if (engineState == null) {
101:                    engineState = new int[stateArraySize];
102:                }
103:
104:                if (results == null) {
105:                    results = new int[stateArraySize];
106:                }
107:
108:                int i, j, k;
109:
110:                // Reset state
111:                for (i = 0; i < stateArraySize; i++) {
112:                    engineState[i] = results[i] = 0;
113:                }
114:                a = b = c = 0;
115:
116:                // Reset index counter for output
117:                index = 0;
118:
119:                // Convert the key bytes to ints and put them into results[] for initialization
120:                byte[] t = new byte[keyBytes.length + (keyBytes.length & 3)];
121:                System.arraycopy(keyBytes, 0, t, 0, keyBytes.length);
122:                for (i = 0; i < t.length; i += 4) {
123:                    results[i >> 2] = byteToIntLittle(t, i);
124:                }
125:
126:                // It has begun?
127:                int[] abcdefgh = new int[sizeL];
128:
129:                for (i = 0; i < sizeL; i++) {
130:                    abcdefgh[i] = 0x9e3779b9; // Phi (golden ratio)
131:                }
132:
133:                for (i = 0; i < 4; i++) {
134:                    mix(abcdefgh);
135:                }
136:
137:                for (i = 0; i < 2; i++) {
138:                    for (j = 0; j < stateArraySize; j += sizeL) {
139:                        for (k = 0; k < sizeL; k++) {
140:                            abcdefgh[k] += (i < 1) ? results[j + k]
141:                                    : engineState[j + k];
142:                        }
143:
144:                        mix(abcdefgh);
145:
146:                        for (k = 0; k < sizeL; k++) {
147:                            engineState[j + k] = abcdefgh[k];
148:                        }
149:                    }
150:                }
151:
152:                isaac();
153:
154:                initialised = true;
155:            }
156:
157:            private void isaac() {
158:                int i, x, y;
159:
160:                b += ++c;
161:                for (i = 0; i < stateArraySize; i++) {
162:                    x = engineState[i];
163:                    switch (i & 3) {
164:                    case 0:
165:                        a ^= (a << 13);
166:                        break;
167:                    case 1:
168:                        a ^= (a >>> 6);
169:                        break;
170:                    case 2:
171:                        a ^= (a << 2);
172:                        break;
173:                    case 3:
174:                        a ^= (a >>> 16);
175:                        break;
176:                    }
177:                    a += engineState[(i + 128) & 0xFF];
178:                    engineState[i] = y = engineState[(x >>> 2) & 0xFF] + a + b;
179:                    results[i] = b = engineState[(y >>> 10) & 0xFF] + x;
180:                }
181:            }
182:
183:            private void mix(int[] x) {
184:                x[0] ^= x[1] << 11;
185:                x[3] += x[0];
186:                x[1] += x[2];
187:                x[1] ^= x[2] >>> 2;
188:                x[4] += x[1];
189:                x[2] += x[3];
190:                x[2] ^= x[3] << 8;
191:                x[5] += x[2];
192:                x[3] += x[4];
193:                x[3] ^= x[4] >>> 16;
194:                x[6] += x[3];
195:                x[4] += x[5];
196:                x[4] ^= x[5] << 10;
197:                x[7] += x[4];
198:                x[5] += x[6];
199:                x[5] ^= x[6] >>> 4;
200:                x[0] += x[5];
201:                x[6] += x[7];
202:                x[6] ^= x[7] << 8;
203:                x[1] += x[6];
204:                x[7] += x[0];
205:                x[7] ^= x[0] >>> 9;
206:                x[2] += x[7];
207:                x[0] += x[1];
208:            }
209:
210:            private int byteToIntLittle(byte[] x, int offset) {
211:                return (int) (x[offset++] & 0xFF) | ((x[offset++] & 0xFF) << 8)
212:                        | ((x[offset++] & 0xFF) << 16) | (x[offset++] << 24);
213:            }
214:
215:            private byte[] intToByteLittle(int x) {
216:                byte[] out = new byte[4];
217:                out[3] = (byte) x;
218:                out[2] = (byte) (x >>> 8);
219:                out[1] = (byte) (x >>> 16);
220:                out[0] = (byte) (x >>> 24);
221:                return out;
222:            }
223:
224:            private byte[] intToByteLittle(int[] x) {
225:                byte[] out = new byte[4 * x.length];
226:                for (int i = 0, j = 0; i < x.length; i++, j += 4) {
227:                    System.arraycopy(intToByteLittle(x[i]), 0, out, j, 4);
228:                }
229:                return out;
230:            }
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.