Seguinte pessoal, não estou conseguindo descriptografar textos que foram criptografados em modos diferentes de ECB. Para quem não sabe há varios modos de criptografia, são eles: ECB, CBC, CFB, OFB e PCBC. Até é possível o java criptografar em qualquer modo, mas pra descriptografar não dá… só em ECB. Queria saber o pq disso já que é pra um trabalho da facul que estaria terminado se não fosse por esse detalhe. Bom aí vai o código do descriptografar:
public Object descriptografar(Key chaveFinal, String textoHexa, String algoritmo, String padding, String modo) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidParameterException
{
janelaPrincipal.addTextoLog("Descriptografando dados...");
byte[] byteTexto = hexaParaByte(textoHexa);
Key chave = chaveFinal;
Cipher cifra = Cipher.getInstance(algoritmo + "/" + modo + "/" + padding);
cifra.init(Cipher.DECRYPT_MODE, chave);
byte[] byteFinal = cifra.doFinal(byteTexto);
ResultadoDescriptografar objResultado = new ResultadoDescriptografar(new String(byteFinal));
return objResultado;
}
public static byte[] hexaParaByte(String h) throws IllegalArgumentException
{
if (h.length() % 2 != 0)
{
throw new IllegalArgumentException();
}
byte[] b = new byte[h.length() / 2];
int j = 0;
for (int i = 0; i < h.length(); i += 2)
{
String doisChar = h.substring(i, i + 2);
b[j] = Integer.decode("0x" + doisChar).byteValue();
j++;
}
return b;
}
Bom, valeu desde já. 