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


001:        package org.bouncycastle.crypto.test;
002:
003:        import java.security.SecureRandom;
004:
005:        import org.bouncycastle.crypto.engines.DESEngine;
006:        import org.bouncycastle.crypto.paddings.BlockCipherPadding;
007:        import org.bouncycastle.crypto.paddings.ISO10126d2Padding;
008:        import org.bouncycastle.crypto.paddings.ISO7816d4Padding;
009:        import org.bouncycastle.crypto.paddings.PKCS7Padding;
010:        import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
011:        import org.bouncycastle.crypto.paddings.TBCPadding;
012:        import org.bouncycastle.crypto.paddings.X923Padding;
013:        import org.bouncycastle.crypto.paddings.ZeroBytePadding;
014:        import org.bouncycastle.crypto.params.KeyParameter;
015:        import org.bouncycastle.util.encoders.Hex;
016:        import org.bouncycastle.util.test.SimpleTest;
017:
018:        /**
019:         * General Padding tests.
020:         */
021:        public class PaddingTest extends SimpleTest {
022:            public PaddingTest() {
023:            }
024:
025:            private void blockCheck(PaddedBufferedBlockCipher cipher,
026:                    BlockCipherPadding padding, KeyParameter key, byte[] data) {
027:                byte[] out = new byte[data.length + 8];
028:                byte[] dec = new byte[data.length];
029:
030:                try {
031:                    cipher.init(true, key);
032:
033:                    int len = cipher.processBytes(data, 0, data.length, out, 0);
034:
035:                    len += cipher.doFinal(out, len);
036:
037:                    cipher.init(false, key);
038:
039:                    int decLen = cipher.processBytes(out, 0, len, dec, 0);
040:
041:                    decLen += cipher.doFinal(dec, decLen);
042:
043:                    if (!areEqual(data, dec)) {
044:                        fail("failed to decrypt - i = " + data.length
045:                                + ", padding = " + padding.getPaddingName());
046:                    }
047:                } catch (Exception e) {
048:                    fail("Exception - " + e.toString(), e);
049:                }
050:            }
051:
052:            public void testPadding(BlockCipherPadding padding,
053:                    SecureRandom rand, byte[] ffVector, byte[] ZeroVector) {
054:                PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
055:                        new DESEngine(), padding);
056:                KeyParameter key = new KeyParameter(Hex
057:                        .decode("0011223344556677"));
058:
059:                //
060:                // ff test
061:                //
062:                byte[] data = { (byte) 0xff, (byte) 0xff, (byte) 0xff,
063:                        (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
064:
065:                if (ffVector != null) {
066:                    padding.addPadding(data, 3);
067:
068:                    if (!areEqual(data, ffVector)) {
069:                        fail("failed ff test for " + padding.getPaddingName());
070:                    }
071:                }
072:
073:                //
074:                // zero test
075:                //
076:                if (ZeroVector != null) {
077:                    data = new byte[8];
078:                    padding.addPadding(data, 4);
079:
080:                    if (!areEqual(data, ZeroVector)) {
081:                        fail("failed zero test for " + padding.getPaddingName());
082:                    }
083:                }
084:
085:                for (int i = 1; i != 200; i++) {
086:                    data = new byte[i];
087:
088:                    rand.nextBytes(data);
089:
090:                    blockCheck(cipher, padding, key, data);
091:                }
092:            }
093:
094:            public void performTest() {
095:                SecureRandom rand = new SecureRandom(new byte[20]);
096:
097:                rand.setSeed(System.currentTimeMillis());
098:
099:                testPadding(new PKCS7Padding(), rand, Hex
100:                        .decode("ffffff0505050505"), Hex
101:                        .decode("0000000004040404"));
102:
103:                testPadding(new ISO10126d2Padding(), rand, null, null);
104:
105:                testPadding(new X923Padding(), rand, null, null);
106:
107:                testPadding(new TBCPadding(), rand, Hex
108:                        .decode("ffffff0000000000"), Hex
109:                        .decode("00000000ffffffff"));
110:
111:                testPadding(new ZeroBytePadding(), rand, Hex
112:                        .decode("ffffff0000000000"), null);
113:
114:                testPadding(new ISO7816d4Padding(), rand, Hex
115:                        .decode("ffffff8000000000"), Hex
116:                        .decode("0000000080000000"));
117:            }
118:
119:            public String getName() {
120:                return "PaddingTest";
121:            }
122:
123:            public static void main(String[] args) {
124:                runTest(new PaddingTest());
125:            }
126:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.