Javaを使用してPDFを暗号化しようとしています。これまでのところ、他のファイル タイプ (.txt、.png など) を正常に暗号化できます。PDFでそれを行うと、復号化するときに情報が壊れます。
これは私がそれを暗号化するために使用しているものです:
public byte[] cryptograph(Key key, byte[] content){
Cipher cipher;
byte[] cryptograph = null;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
cryptograph = cipher.doFinal(content);
} catch (Exception e) {
e.printStackTrace();
}
return cryptograph;
}
そして、これを解読するには:
public byte[] decrypt(Key key,byte[] textCryp){
Cipher cipher;
byte[] decrypted = null;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
decrypted = cipher.doFinal(textCryp);
} catch (Exception e) {
e.printStackTrace();
}
return decrypted;
}
アップデート:
これは、ファイルを読み取るために使用するものです。
public byte[] getFile(){
byte[] content = null;
try {
InputStream is = new FileInputStream("test.pdf");
BufferedInputStream vf = new BufferedInputStream(is);
content = new byte[vf.available()];
vf.read(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return content;
}
これを使用してファイルを書き換えます
public static void saveDecrypt(byte[] bytes) throws IOException{
Document doc=new Document();
try {
PdfWriter.getInstance(doc,new FileOutputStream("fileDecrypted.pdf"));
doc.open();
doc.add(new Paragraph(new String(bytes)));
doc.close();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}