1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.security.*; import java.util.Base64; public class Java { private static Cipher cipher = null; public static void main(String[] args) throws Exception{ // Clave -> Clave en base 64 // 1234567891234567 -> MTIzNDU2Nzg5MTIzNDU2Nw== // decode the base64 encoded string byte[] decodedKey = Base64.getDecoder().decode("MTIzNDU2Nzg5MTIzNDU2Nw=="); // rebuild key using SecretKeySpec SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); System.out.println("originalKey: " + decodedKey); cipher = Cipher.getInstance("AES"); String clearText = "hola"; byte[] clearTextBytes = clearText.getBytes("UTF8"); System.out.println("Texto en claro: " + clearText); cipher.init(Cipher.ENCRYPT_MODE, originalKey); byte[] cipherBytes = cipher.doFinal(clearTextBytes); String cipherText = new String(cipherBytes, "UTF8"); System.out.println("Texto cifrado: " + cipherText); cipher.init(Cipher.DECRYPT_MODE, originalKey); byte[] decryptedBytes = cipher.doFinal(cipherBytes); String decryptedText = new String(decryptedBytes, "UTF8"); System.out.println("Texto descifrado: " + decryptedText); } } |