1 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 50 public class TestEncryption extends TestCase 51 { 52 private static Logger log = Logger.getLogger(TestEncryption.class); 53 54 59 public TestEncryption( String name ) 60 { 61 super( name ); 62 } 63 64 69 public static Test suite() 70 { 71 return new TestSuite( TestEncryption.class ); 72 } 73 74 79 public static void main( String[] args ) 80 { 81 String[] arg = {TestEncryption.class.getName() }; 82 junit.textui.TestRunner.main( arg ); 83 } 84 85 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 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 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 |