drrqwokuz71031449 2014-03-26 07:48
浏览 48

用于解密通过Java加密的XML的PHP​​代码

I am trying to decrypt XML which is enrypted through Java but no luck... I have tried functions like mcrypt_decrypt and also followed other posts but I am not sure what exact parameters to pass.

Can someone please help me to create PHP function to decrypt this. I have the java code which is encrypting the XML.

private static String encrypt(String encryptionKey, String value) throws Exception{

    //Instantiate the encrypter/decrypter
    String paddedValue = value;
    if ((value.length() % 8) != 0) {
        for (int i= 0; i< (8 - value.length() % 8); i++) {
            paddedValue += ' ';
        }
    }             

    byte[] iv ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
    byte[] valueByteArray = paddedValue.getBytes("UTF8");

    SecretKeySpec desKey = new SecretKeySpec(encryptionKey.getBytes(), "DES");
    Cipher desCipher = Cipher.getInstance("DES/CBC/NoPadding");
    desCipher.init(Cipher.ENCRYPT_MODE, desKey, new IvParameterSpec(iv));

    // Encode the string into bytes using utf-8        
    // Encrypt
    byte[] encryptedBytesArray = desCipher.doFinal(valueByteArray);

    StringBuffer encryptedValue = new StringBuffer(1000000);

    for (int i = 0, n = encryptedBytesArray.length; i < n; i++) {
        String hex = (java.lang.Integer.toHexString(encryptedBytesArray[i] & 0XFF));
        if (hex.length() == 1) {
            encryptedValue.append("0").append(hex);
        } else {
            encryptedValue.append(hex);
        }
    }

    return encryptedValue.toString().toUpperCase();
}

Thank you.

  • 写回答

1条回答 默认 最新

  • dt888999 2014-03-26 10:19
    关注

    You can use mcrypt_decrypt(), using the following arguments:

    mcrypt_decrypt(MCRYPT_DES, $key, hex2bin($data), "cbc");
    

    If you don't have hex2bin() you can use pack():

    mcrypt_decrypt(MCRYPT_DES, $key, pack('H*', $data), "cbc");
    
    评论

报告相同问题?