KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > pdfbox > encryption > TestEncryption


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package test.pdfbox.encryption;
32
33 import java.io.ByteArrayInputStream;
34 import java.io.ByteArrayOutputStream;
35
36 import junit.framework.Test;
37 import junit.framework.TestCase;
38 import junit.framework.TestSuite;
39
40 import org.apache.log4j.Logger;
41
42 import org.pdfbox.encryption.PDFEncryption;
43
44 /**
45  * This will test the encryption algorithms in PDFBox.
46  *
47  * @author Ben Litchfield (ben@csh.rit.edu)
48  * @version $Revision: 1.3 $
49  */

50 public class TestEncryption extends TestCase
51 {
52     private static Logger log = Logger.getLogger(TestEncryption.class);
53
54     /**
55      * Constructor.
56      *
57      * @param name The name of the test to run.
58      */

59     public TestEncryption( String name )
60     {
61         super( name );
62     }
63
64     /**
65      * This will get the suite of test that this class holds.
66      *
67      * @return All of the tests that this class holds.
68      */

69     public static Test suite()
70     {
71         return new TestSuite( TestEncryption.class );
72     }
73
74     /**
75      * infamous main method.
76      *
77      * @param args The command line arguments.
78      */

79     public static void main( String[] args )
80     {
81         String[] arg = {TestEncryption.class.getName() };
82         junit.textui.TestRunner.main( arg );
83     }
84
85     /**
86      * This will test some simple encryption.
87      *
88      * @throws Exception If there is an exception while encrypting.
89      */

90     public void testEncryption() throws Exception
91     {
92         byte[] key={0x65, 0x3d, 0x4f, 0x70, 0x0c };
93         byte[] data={0x31, 0x20, 0x30, 0x20, 0x30, 0x20, 0x72, 0x67, 0x20, 0x30,
94                     0x20, 0x30, 0x20, 0x33, 0x30, 0x38, 0x2e, 0x34, 0x37, 0x34,
95                     0x37, 0x20, 0x35, 0x37, 0x2e, 0x36, 0x32, 0x37, 0x31, 0x20,
96                     0x72, 0x65, 0x20, 0x66, 0x20, 0x30, 0x20, 0x47, 0x20, 0x31,
97                     0x20, 0x77, 0x20, 0x30, 0x2e, 0x35, 0x20, 0x30, 0x2e, 0x35,
98                     0x20, 0x33, 0x30, 0x37, 0x2e, 0x34, 0x37, 0x34, 0x37, 0x20,
99                     0x35, 0x36, 0x2e, 0x36, 0x32, 0x37, 0x31, 0x20, 0x72, 0x65,
100                     0x20, 0x73, 0x20, 0x2f, 0x54, 0x78, 0x20, 0x42, 0x4d, 0x43,
101                     0x20, 0x71, 0x20, 0x31, 0x20, 0x31, 0x20, 0x33, 0x30, 0x36,
102                     0x2e, 0x34, 0x37, 0x34, 0x37, 0x20, 0x35, 0x35, 0x2e, 0x36,
103                     0x32, 0x37, 0x31, 0x20, 0x72, 0x65, 0x20, 0x57, 0x20, 0x6e,
104                     0x20, 0x30, 0x20, 0x67, 0x20, 0x42, 0x54, 0x0a, 0x2f, 0x48,
105                     0x65, 0x6c, 0x76, 0x20, 0x31, 0x30, 0x20, 0x54, 0x66, 0x0a,
106                     0x32, 0x20, 0x32, 0x35, 0x2e, 0x31, 0x30, 0x33, 0x35, 0x20,
107                     0x54, 0x64, 0x0a, 0x31, 0x31, 0x2e, 0x35, 0x35, 0x39, 0x39,
108                     0x20, 0x54, 0x4c, 0x0a, 0x28, 0x2d, 0x2d, 0x5c, 0x30, 0x34,
109                     0x30, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x5c, 0x30,
110                     0x34, 0x30, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5c, 0x30, 0x34,
111                     0x30, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x5c, 0x30, 0x34, 0x30,
112                     0x2d, 0x2d, 0x29, 0x20, 0x54, 0x6a, 0x0a, 0x45, 0x54, 0x0a,
113                     0x20, 0x51, 0x20, 0x45, 0x4d, 0x43, (byte)0x8a, 0x0d, 0x0a
114                     };
115         PDFEncryption enc = new PDFEncryption();
116         ByteArrayOutputStream output = new ByteArrayOutputStream();
117         enc.encryptData( 43, 0, key, new ByteArrayInputStream( data ), output );
118
119         byte[] encrypted = output.toByteArray();
120         printHexString( encrypted );
121
122         ByteArrayOutputStream sameAsInput = new ByteArrayOutputStream();
123         enc.encryptData( 43, 0, key, new ByteArrayInputStream( encrypted ), sameAsInput );
124         byte[] dataAgain = sameAsInput.toByteArray();
125         cmpArray( data, dataAgain );
126     }
127
128     /**
129      * This will compare a couple of arrays and fail if they do not match.
130      *
131      * @param firstArray The first array.
132      * @param secondArray The second array.
133      */

134     private void cmpArray( byte[] firstArray, byte[] secondArray )
135     {
136         if( firstArray.length != secondArray.length )
137         {
138             fail( "The array lengths do not match for " +
139                   ", firstArray length was: " + firstArray.length +
140                   ", secondArray length was: " + secondArray.length);
141         }
142
143         for( int i=0; i<firstArray.length; i++ )
144         {
145             if( firstArray[i] != secondArray[i] )
146             {
147                 fail( "Array data does not match " );
148             }
149         }
150     }
151
152     /**
153      * This will print a byte array as a hex string to standard output.
154      *
155      * @param data The array to print.
156      */

157     private void printHexString( byte[] data )
158     {
159         for( int i=0; i<data.length; i++ )
160         {
161             int nextByte = (data[i] + 256)%256;
162             String hexString = Integer.toHexString( nextByte );
163             if( hexString.length() < 2 )
164             {
165                 hexString = "0" + hexString;
166             }
167             System.out.print( hexString );
168             if( i != 0 && (i+1) % 2 == 0 )
169             {
170                 System.out.print( " " );
171             }
172             else if( i!= 0 &&i % 20 == 0 )
173             {
174                 System.out.println();
175             }
176         }
177         System.out.println();
178     }
179 }
Popular Tags