Re: Triple DES Perl to Java
Alex wrote:
Hi,
I am having a base64 encoded data block, which has been triple des
encrypted with Perl's Crypt::TripleDES class
(http://search.cpan.org/dist/Crypt-TripleDES/lib/Crypt/TripleDES.pm) and
am trying to decrypt it with Java.
My problem is now that whatever I already tried I always get some
exception with messages from a wrong padding to nonexistent algorithms.
Below is some sample code with "O2OsvkIsfY0=" as base64 encoded data
block (decoded "data") and "key" as encryption key.
I just noticed that PBE is apparently not the right algorithm and by
using the following code I at least dont get exceptions anymore, but the
output is not the expected one.
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.BASE64Decoder;
public class Test
{
public static void main(String args[]) throws Exception
{
String data="O2OsvkIsfY0=";
String key="key ";
byte b_data[]=new BASE64Decoder().decodeBuffer(data);
SecretKeyFactory skf=SecretKeyFactory.getInstance("DESEDE");
DESedeKeySpec dks=new DESedeKeySpec(key.getBytes());
SecretKey sk=skf.generateSecret(dks);
Cipher cipher=Cipher.getInstance("DESEDE/ECB/NOPADDING");
cipher.init(Cipher.DECRYPT_MODE, sk);
System.out.println(new String(cipher.doFinal(b_data)));
}
}
Does somebody know where I am wrong? Thanks.