Ciao a tutti, spero di postare nella sezione giusta ...
Veniamo subito al problema: devo cifrare con des una stringa in vb.net e decifrarla con un programma in java, di seguito il codice che utilizzo:
LATO VB.NET (CIFRO)
Public Function desCrypt(ByVal Clear_Text As String) As String
Dim Cipher_text As String = ""
Dim bytIV As Byte() = {12, 89, 148, 62, 0, 78, 254, 129}
Dim algDES As DES = DES.Create
Dim desEncryptor As ICryptoTransform = algDES.CreateEncryptor((StrToByteArray("abcdefgh") ), bytIV)
Dim bytInput() As Byte
bytInput = UTF8.GetBytes(Clear_Text)
Cipher_text = System.Convert.ToBase64String(desEncryptor.Transfo rmFinalBlock(bytInput, 0, bytInput.Length))
Return Cipher_text
End Function
LATO JAVA (DECIFRO, O ALMENO CI PROVO)
public String Des_decrypt (String cipher_text)
{
String algorithm = "DES";
String recovered ="";
SecretKey key = null;
Cipher cipher = null;
try
{
KeySpec keySpec = new DESKeySpec("abcdefgh".getBytes());
key = SecretKeyFactory.getInstance("DES").generateSecret (keySpec);
cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes = cipher.doFinal(Base64.decodeBase64(cipher_text.get Bytes()));
recovered = recoveredBytes.toString();
}
catch (Exception e) {
e.printStackTrace();
}
return recovered;
}
Ovviamente il tutto non funziona, il messaggio di errore (quando decifro in java) è :"Input length must be multiple of 8 when decrypting with padded cipher"
La riga che causa l'errore è : byte[] recoveredBytes = cipher.doFinal(Base64.decodeBase64(cipher_text.get Bytes()));
Dove Sbaglio ????![]()
Grazie a tutti quello che proveranno a dare una mano !