Java 在php中,该函数的等效加密/解密函数是什么?

Java 在php中,该函数的等效加密/解密函数是什么?,java,php,android,encryption,Java,Php,Android,Encryption,我在java中有这段代码,我需要PHP中的等效代码,我在.NET中也有这段代码,并且工作得非常完美,但我需要PHP public static String decrypt(String pValor) throws UnsupportedEncodingException { byte vBytesDecodificados[] = null; try { KeySpec vClave = new DESKeySpec("MyKey".getBytes("

我在java中有这段代码,我需要PHP中的等效代码,我在.NET中也有这段代码,并且工作得非常完美,但我需要PHP

public static String decrypt(String pValor) throws UnsupportedEncodingException {

    byte vBytesDecodificados[] = null;

    try {

        KeySpec vClave = new DESKeySpec("MyKey".getBytes("UTF-8"));
        SecretKey vClaveSecreta = SecretKeyFactory.getInstance("DES").generateSecret(vClave);

        IvParameterSpec iv = new IvParameterSpec(Hex.decodeHex("1234567890ABCDEF".toCharArray()));

        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, vClaveSecreta, iv);

        vBytesDecodificados = cipher.doFinal(Base64.decodeBase64(pValor.getBytes()));

    } catch (Exception e) {

    }

    return new String(vBytesDecodificados, "UTF-8");
}

public static String encrypt(String pValor) throws UnsupportedEncodingException {

    byte vBytesCodificados[] = null;

    try {

        KeySpec vClave = new DESKeySpec("MyKey".getBytes("UTF-8"));
        SecretKey vClaveSecreta = SecretKeyFactory.getInstance("DES").generateSecret(vClave);

        IvParameterSpec iv = new IvParameterSpec(Hex.decodeHex("1234567890ABCDEF".toCharArray()));

        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, vClaveSecreta, iv);

        byte[] utf8 = pValor.getBytes("UTF8");
        byte[] enc = cipher.doFinal(utf8);
        vBytesCodificados = Base64.encodeBase64(enc);

    } catch (Exception e) {

    }

    return new String(vBytesCodificados, "UTF-8");
}

我已经知道使用这种方法不安全,但这是解决方案:

class Encriptacion {
    private $iv = '1234567890ABCDEF';
    private $key = 'MyKey';

    function encode($str) {
        $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
        $str = $this->pkcs5Pad($str, $size);
        $aaa = mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->hex2bin($this->iv));
        $ret = base64_encode($aaa);
        return $ret;
    }

    function decode($str) {
        $str = base64_decode($str);
        $str = mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_DECRYPT, $this->hex2bin($this->iv));
        $str = $this->pkcs5Unpad($str);
        return $str;
    }

    function hex2bin($hexData) {
        $binData = "";
        for ($i = 0; $i < strlen($hexData); $i += 2) {
            $binData .= chr(hexdec(substr($hexData, $i, 2)));
        }
        return $binData;
    }

    function pkcs5Pad($text, $blocksize) {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    function pkcs5Unpad($text) {
        $pad = ord($text {strlen($text) - 1});
        if ($pad > strlen($text))
            return false;

        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
            return false;

        return substr($text, 0, - 1 * $pad);
    }
}
类加密{
私人$iv='1234567890ABCDEF';
private$key='MyKey';
函数编码($str){
$size=mcrypt\u get\u block\u size(mcrypt\u DES,mcrypt\u MODE\u CBC);
$str=$this->pkcs5Pad($str,$size);
$aaa=mcrypt_cbc(mcrypt_DES,$this->key,$str,mcrypt_ENCRYPT,$this->hex2bin($this->iv));
$ret=base64_编码($aaa);
返回$ret;
}
函数解码($str){
$str=base64_解码($str);
$str=mcrypt_cbc(mcrypt_DES,$this->key,$str,mcrypt_DECRYPT,$this->hex2bin($this->iv));
$str=$this->pkcs5Unpad($str);
返回$str;
}
函数hex2bin($hexData){
$binData=“”;
对于($i=0;$istrlen($text))
返回false;
if(strspn($text,chr($pad),strlen($text)-$pad)!=pad)
返回false;
返回substr($text,0,-1*$pad);
}
}

看一看。应该做这项工作。您可能还需要处理二进制数据。最好不要使用mcrypt,它是弃用软件,多年来未更新,不支持标准PKCS#7(née PKCS#5)填充,仅支持非标准的空填充,甚至不能用于二进制数据。mcrypt在2003年就有很多杰出的作品。。AcExbLeX注意到OP的代码指定了<代码> PKCS5pAd/<代码>,McLIPT不支持<代码> PKCS5pAd/<代码>。@ SEBA123NEXDES不安全,它只使用56位密钥,如果可能的话,不应该使用它。它已被AES(高级加密标准)取代。@zaph我一直为你感到难过。你必须在几乎每一个加密标记的问题上重复你自己,而且似乎OP总是不听。坚持下去!