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


001:        package org.bouncycastle.jce.provider.test;
002:
003:        import java.io.ByteArrayInputStream;
004:        import java.io.ByteArrayOutputStream;
005:        import java.io.DataInputStream;
006:        import java.security.Key;
007:        import java.security.Security;
008:
009:        import javax.crypto.Cipher;
010:        import javax.crypto.CipherInputStream;
011:        import javax.crypto.CipherOutputStream;
012:        import javax.crypto.KeyGenerator;
013:        import javax.crypto.SecretKey;
014:        import javax.crypto.spec.SecretKeySpec;
015:        import javax.crypto.spec.IvParameterSpec;
016:
017:        import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
018:        import org.bouncycastle.jce.provider.BouncyCastleProvider;
019:        import org.bouncycastle.util.encoders.Hex;
020:        import org.bouncycastle.util.test.SimpleTest;
021:
022:        /**
023:         * basic test class for the GOST28147 cipher
024:         */
025:        public class GOST28147Test extends SimpleTest {
026:            static String[] cipherTests = {
027:                    "256",
028:                    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
029:                    "4e6f77206973207468652074696d6520666f7220616c6c20",
030:                    "281630d0d5770030068c252d841e84149ccc1912052dbc02",
031:
032:                    "256",
033:                    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
034:                    "4e6f77206973207468652074696d65208a920c6ed1a804f5",
035:                    "88e543dfc04dc4f764fa7b624741cec07de49b007bf36065" };
036:
037:            public String getName() {
038:                return "GOST28147";
039:            }
040:
041:            public void testECB(int strength, byte[] keyBytes, byte[] input,
042:                    byte[] output) throws Exception {
043:                Key key;
044:                Cipher in, out;
045:                CipherInputStream cIn;
046:                CipherOutputStream cOut;
047:                ByteArrayInputStream bIn;
048:                ByteArrayOutputStream bOut;
049:
050:                key = new SecretKeySpec(keyBytes, "GOST28147");
051:
052:                in = Cipher.getInstance("GOST28147/ECB/NoPadding", "BC");
053:                out = Cipher.getInstance("GOST28147/ECB/NoPadding", "BC");
054:                out.init(Cipher.ENCRYPT_MODE, key);
055:                in.init(Cipher.DECRYPT_MODE, key);
056:
057:                //
058:                // encryption pass
059:                //
060:                bOut = new ByteArrayOutputStream();
061:
062:                cOut = new CipherOutputStream(bOut, out);
063:
064:                for (int i = 0; i != input.length / 2; i++) {
065:                    cOut.write(input[i]);
066:                }
067:                cOut.write(input, input.length / 2, input.length - input.length
068:                        / 2);
069:                cOut.close();
070:
071:                byte[] bytes;
072:
073:                bytes = bOut.toByteArray();
074:
075:                if (!areEqual(bytes, output)) {
076:                    fail("GOST28147 failed encryption - expected "
077:                            + new String(Hex.encode(output)) + " got "
078:                            + new String(Hex.encode(bytes)));
079:                }
080:
081:                //
082:                // decryption pass
083:                //
084:                bIn = new ByteArrayInputStream(bytes);
085:
086:                cIn = new CipherInputStream(bIn, in);
087:
088:                DataInputStream dIn = new DataInputStream(cIn);
089:
090:                bytes = new byte[input.length];
091:
092:                for (int i = 0; i != input.length / 2; i++) {
093:                    bytes[i] = (byte) dIn.read();
094:                }
095:                dIn.readFully(bytes, input.length / 2, bytes.length
096:                        - input.length / 2);
097:
098:                if (!areEqual(bytes, input)) {
099:                    fail("GOST28147 failed decryption - expected "
100:                            + new String(Hex.encode(input)) + " got "
101:                            + new String(Hex.encode(bytes)));
102:                }
103:            }
104:
105:            public void testCFB(int strength, byte[] keyBytes, byte[] input,
106:                    byte[] output) throws Exception {
107:                Key key;
108:                Cipher in, out;
109:                CipherInputStream cIn;
110:                CipherOutputStream cOut;
111:                ByteArrayInputStream bIn;
112:                ByteArrayOutputStream bOut;
113:
114:                key = new SecretKeySpec(keyBytes, "GOST28147");
115:
116:                in = Cipher.getInstance("GOST28147/CFB8/NoPadding", "BC");
117:                out = Cipher.getInstance("GOST28147/CFB8/NoPadding", "BC");
118:                byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
119:
120:                out.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
121:                in.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
122:
123:                //
124:                // encryption pass
125:                //
126:                bOut = new ByteArrayOutputStream();
127:
128:                cOut = new CipherOutputStream(bOut, out);
129:
130:                for (int i = 0; i != input.length / 2; i++) {
131:                    cOut.write(input[i]);
132:                }
133:                cOut.write(input, input.length / 2, input.length - input.length
134:                        / 2);
135:                cOut.close();
136:
137:                byte[] bytes;
138:
139:                bytes = bOut.toByteArray();
140:
141:                if (!areEqual(bytes, output)) {
142:                    fail("GOST28147 failed encryption - expected "
143:                            + new String(Hex.encode(output)) + " got "
144:                            + new String(Hex.encode(bytes)));
145:                }
146:
147:                //
148:                // decryption pass
149:                //
150:                bIn = new ByteArrayInputStream(bytes);
151:
152:                cIn = new CipherInputStream(bIn, in);
153:
154:                DataInputStream dIn = new DataInputStream(cIn);
155:
156:                bytes = new byte[input.length];
157:
158:                for (int i = 0; i != input.length / 2; i++) {
159:                    bytes[i] = (byte) dIn.read();
160:                }
161:                dIn.readFully(bytes, input.length / 2, bytes.length
162:                        - input.length / 2);
163:
164:                if (!areEqual(bytes, input)) {
165:                    fail("GOST28147 failed decryption - expected "
166:                            + new String(Hex.encode(input)) + " got "
167:                            + new String(Hex.encode(bytes)));
168:                }
169:            }
170:
171:            private void oidTest() {
172:                String[] oids = { CryptoProObjectIdentifiers.gostR28147_cbc
173:                        .getId(), };
174:
175:                String[] names = { "GOST28147/CBC/PKCS7Padding" };
176:
177:                try {
178:
179:                    byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
180:                            14, 15, 16 };
181:                    IvParameterSpec ivSpec = new IvParameterSpec(new byte[8]);
182:
183:                    for (int i = 0; i != oids.length; i++) {
184:                        Cipher c1 = Cipher.getInstance(oids[i], "BC");
185:                        Cipher c2 = Cipher.getInstance(names[i], "BC");
186:                        KeyGenerator kg = KeyGenerator.getInstance(oids[i],
187:                                "BC");
188:
189:                        SecretKey k = kg.generateKey();
190:
191:                        c1.init(Cipher.ENCRYPT_MODE, k, ivSpec);
192:                        c2.init(Cipher.DECRYPT_MODE, k, ivSpec);
193:
194:                        byte[] result = c2.doFinal(c1.doFinal(data));
195:
196:                        if (!areEqual(data, result)) {
197:                            fail("failed OID test");
198:                        }
199:                    }
200:                } catch (Exception ex) {
201:                    fail("failed exception " + ex.toString(), ex);
202:                }
203:            }
204:
205:            public void performTest() throws Exception {
206:                for (int i = 0; i != cipherTests.length; i += 8) {
207:                    testECB(Integer.parseInt(cipherTests[i]), Hex
208:                            .decode(cipherTests[i + 1]), Hex
209:                            .decode(cipherTests[i + 2]), Hex
210:                            .decode(cipherTests[i + 3]));
211:
212:                    testCFB(Integer.parseInt(cipherTests[i + 4]), Hex
213:                            .decode(cipherTests[i + 4 + 1]), Hex
214:                            .decode(cipherTests[i + 4 + 2]), Hex
215:                            .decode(cipherTests[i + 4 + 3]));
216:
217:                    oidTest();
218:                }
219:            }
220:
221:            public static void main(String[] args) {
222:                Security.addProvider(new BouncyCastleProvider());
223:
224:                runTest(new GOST28147Test());
225:            }
226:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.