Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xpto;
- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.logging.Logger;
- import javax.crypto.Cipher;
- import javax.crypto.CipherInputStream;
- import javax.crypto.CipherOutputStream;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import javax.xml.bind.DatatypeConverter;
- import javax.xml.datatype.DatatypeFactory;
- import org.apache.commons.io.IOUtils;
- /**
- * utilitário de criptografia
- *
- * dę o caminho do arquivo, năo importa muito qual, e ele vai gerar um arquivo
- * de mexmo exato nome, adiionando .cryp no final
- *
- * ex:
- *
- * e: foto1.jpg
- *
- * s: foto1.jpg.cryp
- *
- * a chave de criptografia está no classpath.
- *
- * ou ainda descriptografa, caso o arquivo .cryp gerado tenha sido criado pela
- * mesma chave que este componente conhece.
- *
- * caso năo tenha uma chave, gere uma AES que tá tudo certo.
- *
- * @author leonardo
- *
- */
- public class CryptoUtil {
- private static final Logger LOG = Logger.getLogger("CryptoUtil");
- private static byte[] sKey;
- static {
- try {
- sKey = IOUtils.toByteArray(CryptoUtil.class
- .getResourceAsStream("secret.key"));
- } catch (IOException e) {
- LOG.severe(e.getMessage());
- e.printStackTrace();
- }
- }
- public void genKey() throws Exception {
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
- kgen.init(128);
- SecretKey key = kgen.generateKey();
- Files.write(Paths.get("secret.key"), key.getEncoded());
- }
- public void encrypt(InputStream in, OutputStream out) throws Exception {
- SecretKey key = new SecretKeySpec(sKey, "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE, key);
- try (OutputStream cout = new CipherOutputStream(out, cipher)) {
- IOUtils.copy(in, cout);
- }
- }
- public void decrypt(InputStream in, OutputStream out) throws Exception {
- SecretKey key = new SecretKeySpec(sKey, "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.DECRYPT_MODE, key);
- try (InputStream cin = new CipherInputStream(in, cipher)) {
- IOUtils.copy(cin, out);
- }
- }
- public void encrypt(Path in, Path out) throws Exception {
- try (InputStream fin = Files.newInputStream(in)) {
- try (OutputStream fout = Files.newOutputStream(out)) {
- encrypt(fin, fout);
- }
- }
- }
- public void decrypt(Path in, Path out) throws Exception {
- try (InputStream fin = Files.newInputStream(in)) {
- try (OutputStream fout = Files.newOutputStream(out)) {
- decrypt(fin, fout);
- }
- }
- }
- public void encrypt(String in, String out) throws Exception {
- // if (!out.endsWith(".crypt"))
- // out += ".crypt";
- try (InputStream fin = new FileInputStream(in)) {
- try (OutputStream fout = new FileOutputStream(out)) {
- decrypt(fin, fout);
- }
- }
- }
- public void decrypt(String in, String out) throws Exception {
- // if (!in.endsWith(".crypt"))
- // in += ".crypt";
- try (InputStream fin = new FileInputStream(in)) {
- try (OutputStream fout = new FileOutputStream(out)) {
- decrypt(fin, fout);
- }
- }
- }
- public byte[] encrypt(InputStream in) throws Exception {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- decrypt(in, out);
- return out.toByteArray();
- }
- public byte[] decrypt(InputStream in) throws Exception {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- decrypt(in, out);
- return out.toByteArray();
- }
- public byte[] decrypt(Path pin) throws Exception {
- return decrypt(Files.newInputStream(pin));
- }
- public static void main(String[] args) throws Exception {
- // new CryptoUtil().genKey();
- byte[]b = new CryptoUtil().decrypt(Paths.get("msg.txt"));
- String s = DatatypeConverter.printBase64Binary(b);
- System.out.println(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement