0

so I encrypted a string on my php server.

encrypt("http://google.com", "happy");

function encrypt($str, $key)
{
    $block = mcrypt_get_block_size('des', 'ecb');
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);

    return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
}

btw, this returns some weird string...I was expecting letters and numbers: ZöÞ ÔP»8

Back on Java, I need to decrypt this string with the key.

4 Answers 4

1

This may help for MCrypt in java:

http://www.java2s.com/Open-Source/Java-Document/EJB-Server/resin-4.0.7/com/caucho/quercus/lib/mcrypt/Mcrypt.java.htm

Another link:

http://www.kanatorn.info/2011/11/07/aes-encrypt-decrypt-between-java-php/

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not au fait with mcrypt, but running ASCII plaintext through encryption doesn't always result in an ASCII ciphertext. Chances are your generated encrypted ciphertext is going to translate into a 'weird string' when you try and interpret it as ASCII or unicode text.

Comments

0

first, make sure it isn't an one-way encryption. second, algorithm and arguments used in php and reverse engineering in java

Comments

0

I think this will be useful. Note that the charset is UTF-8.

public class Foo {

    public static void main(String[] args) {
        try {
            String cipherSpec = "DES/ECB/NoPadding";
            Cipher cipher = Cipher.getInstance(cipherSpec);
            int blockSize = cipher.getBlockSize();

            String keyText = "happy";
            Key key = new SecretKeySpec(padRight(keyText, blockSize).getBytes("UTF-8"), "DES");

            String input = "http://google.com";
                   input = padRight(input, input.length() + blockSize - (input.length() % blockSize));

            // encrypt
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] cipherText = cipher.doFinal(input.getBytes(CHARSET));
            System.out.println("\ncipher text: ");
            System.out.println(new String(cipherText, CHARSET));

            // decrypt
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] plainText = cipher.doFinal(cipherText);
            System.out.println("\nplain text: ");
            System.out.println(new String(plainText, CHARSET));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    final static String CHARSET = "UTF-8";

    static String padRight(String s, int n) {
        return String.format("%1$-" + n + "s", s);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.