Pessoal, alguém pode me ajudar com essa exception, por favor?
Exception:
Exception in thread "main" java.lang.IllegalStateException: Cipher not initialized
at java.base/javax.crypto.Cipher.checkCipherState(Cipher.java:1787)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2194)
at application.EncryptDecrypt.decripta(EncryptDecrypt.java:39)
at application.Main.main(Main.java:67)
EncryptDecrypt:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class EncryptDecrypt {
public SecretKey chave() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
SecretKey chaveDES = keyGenerator.generateKey();
return chaveDES;
}
public Cipher cifra() throws Exception {
Cipher cifraDES = Cipher.getInstance("DES/ECB/PKCS5Padding");
return cifraDES;
}
public byte[] encripta(String string) throws Exception {
cifra().init(Cipher.ENCRYPT_MODE, chave());
byte[] textoPuro = string.getBytes();
byte[] textoEncriptado = cifra().doFinal(textoPuro);
return textoEncriptado;
}
public byte[] decripta(String string) throws Exception {
byte[] stringByte = string.getBytes();
cifra().init(Cipher.DECRYPT_MODE, chave());
byte[] textoDescriptografado = cifra().doFinal(stringByte);
return textoDescriptografado;
}
}
Main:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import api.ServicoDeCep;
import db.DB;
import domain.Endereco;
public class Main {
public static void main(String[] args) throws Exception {
System.out.print("Informe seu CEP: ");
String cep = new Scanner(System.in).nextLine();
Endereco endereco = ServicoDeCep.buscaEnderecoPelo(cep);
String logradouro = endereco.getLogradouro();
String bairro = endereco.getBairro();
String localidade = endereco.getLocalidade();
String uf = endereco.getUf();
int ddd = endereco.getDdd();
EncryptDecrypt ed = new EncryptDecrypt();
// String localidadeEncrypted = new String(ed.encripta(localidade));
Connection conn = null;
ResultSet rs = null;
PreparedStatement st = null;
PreparedStatement st2 = null;
PreparedStatement st3 = null;
PreparedStatement ps = null;
try {
conn = DB.getConnection();
// st = conn.prepareStatement(
// "INSERT INTO cep "
// + "(numero_do_cep) "
// + "VALUES "
// + “(?)”);
// st.setString(1, cep);
// st2 = conn.prepareStatement(
// "INSERT INTO atributosdocep "
// + "(uf, ddd, cidade, bairro, rua, numero, cod_cep) "
// + "VALUES "
// + “(?, ?, ?, ?, ?, ?, ?)”);
//
//
// st2.setString(1, uf);
// st2.setInt(2, ddd);
// st2.setString(3, localidadeEncrypted);
// st2.setString(4, bairro);
// st2.setString(5, logradouro);
// st2.setInt(6, 145);
// st2.setInt(7, 1);
//
ps = conn.prepareStatement(“select * from atributosdocep WHERE (id_atributos = ?)”);
ps.setInt(1, 9);
rs = ps.executeQuery();
while (rs.next()) {
System.out.println("Localidade Descriptografada: " +
new String(ed.decripta(rs.getString(“cidade”))));
}
//
//
// st3 = conn.prepareStatement(
// "UPDATE atributosdocep "
// + "SET cod_cep = ? "
// + "WHERE "
// + “(cod_cep = ?)”);
//
// st3.setInt(1, 3);
// st3.setInt(2, 2);
// int rowsAffected = st.executeUpdate();
// int rowsAffected2 = st2.executeUpdate();
// int rowsAffected3 = st3.executeUpdate();
// System.out.println("Done! Rows affected: " + rowsAffected);
// System.out.println("Done! Rows affected2: " + rowsAffected2);
// System.out.println("Done! Rows affected3: " + rowsAffected3);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DB.closeResultSet(rs);
DB.closeStatement(st);
// DB.closeStatement(st2);
// DB.closeStatement(st3);
DB.closeConnection();
}
}
}
Desculpa, pelo código bagunçado.