dongren4147 2011-04-15 11:57
浏览 30
已采纳

在php中解密字符串,用aspEncrypt加密

I need to communicate with a asp platform that uses the aspEncrypt from persits. Can anyone provide an example how to decode a string with PHP and mcrypt that was created via the aspEncrypt routines.

An example page of aspEncrypt is available at this link: http://support.persits.com/encrypt/demo_text.asp

So if I use the text "Test" and the key "test" it provides an base64 encoded string. I need a php example that convert this encoded string back to the text "Test" with usage of key "test".

  • 写回答

3条回答 默认 最新

  • douyu3145 2011-04-21 14:37
    关注

    This is how i finally solved it:

    Expectation:

    • Key is known
    • IV is known (in my case, first 32 characters of encoded data)
    • Encrypted Text is known

    In my special case all received data hex encoded. This means IV and encrypted text.

    function decrypt($sString, $sIv, $sKey, $iCipherAlg) {       
       $sDecrypted = mcrypt_decrypt($iCipherAlg, $sKey, $sString, MCRYPT_MODE_CBC, $sIv);
        return trim($sDecrypted);
    }
    
    function hex2bin($sData) {
        $iLen = strlen($sData);
        $sNewData = '';
        for($iCount=0;$iCount<$iLen;$iCount+=2) {
            $sNewData .= pack("C",hexdec(substr($sData,$iCount,2)));
        }
        return $sNewData;
    } 
    
    $sKey = 'this is my key';
    // first 32 chars are IV
    $sIv = hex2bin(substr($sEncodedData, 0, 32));
    $sEncodedData = substr($sEncodedData, 32);
    $sEncodedRaw = hex2bin($sEncodedData);
    $sDecrypted = decrypt($sEncodedRaw, $sIv, $sKey, MCRYPT_RIJNDAEL_128);
    

    A corresponding encryption works like that:

    $sIv = mcrypt_create_iv(mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
    $sKey = 'this is my key';
    $sContent = 'a lot of content';
    $sEncrypted = bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $sKey, $sContent, MCRYPT_MODE_CBC, $sIv));
    $sFullEncodedText = bin2hex($sIv) . $sEncrypted;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码