Clase para encriptar y desencriptar

Su propio nombre lo dice…para copiar y pegar

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import javax.mail.internet.MimeUtility;

public class UtilsSeguridad {

private final static String keyBuffer = «56af65d2»;

public static byte[] encode(byte[] b) throws Exception {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

OutputStream b64os = MimeUtility.encode(baos, «base64»);

b64os.write(b);

b64os.close();

return baos.toByteArray();

}

public static byte[] decode(byte[] b) throws Exception {

ByteArrayInputStream bais = new ByteArrayInputStream(b);

InputStream b64is = MimeUtility.decode(bais, «base64»);

byte[] tmp = new byte[b.length];

int n = b64is.read(tmp);

byte[] res = new byte[n];

System.arraycopy(tmp, 0, res, 0, n);

return res;

}

private static SecretKeySpec getKey() {

// keyBuffer = keyBuffer.substring(0, 8);

SecretKeySpec key = new SecretKeySpec(keyBuffer.getBytes(), «DES»);

return key;

}

public static String desencripta(String s) throws Exception {

String s1 = null;

if (s.indexOf(«{DES}») != -1) {

String s2 = s.substring(«{DES}».length());

Cipher cipher = Cipher.getInstance(«DES/ECB/PKCS5Padding»);

cipher.init(2, getKey());

byte abyte0[] = cipher.doFinal(decode(s2.getBytes()));

s1 = new String(abyte0);

} else {

s1 = s;

}

return s1;

}

public static String encripta(String s) throws Exception {

byte abyte0[];

SecureRandom securerandom = new SecureRandom();

securerandom.nextBytes(keyBuffer.getBytes());

Cipher cipher = Cipher.getInstance(«DES/ECB/PKCS5Padding»);

cipher.init(1, getKey());

abyte0 = encode(cipher.doFinal(s.getBytes())); // antes

return «{DES}» + new String(abyte0);

}

}

Respuestas

  1. Cambiar icono de tazas de cafe en las ventanas de java…

    Tu post es interesante.. he agregado un Trackback de tu post en mi blog :)…

    1. Gracias. Acabo de ver el tuyo y también lo es 🙂

  2. Hey

    No funciona la clase, arroja el siguiente error

    java.security.InvalidKeyException: Illegal key size or default parameters
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)

    tu sabes que puede ser
    Saludos, buen blog.-

    1. Avatar de oscarmorales86@hotmail.com
      oscarmorales86@hotmail.com

      mmmmm no (especifique el problema)

  3. buen clase , estupendo

  4. Esto es un wordpress con un tema de durpal hablando de java! grotesco!!

  5. Excelente justo lo que buscaba!! gracias!

  6. No funciona…Tengo problemas con la linea: import javax.mail.internet.MimeUtility;
    Hay que descargar una libreria o algo?

  7. mmmta no jalo

  8. Hey there! I’ve been following your blog for a while now and finally got the courage to go ahead and give you a shout out from Kingwood Tx! Just wanted to mention keep up the excellent work!

    1. Hello!
      Sincerely thank your message of support, I hope to continue contributing to the community for a long time!!!

  9. Thanks for sharing your thoughts about application. Regards

Replica a Anonimo Cancelar la respuesta