【问题标题】:which is the equivalent encrypt/decrypt function of this in php?这是 php 中 this 的等效加密/解密函数?
【发布时间】:2016-05-31 21:32:06
【问题描述】:

我在 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");
}

【问题讨论】:

  • Mcrypt librarymcrypt-encrypt/mcrypt-decrypt 之类的东西应该可以完成这项工作。您可能还需要pack 来处理二进制数据。
  • 最好不要使用mcrypt,它是废弃软件,多年未更新,不支持标准PKCS#7(née PKCS#5)填充,只有非标准空填充可以'甚至不能与二进制数据一起使用。 mcrypt 有许多出色的 bugs 可以追溯到 2003 年。请考虑使用 defuse,它正在维护并且是正确的。
  • @AlexBlex 请注意,OP 的代码指定 PKCS5Padding 而 mcrypt 不支持 PKCS5Padding
  • @seba123neo DES 不安全,它只使用 56 位密钥,尽可能不使用它。它已被 AES(高级加密标准)取代。
  • @zaph 我总是为你感到难过。您几乎必须在每个带有加密标签的问题上重复自己,而且似乎 OP 从不听。继续加油!

标签: java php android encryption


【解决方案1】:

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

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);
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 2018-07-04
    • 1970-01-01
    • 2014-04-21
    • 2012-01-21
    • 2020-12-31
    • 2018-12-30
    • 1970-01-01
    • 2013-05-20
    相关资源
    最近更新 更多