douping3860 2010-11-12 23:57
浏览 79
已采纳

加密和解密后字符串不再相等

I use the following class to encrypt and decrypt a string. After creating two identical strings, I encrypt one of the strings and then decrypt it. However, the decrypted string is no longer equal to its twin (even though they look identical in text form after the conversion) . Also, after taking the encrypt-decrypted string and its twin and convert them to hex use bin2hex, I find out that they look similar only that the string that got encrypted before has an addition number of zero at the end.

Can someone point out what I did wrong? Thank you in advance.

class proCrypt {

public function __set( $name, $value )
{
    switch( $name)
    {
        case 'key':
        case 'ivs':
        case 'iv':
        $this->$name = $value;
        break;

        default:
        throw new Exception( "$name cannot be set" );
    }
}

/**
*
* Gettor - This is called when an non existant variable is called
*
* @access    public
* @param    string    $name
*
*/
public function __get( $name )
{
    switch( $name )
    {
        case 'key':
        return 'abcd';

        case 'ivs':
        return mcrypt_get_iv_size( MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB );

        case 'iv':
        return mcrypt_create_iv( $this->ivs );

        default:
        throw new Exception( "$name cannot be called" );
    }
}

/**
*
* Encrypt a string
*
* @access    public
* @param    string    $text
* @return    string    The encrypted string
*
*/
public function encrypt( $text )
{
    // add end of text delimiter
    $data = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv );
    return bin2hex($data);
}

/**
*
* Decrypt a string
*
* @access    public
* @param    string    $text
* @return    string    The decrypted string
*
*/
public function decrypt( $text )
{
    $text = pack("H*" , $text);
    return mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv );
}

} // end of class

  • 写回答

2条回答 默认 最新

  • douba3378 2010-11-13 00:19
    关注

    You've got a 16-length string padded with spaces, presumably. Try trim() before bin2hex() to get rid of the leading and trailing spaces.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入