Pessoal,
Eu criei uma classe de ciptografia usando o Cipher do javax.crypto. Porém eu tenho um problema com a geração da chave. Eu gero as senhas do usuário no meu BD com um programa. Porém quando vou rodar a aplicação na web, tenho que validar a senha do bando e ele não bate, pois as chaves são diferentes e a criptografia muda. Como posso resolver isso?
Segue o código:
import javax.crypto.*;
/**
* Esta classe possui método estáticos para fazer criptografia de dados.
*/
public class Criptografia {
private Cipher encCipher;
private Cipher decCipher;
private static Criptografia instance;
public synchronized static Criptografia getInstance() {
if( instance == null ) instance = new Criptografia();
return instance;
}
private Criptografia() {
try {
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
encCipher = Cipher.getInstance("DES");
decCipher = Cipher.getInstance("DES");
encCipher.init(Cipher.ENCRYPT_MODE, key);
decCipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
e.printStackTrace();
}
}
public String cryptDES( String data ) throws Exception {
try {
// Encode the string into bytes using utf-8
byte[] bytes = data.getBytes();
// Encrypt
byte[] enc = encCipher.doFinal(bytes);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String decryptDES( String encrypted ) throws Exception {
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(encrypted);
// Decrypt
byte[] bytes = decCipher.doFinal(dec);
return new String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
