开发者

Decrypt string in php which was encrypted with aspEncrypt

I need to commun开发者_JAVA技巧icate 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".


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;


I encountered an old VBScript project which was encrypting strings with AspEncrypt like this:

Function EncryptString(data, base64Iv)
    Set CM = Server.CreateObject("Persits.CryptoManager")
    Set Context = CM.OpenContextEx("Microsoft Enhanced RSA and AES Cryptographic Provider", "", True)
    Set Key = Context.GenerateKeyFromPassword("secret encryption password", calgSHA512, calgAES256)

    Set IVblob = CM.CreateBlob
    IVblob.Base64 = base64Iv
    Key.SetIV IVblob

    Set Blob = Key.EncryptText(data)
    EncryptString = Blob.Base64 & ":" & base64Iv
End Function

Based on the arguments to GenerateKeyFromPassword, a binary key is created by hashing the password with SHA-512, and data is encrypted with the aes-256-cbc algorithm. The random Base64-encoded initialization vector is appended to the encrypted value after a colon.

This can be replicated in PHP using the OpenSSL extension:

class Aes256Cbc
{
    private string $algo = 'aes-256-cbc';
    private string $key;
    private int $ivLen;

    public function __construct(string $password)
    {
        $this->key = hash('sha512', $password, true);
        $this->ivLen = openssl_cipher_iv_length($this->algo);
    }

    public function encrypt(string $data): string
    {
        $iv = random_bytes($this->ivLen);
        $ciphertext = openssl_encrypt($data, $this->algo, $this->key, OPENSSL_RAW_DATA, $iv);
        return base64_encode($ciphertext) . ':' . base64_encode($iv);
    }

    public function decrypt(string $encrypted): string
    {
        [$ctPart, $ivPart] = explode(':', $encrypted);
        $iv = base64_decode($ivPart);
        $ciphertext = base64_decode($ctPart);
        return openssl_decrypt($ciphertext, $this->algo, $this->key, OPENSSL_RAW_DATA, $iv);
    }
}

Example usage:

$aes = new Aes256Cbc("secret encryption password");
$decrypted = $aes->decrypt($someValue);

Note: if AspEncrypt was used without setting an initialization vector, the IV will be sequence of null bytes. This fixed IV could be generated in the above PHP class as follows:

$iv = str_repeat("\0", $this->ivLen);


It depends on which cipher it uses, take a look at mcrypt as long as you know the cipher and key it should be easy to decrypt.


If you know the cipher and mode used by the encryption, the function mcrypt_decrypt can decrypt it.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜