Skip to content

Instantly share code, notes, and snippets.

@diogok
Created December 10, 2009 19:22
Show Gist options
  • Save diogok/253574 to your computer and use it in GitHub Desktop.
Save diogok/253574 to your computer and use it in GitHub Desktop.
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* @author diogo.souza
*/
public class Enc {
public Enc() {
}
/**
* Gera uma chave criptografica de 128bits aleatória
* @return byte[] key
*/
public byte[] key() throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey key = keyGen.generateKey();
return key.getEncoded();
}
/**
* Encripta uma mensagem usando AES para uma dada chave
* @param byte[] input deve estar em um tamanho multiplo de 16 (nullPadString)
* @param byte[] key
* @return
*/
public byte[] encode(byte[] input, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(input);
return encrypted;
}
/**
* Retorna o valor original de uma mensagem criptgrafada em AES
* @param byte[] input
* @param byte[] key
* @return
*/
public byte[] decode(byte[] input, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(input);
return decrypted;
}
/**
* Converte uma mensagem criptografada para uma string de sua representação hexadecimal.
* @param byte[] hex
* @return String str
*/
public String fromHex(byte[] hex) {
StringBuffer sb = new StringBuffer();
for (int i=0; i < hex.length; i++) {
sb.append( Integer.toString( ( hex[i] & 0xff ) + 0x100, 16).substring( 1 ) );
}
return sb.toString();
}
/**
* Converte uma representação hexadecimal para seus bytes hexadecimal (valor encriptado)
* @param String s
* @return byte[] data
*/
public byte[] toHex(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
/**
* Corrige o tamanho de uma String para multiplo de 16
* @param String original
* @return String final
*/
public String nullPadString(String original) {
StringBuffer output = new StringBuffer(original);
int remain = output.length() % 16;
if (remain != 0) {
remain = 16 - remain;
for (int i = 0; i < remain; i++)
output.append((char) 0);
}
return output.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment