1 49 package com.lowagie.text.pdf.crypto; 50 51 import org.bouncycastle.crypto.BlockCipher; 52 import org.bouncycastle.crypto.engines.AESFastEngine; 53 import org.bouncycastle.crypto.modes.CBCBlockCipher; 54 import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; 55 import org.bouncycastle.crypto.params.KeyParameter; 56 import org.bouncycastle.crypto.params.ParametersWithIV; 57 58 62 public class AESCipher { 63 private PaddedBufferedBlockCipher bp; 64 65 66 public AESCipher(boolean forEncryption, byte[] key, byte[] iv) { 67 BlockCipher aes = new AESFastEngine(); 68 BlockCipher cbc = new CBCBlockCipher(aes); 69 bp = new PaddedBufferedBlockCipher(cbc); 70 KeyParameter kp = new KeyParameter(key); 71 ParametersWithIV piv = new ParametersWithIV(kp, iv); 72 bp.init(forEncryption, piv); 73 } 74 75 public byte[] update(byte[] inp, int inpOff, int inpLen) { 76 int neededLen = bp.getUpdateOutputSize(inpLen); 77 byte[] outp = null; 78 if (neededLen > 0) 79 outp = new byte[neededLen]; 80 else 81 neededLen = 0; 82 bp.processBytes(inp, inpOff, inpLen, outp, 0); 83 return outp; 84 } 85 86 public byte[] doFinal() { 87 int neededLen = bp.getOutputSize(0); 88 byte[] outp = new byte[neededLen]; 89 int n = 0; 90 try { 91 n = bp.doFinal(outp, 0); 92 } catch (Exception ex) { 93 return outp; 94 } 95 if (n != outp.length) { 96 byte[] outp2 = new byte[n]; 97 System.arraycopy(outp, 0, outp2, 0, n); 98 return outp2; 99 } 100 else 101 return outp; 102 } 103 104 } 105 | Popular Tags |