Advertisement
sombriks

Untitled

Jun 9th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.58 KB | None | 0 0
  1. package br.com.atl.atsmsx.util;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11.  
  12. import javax.crypto.Cipher;
  13. import javax.crypto.CipherInputStream;
  14. import javax.crypto.CipherOutputStream;
  15. import javax.crypto.SecretKey;
  16. import javax.crypto.spec.SecretKeySpec;
  17.  
  18. import org.apache.commons.io.IOUtils;
  19. import org.apache.log4j.Logger;
  20.  
  21. /**
  22.  * utilitário de criptografia
  23.  *
  24.  * dê o caminho do arquivo, não importa muito qual, e ele vai gerar um arquivo
  25.  * de mexmo exato nome, adiionando .cryp no final
  26.  *
  27.  * ex:
  28.  *
  29.  * e: foto1.jpg
  30.  *
  31.  * s: foto1.jpg.cryp
  32.  *
  33.  * a chave de criptografia está no classpath.
  34.  *
  35.  * ou ainda descriptografa, caso o arquivo .cryp gerado tenha sido criado pela
  36.  * mesma chave que este componente conhece.
  37.  *
  38.  * caso não tenha uma chave, gere uma AES que tá tudo certo.
  39.  *
  40.  * @author leonardo
  41.  *
  42.  */
  43. public class CryptoUtil {
  44.  
  45.     private static final Logger LOG = Logger.getLogger(CryptoUtil.class);
  46.  
  47.     private static byte[] sKey;
  48.  
  49.     static {
  50.         try {
  51.             // KeyGenerator kgen = KeyGenerator.getInstance("AES");
  52.             // kgen.init(128);
  53.             // SecretKey key = kgen.generateKey();
  54.             // Files.write(Paths.get("secret.key"), key.getEncoded());
  55.  
  56.             sKey = IOUtils.toByteArray(CryptoUtil.class
  57.                     .getResourceAsStream("secret.key"));
  58.         } catch (IOException e) {
  59.             LOG.fatal(e);
  60.             e.printStackTrace();
  61.         }
  62.     }
  63.  
  64.     public void encrypt(InputStream in, OutputStream out) throws Exception {
  65.         SecretKey key = new SecretKeySpec(sKey, "AES");
  66.         Cipher cipher = Cipher.getInstance("AES");
  67.         cipher.init(Cipher.ENCRYPT_MODE, key);
  68.         try (OutputStream cout = new CipherOutputStream(out, cipher)) {
  69.             IOUtils.copy(in, cout);
  70.         }
  71.     }
  72.  
  73.     public void decrypt(InputStream in, OutputStream out) throws Exception {
  74.         SecretKey key = new SecretKeySpec(sKey, "AES");
  75.         Cipher cipher = Cipher.getInstance("AES");
  76.         cipher.init(Cipher.DECRYPT_MODE, key);
  77.         try (InputStream cin = new CipherInputStream(in, cipher)) {
  78.             IOUtils.copy(cin, out);
  79.         }
  80.     }
  81.  
  82.     public void encrypt(Path in, Path out) throws Exception {
  83.         try (InputStream fin = Files.newInputStream(in)) {
  84.             try (OutputStream fout = Files.newOutputStream(out)) {
  85.                 encrypt(fin, fout);
  86.             }
  87.         }
  88.     }
  89.  
  90.     public void decrypt(Path in, Path out) throws Exception {
  91.         try (InputStream fin = Files.newInputStream(in)) {
  92.             try (OutputStream fout = Files.newOutputStream(out)) {
  93.                 decrypt(fin, fout);
  94.             }
  95.         }
  96.     }
  97.  
  98.     public void encrypt(String in, String out) throws Exception {
  99.         // if (!out.endsWith(".crypt"))
  100.         // out += ".crypt";
  101.         try (InputStream fin = new FileInputStream(in)) {
  102.             try (OutputStream fout = new FileOutputStream(out)) {
  103.                 decrypt(fin, fout);
  104.             }
  105.         }
  106.     }
  107.  
  108.     public void decrypt(String in, String out) throws Exception {
  109.         // if (!in.endsWith(".crypt"))
  110.         // in += ".crypt";
  111.         try (InputStream fin = new FileInputStream(in)) {
  112.             try (OutputStream fout = new FileOutputStream(out)) {
  113.                 decrypt(fin, fout);
  114.             }
  115.         }
  116.     }
  117.  
  118.     public byte[] encrypt(InputStream in) throws Exception {
  119.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  120.         decrypt(in, out);
  121.         return out.toByteArray();
  122.     }
  123.  
  124.     public byte[] decrypt(InputStream in) throws Exception {
  125.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  126.         decrypt(in, out);
  127.         return out.toByteArray();
  128.     }
  129.  
  130.     public byte[] decrypt(Path pin) throws Exception {
  131.         return decrypt(Files.newInputStream(pin));
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement