Criptografia PBE com MD5 e DES em PHP

1 resposta
T

Bom dia pessoal.

Temos um algoritmo em Java para fazer criptografia e descriptografia com PBE com MD5 e DES. O algoritmo funciona bem, porém precisamos de um algoritmo em PHP que faça a descriptografia de uma informação criptografada com o algoritmo Java.

Já procuramos bastante na internet mas não conseguimos encontrar nada que funcionasse nesse caso. Alguém conhece alguma forma de fazer isso em PHP?

Segue nossa classe de criptografia:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class CriptografiaChave {
	private static SecretKey skey;
	private static KeySpec ks;
	private static PBEParameterSpec ps;
	private static final String algorithm = "PBEWithMD5AndDES";
	private static BASE64Encoder enc = new BASE64Encoder();
	private static BASE64Decoder dec = new BASE64Decoder();

	public CriptografiaChave(String caminhoChave) {
		try {
			SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);

			ps = new PBEParameterSpec(new byte[] { 5, 6, 3, 7, 9, 9, 3, 4 }, 10);
			ks = new PBEKeySpec((LeituraReg(caminhoChave)).toCharArray());
			skey = skf.generateSecret(ks);
			
		} catch (java.security.NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		
		} catch (java.security.spec.InvalidKeySpecException ex) {
			ex.printStackTrace();
		
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public String encrypt(final String text) {
		Cipher cipher = null;
		String retorno = null;
		
		try {
	        cipher = Cipher.getInstance(algorithm);
        
		} catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
        
		} catch (NoSuchPaddingException e) {
	        e.printStackTrace();
        }
        
        
		try {
	        cipher.init(Cipher.ENCRYPT_MODE, skey, ps);
        
		} catch (InvalidKeyException e) {
	        e.printStackTrace();
        
		} catch (InvalidAlgorithmParameterException e) {
	        e.printStackTrace();
        }
        
        
		try {
			retorno = enc.encode(cipher.doFinal(text.getBytes()));
        
		} catch (IllegalBlockSizeException e) {
	        e.printStackTrace();
        
		} catch (BadPaddingException e) {
	        e.printStackTrace();
        }
        
		return retorno;
	}

	public String decrypt(final String text) {
		String ret = null;
		Cipher cipher = null;
		
        try {
	        cipher = Cipher.getInstance(algorithm);
        
        } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
        
        } catch (NoSuchPaddingException e) {
	        e.printStackTrace();
        }
        
        
		try {
	        cipher.init(Cipher.DECRYPT_MODE, skey, ps);
        
		} catch (InvalidKeyException e) {
	        e.printStackTrace();
        
		} catch (InvalidAlgorithmParameterException e) {
	        e.printStackTrace();
        }
        
		
		try {
			ret = new String(cipher.doFinal(dec.decodeBuffer(text)));
		
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return ret;
	}

	public String LeituraReg(String url) throws IOException {
		FileReader reader = new FileReader(url);
		BufferedReader leitor = new BufferedReader(reader);
		String linha = leitor.readLine();

		return linha;
	}
}

1 Resposta

E

se me lembro bem, MD5 não descriptografa.

Criado 23 de dezembro de 2011
Ultima resposta 23 de dez. de 2011
Respostas 1
Participantes 2