package stackoverflow; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.GeneralSecurityException; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; /** * @author TheLima */ public class Q_13271184 { private static final File DESKTOP = new File(System.getProperty("user.home") + "/Desktop"); private static final File BASE_PDF = new File(DESKTOP, "Placas_teste.pdf"); private static final byte[] SALT = { (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,}; private static final char[] KEY = "Just A Key For Testing".toCharArray(); /** * @param args the command line arguments */ public static void main(String[] args) throws GeneralSecurityException, IOException { byte[] content = readFile(BASE_PDF); System.out.println("ORIGINAL: " + new String(content).substring(0, 8)); byte[] encrypted = encrypt(content); System.out.println("ENCRYPTED: " + new String(encrypted).substring(0, 8)); File encryptedFile = new File(DESKTOP, "EncryptedPDF.pdf"); saveFile(encrypted, encryptedFile); content = readFile(encryptedFile); byte[] decrypted = decrypt(content); System.out.println("DECRYPTED: " + new String(decrypted).substring(0, 8)); File decryptedFile = new File(DESKTOP, "DecryptedPDF.pdf"); saveFile(decrypted, decryptedFile); } private static byte[] encrypt(byte[] bytes) throws GeneralSecurityException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(KEY)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); //Encrypt and save to temporary storage. byte[] encrypted = pbeCipher.doFinal(bytes); //Cleanup data-sources - Leave no traces behind. for (int i = 0; i < bytes.length; i++) { bytes[i] = 0; } bytes = null; System.gc(); //Return encryption result. return encrypted; } private static byte[] decrypt(byte[] bytes) throws GeneralSecurityException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(KEY)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return pbeCipher.doFinal(bytes); } private static byte[] readFile(File file) { byte[] content = null; InputStream is; try { is = new FileInputStream(file); BufferedInputStream vf = new BufferedInputStream(is); content = new byte[(file.length() <= Integer.MAX_VALUE) ? (int) file.length() : Integer.MAX_VALUE]; // content = new byte[is.available()]; System.out.printf("file.length() : %d\nis.available() : %d", file.length(), is.available()); vf.read(content); is.close(); } catch (FileNotFoundException ex) { Logger.getLogger(Q_13271184.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Q_13271184.class.getName()).log(Level.SEVERE, null, ex); } return content; } public static void saveFile(byte[] bytes, File file) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); bos.write(bytes); bos.flush(); bos.close(); } }