1 49 package com.lowagie.text.pdf; 50 51 import com.lowagie.text.pdf.crypto.AESCipher; 52 import com.lowagie.text.pdf.crypto.ARCFOUREncryption; 53 54 public class StandardDecryption { 55 protected ARCFOUREncryption arcfour; 56 protected AESCipher cipher; 57 private byte[] key; 58 private static final int AES_128 = 4; 59 private boolean aes; 60 private boolean initiated; 61 private byte[] iv = new byte[16]; 62 private int ivptr; 63 64 65 public StandardDecryption(byte key[], int off, int len, int revision) { 66 aes = revision == AES_128; 67 if (aes) { 68 this.key = new byte[len]; 69 System.arraycopy(key, off, this.key, 0, len); 70 } 71 else { 72 arcfour = new ARCFOUREncryption(); 73 arcfour.prepareARCFOURKey(key, off, len); 74 } 75 } 76 77 public byte[] update(byte[] b, int off, int len) { 78 if (aes) { 79 if (initiated) 80 return cipher.update(b, off, len); 81 else { 82 int left = Math.min(iv.length - ivptr, len); 83 System.arraycopy(b, off, iv, ivptr, left); 84 off += left; 85 len -= left; 86 ivptr += left; 87 if (ivptr == iv.length) { 88 cipher = new AESCipher(false, key, iv); 89 initiated = true; 90 if (len > 0) 91 return cipher.update(b, off, len); 92 } 93 return null; 94 } 95 } 96 else { 97 byte[] b2 = new byte[len]; 98 arcfour.encryptARCFOUR(b, off, len, b2, 0); 99 return b2; 100 } 101 } 102 103 public byte[] finish() { 104 if (aes) { 105 return cipher.doFinal(); 106 } 107 else 108 return null; 109 } 110 } | Popular Tags |