Advertisement
TheLima

StackOverflow - Q13271184 - Example

Nov 7th, 2012
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. package stackoverflow;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.security.GeneralSecurityException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.crypto.Cipher;
  15. import javax.crypto.SecretKey;
  16. import javax.crypto.SecretKeyFactory;
  17. import javax.crypto.spec.PBEKeySpec;
  18. import javax.crypto.spec.PBEParameterSpec;
  19.  
  20. /**
  21.  * @author TheLima
  22.  */
  23. public class Q_13271184 {
  24.  
  25.     private static final File DESKTOP =
  26.                     new File(System.getProperty("user.home") + "/Desktop");
  27.     private static final File BASE_PDF = new File(DESKTOP, "Placas_teste.pdf");
  28.     private static final byte[] SALT = {
  29.         (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
  30.         (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};
  31.     private static final char[] KEY = "Just A Key For Testing".toCharArray();
  32.  
  33.     /**
  34.      * @param args the command line arguments
  35.      */
  36.     public static void main(String[] args) throws GeneralSecurityException, IOException {
  37.         byte[] content = readFile(BASE_PDF);
  38.         System.out.println("ORIGINAL: " + new String(content).substring(0, 8));
  39.         byte[] encrypted = encrypt(content);
  40.         System.out.println("ENCRYPTED: " + new String(encrypted).substring(0, 8));
  41.         File encryptedFile = new File(DESKTOP, "EncryptedPDF.pdf");
  42.         saveFile(encrypted, encryptedFile);
  43.         content = readFile(encryptedFile);
  44.         byte[] decrypted = decrypt(content);
  45.         System.out.println("DECRYPTED: " + new String(decrypted).substring(0, 8));
  46.         File decryptedFile = new File(DESKTOP, "DecryptedPDF.pdf");
  47.         saveFile(decrypted, decryptedFile);
  48.     }
  49.  
  50.     private static byte[] encrypt(byte[] bytes) throws GeneralSecurityException {
  51.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
  52.         SecretKey key = keyFactory.generateSecret(new PBEKeySpec(KEY));
  53.         Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
  54.         pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
  55.  
  56.         //Encrypt and save to temporary storage.
  57.         byte[] encrypted = pbeCipher.doFinal(bytes);
  58.  
  59.         //Cleanup data-sources - Leave no traces behind.
  60.         for (int i = 0; i < bytes.length; i++) {
  61.             bytes[i] = 0;
  62.         }
  63.         bytes = null;
  64.         System.gc();
  65.  
  66.         //Return encryption result.
  67.         return encrypted;
  68.     }
  69.  
  70.     private static byte[] decrypt(byte[] bytes) throws GeneralSecurityException {
  71.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
  72.         SecretKey key = keyFactory.generateSecret(new PBEKeySpec(KEY));
  73.         Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
  74.         pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
  75.         return pbeCipher.doFinal(bytes);
  76.     }
  77.  
  78.     private static byte[] readFile(File file) {
  79.         byte[] content = null;
  80.         InputStream is;
  81.         try {
  82.             is = new FileInputStream(file);
  83.             BufferedInputStream vf = new BufferedInputStream(is);
  84.             content = new byte[(file.length() <= Integer.MAX_VALUE)
  85.                             ? (int) file.length() : Integer.MAX_VALUE];
  86. //          content = new byte[is.available()];
  87.  
  88.             System.out.printf("file.length() : %d\nis.available() : %d",
  89.                             file.length(), is.available());
  90.  
  91.             vf.read(content);
  92.             is.close();
  93.         } catch (FileNotFoundException ex) {
  94.             Logger.getLogger(Q_13271184.class.getName()).log(Level.SEVERE, null, ex);
  95.         } catch (IOException ex) {
  96.             Logger.getLogger(Q_13271184.class.getName()).log(Level.SEVERE, null, ex);
  97.         }
  98.         return content;
  99.     }
  100.  
  101.     public static void saveFile(byte[] bytes, File file) throws IOException {
  102.         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
  103.         bos.write(bytes);
  104.         bos.flush();
  105.         bos.close();
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement