Skip to content

Instantly share code, notes, and snippets.

@betray32
Created September 27, 2019 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betray32/40ebe6c69b86b66bb0d8e116c021c270 to your computer and use it in GitHub Desktop.
Save betray32/40ebe6c69b86b66bb0d8e116c021c270 to your computer and use it in GitHub Desktop.
AES
package cl.bancoconsorcio.apis.helper;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.log4j.Logger;
/**
* Aes
*
* @author Camilo Contreras
*
*/
public class UtilesAes {
/**
* LOGGER
*/
private static final Logger log = Logger.getLogger(UtilesAes.class);
/**
* Encriptar
*
* @param input
* @param key
* @return
*/
public static String encrypt(String input, String key) {
byte[] crypted = null;
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes());
} catch (Exception e) {
log.error("Error al encriptar, Detalle > ", e);
}
java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
return new String(encoder.encodeToString(crypted));
}
/**
* Desencriptar
*
* @param input
* @param key
* @return
*/
public static String decrypt(String input, String key) {
byte[] output = null;
try {
java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
output = cipher.doFinal(decoder.decode(input));
return new String(output);
} catch (Exception e) {
log.error("Error al desencriptar, Detalle > ", e);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment