Android Open Source - RadaeePDF-B4A P D F A E S Enc






From Project

Back to project page RadaeePDF-B4A.

License

The source code is released under:

Apache License

If you think the Android project RadaeePDF-B4A listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.radaee.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
//from www  . j a v  a 2s  .c o  m
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * encrypt PDF file class.
 * @author radaee
 *
 */
public class PDFAESEnc
{
  static private byte[] process_block( Cipher cipher, byte[] data, int len )
  {
    try
    {
      return cipher.doFinal(data, 0, len);
    }
    catch(Exception e)
    {
      return null;
    }
  }
  static private void write_int(byte[] data, int off, int val)
  {
    data[off + 3] = (byte)(val&0xFF);
    data[off + 2] = (byte)((val>>8)&0xFF);
    data[off + 1] = (byte)((val>>16)&0xFF);
    data[off + 0] = (byte)((val>>24)&0xFF);
  }
  /**
   * encrypt PDF file
   * @param src_file normal PDF file.
   * @param dst_file encrypted PDF file.
   * @param key key to encrypt, must 16 bytes, or 24 bytes, or 32 bytes
   * @return true or false
   */
  static public boolean encrypt( String src_file, String dst_file, byte[] key )
  {
    try
    {
      File ifile = new File(src_file);
      long total = ifile.length();
      int secs_cnt = (int)((total + 4095)/4096);
      byte[] header = new byte[(secs_cnt + 2) * 8];
      header[0] = 'R';
      header[1] = 'D';
      header[2] = 'A';
      header[3] = 'E';
      header[4] = 'S';
      header[5] = 0;
      header[6] = 0;
      header[7] = 0;
      write_int(header, 8, secs_cnt);
      write_int(header, 12, (int)total);

      FileInputStream is = new FileInputStream(new File(src_file));
      File ofile = new File(dst_file);
      ofile.createNewFile();
      RandomAccessFile os = new RandomAccessFile(dst_file, "rw");
      os.write(header);

      SecretKeySpec skey = new SecretKeySpec(key, "AES");  
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
      byte[] ivbytes = new byte[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
      IvParameterSpec iv = new IvParameterSpec(ivbytes);//need IV in CBC mode
      cipher.init(Cipher.ENCRYPT_MODE, skey, iv);

      byte[] src = new byte[4096];
      int read = 0;
      int header_off = 16;
      int off = header.length;
      while( (read = is.read(src)) > 0 )
      {
        //there are some bytes for padding.
        //so, dst.length always great than src.length.
        byte[] dst = process_block(cipher, src, read);
        os.write(dst);
        int len = dst.length;
        write_int(header, header_off, off);
        write_int(header, header_off + 4, len);
        header_off += 8;
        off += len;
      }
      is.close();
      os.seek(0);
      os.write(header);
      os.close();
      return true;
    }
    catch(Exception e)
    {
      return false;
    }
  }
}




Java Source Code List

com.example.pdfhttpdemo.MainActivity.java
com.example.pdfhttpdemo.ReaderController.java
com.radaee.pdf.BMDatabase.java
com.radaee.pdf.BMDatabase.java
com.radaee.pdf.BMDatabase.java
com.radaee.pdf.Document.java
com.radaee.pdf.Document.java
com.radaee.pdf.Document.java
com.radaee.pdf.Global.java
com.radaee.pdf.Global.java
com.radaee.pdf.Global.java
com.radaee.pdf.HWriting.java
com.radaee.pdf.HWriting.java
com.radaee.pdf.HWriting.java
com.radaee.pdf.Ink.java
com.radaee.pdf.Ink.java
com.radaee.pdf.Ink.java
com.radaee.pdf.Matrix.java
com.radaee.pdf.Matrix.java
com.radaee.pdf.Matrix.java
com.radaee.pdf.PDFHttpStream.java
com.radaee.pdf.PageContent.java
com.radaee.pdf.PageContent.java
com.radaee.pdf.PageContent.java
com.radaee.pdf.Page.java
com.radaee.pdf.Page.java
com.radaee.pdf.Page.java
com.radaee.pdf.Path.java
com.radaee.pdf.Path.java
com.radaee.pdf.Path.java
com.radaee.reader.PDFCropAct.java
com.radaee.reader.PDFCrop.java
com.radaee.reader.PDFEncAct.java
com.radaee.reader.PDFInkAct.java
com.radaee.reader.PDFInk.java
com.radaee.reader.PDFReaderAct.java
com.radaee.reader.PDFReaderOldAct.java
com.radaee.reader.PDFReaderOld.java
com.radaee.reader.PDFReader.java
com.radaee.reader.PDFSimpleAct.java
com.radaee.reader.PDFSimple.java
com.radaee.reader.PDFTestAct.java
com.radaee.reader.ReaderActivity.java
com.radaee.reader.ReaderController.java
com.radaee.reader.ReaderController.java
com.radaee.util.ComboListAdt.java
com.radaee.util.ComboList.java
com.radaee.util.PDFAESEnc.java
com.radaee.util.PDFAESEnc.java
com.radaee.util.PDFAESStream.java
com.radaee.util.PDFAESStream.java
com.radaee.util.PDFAssetStream.java
com.radaee.util.PDFAssetStream.java
com.radaee.util.PDFFileStream.java
com.radaee.util.PDFFileStream.java
com.radaee.util.PDFGridAdt.java
com.radaee.util.PDFGridItem.java
com.radaee.util.PDFGridThread.java
com.radaee.util.PDFGridView.java
com.radaee.util.PDFHttpStream.java
com.radaee.util.PDFHttpStream.java
com.radaee.util.PDFMemStream.java
com.radaee.util.PDFMemStream.java
com.radaee.util.PDFThumbView.java
com.radaee.util.SnatchAdt.java
com.radaee.util.SnatchView.java
com.rootsoft.pdfviewer.main.java
com.rootsoft.pdfviewer.readercontroller.java
com.rootsoft.rspdfviewer.pdf.RSPDFAnnotation.java
com.rootsoft.rspdfviewer.pdf.RSPDFBMDatabase.java
com.rootsoft.rspdfviewer.pdf.RSPDFDocument.java
com.rootsoft.rspdfviewer.pdf.RSPDFGlobal.java
com.rootsoft.rspdfviewer.pdf.RSPDFHWriting.java
com.rootsoft.rspdfviewer.pdf.RSPDFInk.java
com.rootsoft.rspdfviewer.pdf.RSPDFMatrix.java
com.rootsoft.rspdfviewer.pdf.RSPDFPageContent.java
com.rootsoft.rspdfviewer.pdf.RSPDFPage.java
com.rootsoft.rspdfviewer.pdf.RSPDFPath.java
com.rootsoft.rspdfviewer.pdf.RSPDFTemplate.java
com.rootsoft.rspdfviewer.pdf.RSPDFVPage.java
com.rootsoft.rspdfviewer.pdf.RSPDFViewCurl.java
com.rootsoft.rspdfviewer.pdf.RSPDFViewDual.java
com.rootsoft.rspdfviewer.pdf.RSPDFViewHorz.java
com.rootsoft.rspdfviewer.pdf.RSPDFViewThumb.java
com.rootsoft.rspdfviewer.pdf.RSPDFViewVert.java
com.rootsoft.rspdfviewer.pdf.RSPDFView.java
com.rootsoft.rspdfviewer.pdf.RSReaderView.java