Ver Mensaje Individual
  #13 (permalink)  
Antiguo 22/04/2005, 13:47
Avatar de stock
stock
 
Fecha de Ingreso: junio-2004
Ubicación: Monterrey NL
Mensajes: 2.390
Antigüedad: 19 años, 9 meses
Puntos: 53
Tema: Seguridad
Pregunta: Algun algoritmo para encriptar passwords??
Respuesta: Existen varios algoritmos para encriptar passwords o algun otro texto, el siguiente codigo es el Blowfish, con este podemos encriptar y desencriptar passwords, es muy rapido y seguro unicamente especificamos una KEY, para encriptar, y para desencriptar usamos la misma KEY con la que encriptamos.

Código PHP:


import java
.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.spec.*;

import java.io.*;

public class 
Blowfish {


    public static 
String encriptar(String cleartextString key)
            
throws Exception {
        return 
crypt(cleartextkeyCipher.ENCRYPT_MODE);    
    }

 
    public static 
String desEncriptar(String ciphertextString key)
            
throws Exception {
        return 
crypt(ciphertextkeyCipher.DECRYPT_MODE);    
    }

    private static 
String crypt(String inputString keyint mode
            
throws Exception {

        
// Install SunJCE provider
        
Provider sunJce = new com.sun.crypto.provider.SunJCE();
        
Security.addProvider(sunJce);
         
        
KeyGenerator kgen KeyGenerator.getInstance("Blowfish");
        
kgen.init(448);
        
SecretKey skey kgen.generateKey();

        
byte[] raw key.getBytes();
        
SecretKeySpec skeySpec = new SecretKeySpec(raw"Blowfish");
            
        
Cipher cipher Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        
cipher.init(modeskeySpec);

        
ByteArrayOutputStream bos = new ByteArrayOutputStream();
        
ByteArrayInputStream bis = new ByteArrayInputStream(input.getBytes());
        
CipherOutputStream cos = new CipherOutputStream(boscipher);

        
int length 0;
        
byte[] buffer =  new byte[8192];

        while ((
length bis.read(buffer)) != -1) {
           
cos.write(buffer0length);
        }

        
bis.close();
        
cos.close();

        return 
bos.toString();
    }

Para usarlo unicamente necesitas importar la clase (si esta dentro de un paquete) o poner el archivo en el mismo directorio que tu aplicacion, no necesitas intanciar la clase, pues es STATIC, entonces unicamente harias esto:

String txtEncriptado = Blowfish.encriptar("mipassword","estaeslallave");

Última edición por stock; 28/06/2005 a las 23:15