TRIPLE DES加密/解密使用php [英] TRIPLE DES encryption/decryption using php

查看:250
本文介绍了TRIPLE DES加密/解密使用php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中有这个TRIPLE DES加密代码

I have this TRIPLE DES ENCRYPTION CODE IN PHP

    $encryption_key = "CE51E06875F7D964";
    $data='tokenNo=test&securityCode=111' ;
    echo $desEncryptedData = encryptText_3des($data, $encryption_key);//outputs 3des encrypted data

function encryptText_3des($plainText, $key) {
    $key = hash("md5", $key, TRUE); 
    for ($x=0;$x<8;$x++) {
        $key = $key.substr($key, $x, 1);
    }
    $padded = pkcs5_pad($plainText,
        mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC));
    return $encrypted;
}
 function pkcs5_pad ($text, $blocksize)   
{
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}

我可以将数据加密为 xcFEvIdLXc2fjhG1i4iPOQu5L6ahxwZVucDOPqeMM2E = / code>

Im able to encrypt the data as xcFEvIdLXc2fjhG1i4iPOQu5L6ahxwZVucDOPqeMM2E=

现在我有钥匙,我能否将这些数据解密为纯文本格式?

Now I have the key,will I able to decrypt this data to plain text format?

我以这种方式尝试

            $encryption_key = "CE51E06875F7D964";
        $data='xcFEvIdLXc2fjhG1i4iPOQu5L6ahxwZVucDOPqeMM2E=' ; //encrypted data
        echo $desEncryptedData = encryptText_3des($data, $encryption_key);//outputs 3des encrypted data

    function encryptText_3des($plainText, $key) {
        $key = hash("md5", $key, TRUE); 
        for ($x=0;$x<8;$x++) {
            $key = $key.substr($key, $x, 1);
        }
        $padded = pkcs5_unpad($plainText,
            mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC));
        return $encrypted;
    }

    function pkcs5_unpad($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);
    }

但是我不能这样做。我在做什么是错的?请建议我解密这个方法吗?加密密钥本身是用来解密三重DES中的数据吗?请帮助

But I couldnt do it.IS what I am doing is wrong?Please suggest me a way to decrypt this?Is the encryption key itself is used to decrypt the data in triple DES?Please help

推荐答案

是解决方案:

public function encrypt($data, $secret)
{
    //Generate a key from a hash
    $key = md5(utf8_encode($secret), true);

    //Take first 8 bytes of $key and append them to the end of $key.
    $key .= substr($key, 0, 8);

    //Pad for PKCS7
    $blockSize = mcrypt_get_block_size('tripledes', 'ecb');
    $len = strlen($data);
    $pad = $blockSize - ($len % $blockSize);
    $data .= str_repeat(chr($pad), $pad);

    //Encrypt data
    $encData = mcrypt_encrypt('tripledes', $key, $data, 'ecb');

    return base64_encode($encData);
}

public function decrypt($data, $secret)
{
    //Generate a key from a hash
    $key = md5(utf8_encode($secret), true);

    //Take first 8 bytes of $key and append them to the end of $key.
    $key .= substr($key, 0, 8);

    $data = base64_decode($data);

    $data = mcrypt_decrypt('tripledes', $key, $data, 'ecb');

    $block = mcrypt_get_block_size('tripledes', 'ecb');
    $len = strlen($data);
    $pad = ord($data[$len-1]);

    return substr($data, 0, strlen($data) - $pad);
}

问候。

这篇关于TRIPLE DES加密/解密使用php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆