【问题标题】:Decrypt string in php which was encrypted with aspEncrypt解密用aspEncrypt加密的php中的字符串
【发布时间】:2011-08-06 06:56:43
【问题描述】:

我需要与使用 persits 的 aspEncrypt 的 asp 平台通信。 谁能提供一个示例,如何使用 PHP 和 mcrypt 解码通过 aspEncrypt 例程创建的字符串。

此链接提供了 aspEncrypt 的示例页面: http://support.persits.com/encrypt/demo_text.asp

因此,如果我使用文本“Test”和键“test”,它会提供一个 base64 编码的字符串。我需要一个 php 示例,使用键“test”将此编码字符串转换回文本“Test”。

【问题讨论】:

  • asp 脚本使用哪种密码?
  • 它使用 RC2,但您可以选择它应该是哪种密码。但无论我使用哪种密码,它都不起作用。

标签: php mcrypt encryption


【解决方案1】:

这取决于它使用的密码,看看mcrypt,只要你知道它应该很容易解密的密码和密钥。

【讨论】:

  • 没有机会。自己试试吧。打开链接,输入密钥和文本,选择密码并尝试使用 php 解密结果
【解决方案2】:

如果你知道加密使用的密码和模式,函数mcrypt_decrypt可以解密。

http://uk3.php.net/manual/en/function.mcrypt-decrypt.php

【讨论】:

  • 我知道所有必要的东西(密钥、密码等),但似乎 aspencrypt 创建的密钥有所不同。有没有人使用过这个特殊的 aspEncrypt 模块?
【解决方案3】:

这就是我最终解决的方法:

期望:

  • 密钥已知
  • IV 是已知的(在我的例子中,编码数据的前 32 个字符)
  • 加密文本已知

在我的特殊情况下,所有接收到的数据都是十六进制编码的。 这意味着 IV 和加密文本。

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

相应的加密是这样工作的:

$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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2018-05-19
    • 2019-06-29
    相关资源
    最近更新 更多