2012-01-16 122 views
3

我正在開發一個應用程序使用一些存儲在SD卡上的pdf文件的項目。但我需要對這些文件進行加密,以便用戶不能正常使用它們。PDF加密/解密在android中?

有沒有這個問題的任何庫或api。 已經使用了itext,但不適用於android。請幫忙?

回答

4

我用AES寫下面的例子來加密和解密文件。看看這是否有幫助

FileInputStream fis = new FileInputStream(new File("D:/Shashank/Test123.txt")); 
     File outfile = new File("D:/Shashank/encTest1234.txt"); 
     int read; 
     if(!outfile.exists()) 
      outfile.createNewFile(); 
     File decfile = new File("D:/Shashank/dec123.txt"); 
     if(!decfile.exists()) 
      decfile.createNewFile(); 
     FileOutputStream fos = new FileOutputStream(outfile); 
     FileInputStream encfis = new FileInputStream(outfile); 
     FileOutputStream decfos = new FileOutputStream(decfile); 
     Cipher encipher = Cipher.getInstance("AES"); 
     Cipher decipher = Cipher.getInstance("AES"); 
     KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
     SecretKey skey = kgen.generateKey(); 
     encipher.init(Cipher.ENCRYPT_MODE, skey); 
     CipherInputStream cis = new CipherInputStream(fis, encipher); 
     decipher.init(Cipher.DECRYPT_MODE, skey); 
     CipherOutputStream cos = new CipherOutputStream(decfos,decipher); 
     while((read = cis.read())!=-1) 
       { 
        fos.write((char)read); 
        fos.flush(); 
       } 
     fos.close(); 
     while((read=encfis.read())!=-1) 
     { 
      cos.write(read); 
      cos.flush(); 
     } 
    cos.close(); 
+0

「skey」是您的鑰匙...生成一次並將其存儲在某個地方。 – 2012-01-16 08:12:36

+0

它正在使用一個正常的txt文件將這項工作的PDF文件? – Navdroid 2012-01-16 10:47:10

+2

是!它使用輸入/輸出流,它對字節而不是文本起作用。 – 2012-01-16 10:54:00