Tenho os seguintes valores gerados em uma programa feito em C, com criptografia 3DES-EDE-CBC [Standard 1977]
String encText = “sOYQCgaWyDFMLRbFAL%2FCiz%2BI7bG3QujiV1F8oB6EqgI%3D”;
String chave = “5Lf@Y-^2dKwLSM+sNN^WIc+
BOt]?TC”;
Preciso construir minha aplicação, para que seja capaz de “conversar” com este programa em C, usando esta criptografia, ou seja, teremos a chave compartilhada por um canal externo (fora do sistema) que será informada ao programa a cada sessão. A aplicação JAVA deve receber a String criptografa, o usuário informa a chave manualmente, e o programa decodifica a String. No parte em C, funciona da mesma maneira. Não tenho acesso ao fonte em C, apenas a informação quanto ao algorítimo acima.
Penso que o problema está na conversão de String para byte e também, conforme está abaixo, no tamanho da chave, porém, o programa em C, está me informando os valores acima, ou seja, como faço para essa geringonça trabalhar com a chave nesse comprimento informado, e descriptografar a s String?
Estou tentando fazer o seguinte:
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class TripleDESTeste1 {
static String encText = "sOYQCgaWyDFMLRbFAL%2FCiz%2BI7bG3QujiV1F8oB6EqgI%3D";
static String chave = "5Lf@Y-^2dKwLSM+sNN^W`Ic+`BOt]?TC";
public static void main(String[] args) throws Exception {
String decodedtext = new TripleDESTest().decrypt(encText.getBytes());
System.out.println(decodedtext);
}
public byte[] encrypt(String message) throws Exception {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest(chave.getBytes("utf-8"));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
final byte[] plainTextBytes = message.getBytes("utf-8");
final byte[] cipherText = cipher.doFinal(plainTextBytes);
// final String encodedCipherText = new sun.misc.BASE64Encoder()
// .encode(cipherText);
return cipherText;
}
public String decrypt(byte[] message) throws Exception {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest(chave.getBytes("utf-8"));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
// final byte[] encData = new
// sun.misc.BASE64Decoder().decodeBuffer(message);
final byte[] plainText = decipher.doFinal(message);
return new String(plainText, "UTF-8");
}
}
O erro lançado é:
Exception in thread “main” javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*…)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*…)
at com.sun.crypto.provider.DESedeCipher.engineDoFinal(DashoA13*…)
at javax.crypto.Cipher.doFinal(DashoA13*…)
at TripleDES.TripleDESTest.decrypt(TripleDESTest.java:64)
at TripleDES.TripleDESTeste1.main(TripleDESTeste1.java:22)
Java Result: 1