6

I have a PHP servor which decrypt data in 3DES with the CFB Mode

I encrypt in PHP :

$montant = "500";
$message_crypte = mcrypt_encrypt(MCRYPT_3DES, "N4y1FRDRJ7wn7eJNnWaahCIS", $montant, ,CRYPT_MODE_CFB, "NCNPJDcR");
$montant = base64_encode($message_crypte);

This script in PHP is OK with other system.

And I want to encrypt in Java :

public class CryptData {
    private KeySpec keySpec;
    private SecretKey key;
    private IvParameterSpec iv;

    public CryptData(String keyString, String ivString) {
        try {
            final MessageDigest md = MessageDigest.getInstance("md5");

            final byte[] digestOfPassword = md.digest(Base64
                    .decodeBase64(keyString.getBytes("ISO-8859-1")));

            final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
            for (int j = 0, k = 16; j < 8;) {
                keyBytes[k++] = keyBytes[j++];
            }

            //keySpec = new DESedeKeySpec(keyBytes);
            keySpec = new DESedeKeySpec(keyString.getBytes());

            key = SecretKeyFactory.getInstance("DESede")
                    .generateSecret(keySpec);

            iv = new IvParameterSpec(ivString.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String encrypt(String value) {
        try {
            Cipher ecipher = Cipher.getInstance("DESede/CFB/NoPadding");

                    //"SunJCE");
            ecipher.init(Cipher.ENCRYPT_MODE, key, iv);

            if (value == null)
                return null;

            // Encode the string into bytes using utf-8
            byte[] valeur = value.getBytes("ISO-8859-1");
            //byte[] utf8 = value.getBytes();

            // Encrypt
            byte[] enc = ecipher.doFinal(valeur);

            // Encode bytes to base64 to get a string
            return new String(Base64.encodeBase64(enc), "ISO-8859-1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

I have not the same result in PHP and in Java

How modify Java treatment to obtain the same result as PHP?

Zoe
  • 27,060
  • 21
  • 118
  • 148
user1450740
  • 731
  • 10
  • 26
  • It looks like your PHP isn't performing any PKCS#5 padding -- was that just omitted from your sample here or is there no padding being performed on the PHP side? – sarnold Jun 12 '12 at 09:03
  • My PHP script is already working with other sites in PHP or ASP or C #, so I would not change the code, but rather in Java. Do you have an idea? I tested with NoPadding, but it does not work either ... – user1450740 Jun 12 '12 at 09:06
  • This might just be me, but to me it looks like you aren't base64 encoding in the PHP script, so if you decode it at the other end, it will be read as pure gibberish? –  Jun 14 '12 at 09:21
  • I edit my post, and with PHP, I obtain : "N7IM" and with Java, I obtain "N5vL". I modify the padding, but it isn't good. Have you an idea ? – user1450740 Jul 06 '12 at 10:20
  • I have the solution !!!!! Cipher ecipher = Cipher.getInstance("DESede/CFB8/NoPadding"); .... I replace CFB by CFB8 – user1450740 Jul 06 '12 at 10:31
  • 4
    @user1450740 please answer the question so others could see we have a solution here. – Ido Green Jul 17 '12 at 08:41
  • The solution is "Cipher ecipher = Cipher.getInstance("DESede/CFB8/NoPadding");"... CFB8 ! – user1450740 Jul 26 '12 at 14:17
  • 1
    @user1450740 Choose the "Answer Your Question" link at the bottom of the page, post your response then select it as the answer. This will remove it from the unanswered page. – Duncan Jones Jul 31 '12 at 10:38

1 Answers1

2

The answer is:

Cipher ecipher = Cipher.getInstance("DESede/CFB8/NoPadding");

I need to use "CFB8"

Zoe
  • 27,060
  • 21
  • 118
  • 148
user1450740
  • 731
  • 10
  • 26