1

我想在 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 文件相关的特殊性?

4

3 回答 3

0

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

于 2013-05-14T17:11:27.083 回答
0

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

于 2013-10-13T03:24:26.913 回答
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加密块也是有问题的。你也需要改变它。请注意,您可以使用CipherOutputStreamandCipherInputStream隐藏加密和写入/读取字节的详细信息。对于这些要求,我实际上会建议这样做。

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

更新:这是使用相同密钥进行加密和解密并利用CipherInputStreamand的代码CipherOutputStream

// 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();
于 2013-05-14T17:09:18.673 回答