Advertisement
sombriks

Untitled

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