简体   繁体   English

用经Java加密的PHP中的DES解密字符串

[英]Decrypt a string with DES in PHP that's been encrypted by Java

I've been banging my head against the wall trying to figure out exactly how to format everything to decrypt this string in PHP that's been encrypted in a custom Java class. 我一直在摸索,试图弄清楚如何格式化所有内容以使用已在自定义Java类中加密的PHP解密此字符串。

Here's the relevent functions from the Java class. 这是Java类中的relevent函数。 The "salt" variable is a class variable byte array set earlier: “ salt”变量是前面设置的类变量字节数组:

public DesEncrypter(String passPhrase) {
    try {
        // Create the key
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt,
                iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
                .generateSecret(keySpec);
        ecipher = Cipher.getInstance(key.getAlgorithm());
        dcipher = Cipher.getInstance(key.getAlgorithm());

        // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt,
                iterationCount);

        // Create the ciphers
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    } catch (java.security.InvalidAlgorithmParameterException e) {
    } catch (java.security.spec.InvalidKeySpecException e) {
    } catch (javax.crypto.NoSuchPaddingException e) {
    } catch (java.security.NoSuchAlgorithmException e) {
    } catch (java.security.InvalidKeyException e) {
    }
}

public String encrypt(String str) {
    try {
        // Encode the string into bytes using utf-8
        byte[] utf8 = str.getBytes("UTF8");

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

        // Encode bytes to base64 to get a string
        return new sun.misc.BASE64Encoder().encode(enc);
    } catch (javax.crypto.BadPaddingException e) {
    } catch (IllegalBlockSizeException e) {
    } catch (UnsupportedEncodingException e) {
    }
    return null;
}

public String decrypt(String str) {
    try {
        // Decode base64 to get bytes
        byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

        // Decrypt
        byte[] utf8 = dcipher.doFinal(dec);

        // Decode using utf-8
        return new String(utf8, "UTF8");
    } catch( Exception ex) {
        ex.printStackTrace(System.err);
    }
    return null;
}

And here's what I have so far in PHP (FYI, I'm using this encryption library in PHP https://github.com/phpseclib/phpseclib ): 这是到目前为止我在PHP中拥有的东西(仅供参考,我正在PHP https://github.com/phpseclib/phpseclib中使用此加密库):

$app->get('/decrypt', function () use ($app) {
    $data = '3aCRLRd3srA/QF4MQb0D+P==';
    $salt = pack('nvc*', 0xB7, 0x9A, 0xC1, 0x34, 0x26, 0x89, 0xW3, 0x30);
    $secret = "secret";
    $keyLength = 16;

    $cipher = new Crypt_DES(CRYPT_DES_MODE_CBC);
    $cipher->setPassword($secret, 'pbkdf2', 'md5', $salt, $keyLength);

    var_dump($cipher->decrypt($data));
});

Right now it's dumping out a bunch of binary, which I've tried base64_decoding, but that doesn't do anything either. 现在,它正在倾倒一堆二进制文件,我已经尝试了base64_decoding,但这也不起作用。

If key.getAlgorithm() is "DES" then you need to provide a fully specified Cipher name like "DES/CBC/PKCS5Padding". 如果key.getAlgorithm()为“ DES”,则需要提供一个完整指定的密码名称,例如“ DES / CBC / PKCS5Padding”。

You will also need to provide the IV if it is non-null. 如果IV不为空,则还需要提供该IV。 Usually the IV is prepended to the ciphertext. 通常,IV在密文之前。

You can get the IV with cipher.getIV() and set with $cipher->setIV('...'); 您可以使用cipher.getIV()获取IV并使用$cipher->setIV('...'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM