使用Java加密/解密pdf文件

时间:2013-05-14 16:02:25

标签: java android encryption

我想在Android上加密/解密pdf文件(但它是一个常见的java问题)

我有这个代码来生成我的密钥:

 public static byte[] getRawKey(byte[] seed) throws Exception {
     KeyGenerator kgen = KeyGenerator.getInstance("AES");
     SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
     sr.setSeed(seed);
     kgen.init(128, sr);
     SecretKey skey = kgen.generateKey();
     byte[] raw = skey.getEncoded();
     return raw;
 }

我的代码写加密文件:

 inStream = new BufferedInputStream(conn.getInputStream());
 outFile = new File(path + fileName);
 outStream = new BufferedOutputStream(new FileOutputStream(outFile), 4096);
 byte[] data = new byte[4096];
 String seed = "password";
 byte[] rawKey = Utils.getRawKey(seed.getBytes());
 SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
 Cipher cipher = Cipher.getInstance("AES");
 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
 int bytesRead = 0;
 while((bytesRead = inStream.read(data, 0, data.length)) >= 0)
 {
      outStream.write(cipher.doFinal(data),0, bytesRead);
 }
 outStream.flush();
 outStream.close();  
 inStream.close();

我的代码解密它(并将其保存到新的解密文件中):

  FileInputStream fis = new FileInputStream(file);
  FileOutputStream fos = new FileOutputStream(tmp_file);
  String seed = "password";
  byte[] rawKey = Utils.getRawKey(seed.getBytes());
  SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
  Cipher cipher = Cipher.getInstance("AES");
  cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  int b;
  byte[] data = new byte[4096];
  while((b = fis.read(data)) != -1) {
       fos.write(cipher.doFinal(data), 0, b);
  }
 fos.flush();
 fos.close();
 fis.close();

我在stackoverflow上阅读了很多内容,并尝试按照说明操作,但这不起作用,我收到了这个错误:

 javax.crypto.BadPaddingException: pad block corrupted at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCi

我做错了什么?是否有与pdf文件相关的特异性?

3 个答案:

答案 0 :(得分:0)

尝试更改解密以使用此功能:

int encryptedCount;
final byte[] decryptedData = new byte[4096];
final byte[] encryptedData = new byte[4096];
while ((encryptedCount = fis.read(encryptedData)) != -1) {
    final int decryptedCount = cipher.update(encryptedData, 0, encryptedCount, decryptedData);
    fos.write(decryptedData, 0, decryptedCount);
}
fos.write(cipher.doFinal());

同样,在加密块中调用doFinal会有问题。你也需要改变它。请注意,您可以使用CipherOutputStreamCipherInputStream来隐藏加密和写入/读取字节的详细信息。我实际上会建议这些要求。

此外,我不认为将SecureRandom"password"播种会产生您想要的预期效果。我认为这也可能是您的问题的根源,因为您需要确保加密和解密使用相同的密钥。

更新:以下代码使用相同的密钥进行加密和解密,并使用CipherInputStreamCipherOutputStream

// get the key
final KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(128);
final SecretKey secretKey = generator.generateKey();

// perform encryption
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
FileInputStream fis = new FileInputStream(System.getProperty("user.home") + java.io.File.separatorChar + "plain.pdf");
FileOutputStream fos = new FileOutputStream(System.getProperty("user.home") + java.io.File.separatorChar + "test.enc");
final CipherOutputStream output = new CipherOutputStream(fos, cipher);

int bytesRead = 0;
final byte[] plainText = new byte[4096];
while ((bytesRead = fis.read(plainText)) >= 0) {
    output.write(plainText, 0, bytesRead);
}
output.flush();
output.close();
fos.close();
fis.close();
final byte[] iv = cipher.getIV();

// decrypt the file
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
fis = new FileInputStream(System.getProperty("user.home") + java.io.File.separatorChar + "test.enc");
fos = new FileOutputStream(System.getProperty("user.home") + java.io.File.separatorChar + "test.pdf");
final CipherInputStream input = new CipherInputStream(fis, cipher);

final byte[] decryptedData = new byte[4096];
int decryptedRead;
while ((decryptedRead = input.read(decryptedData)) >= 0) {
    fos.write(decryptedData, 0, decryptedRead);
}
fos.flush();
fos.close();
input.close();
fis.close();

答案 1 :(得分:0)

你不应该在你的循环中调用cipher.doFinal。 而是调用cipher.update(data,...) 然后在循环完成后调用cipher.doFinal()。 否则你只处理最后一个块。

答案 2 :(得分:0)

您必须使用密码(在keyspec中),字符长度仅为16,24,32 。祝你好运