Search code examples
phpblenc

Error validation the encoded script


I'm trying this

<?php
    /* read the PHP source code */
    $source_code = file_get_contents("hello.php");

    $source_code = preg_replace('#^<\?php\s+#', '', $source_code);
    $source_code = preg_replace('#\s+\?>\s*$#', '', $source_code);

    /* create the encrypted version */
    $redistributable_key = blenc_encrypt($source_code, "encrypt.php", "my_fixed_password");

    $key_file = __DIR__ ."\keys"; 

    file_put_contents($key_file, $redistributable_key . "\n", FILE_APPEND);

    include 'encrypt.php';
    echo $hello;
?>

hello.php

<?php 

$hello = "Ciao";

I got this error

PHP Fatal error:  blenc_compile: Validation of script 
'encrypt.php' failed, cannot execute.

Please note that:

  1. The key file is created, I'm already using the '\n' fix
  2. I replaced <?php and ?> because another Stack Overflow question told me that it's a problem

Solution

  • <?php
        $file_name = basename($file);
    
        $unencrypted_key = = md5(time());
    
        $source_code = file_get_contents($file);
    
        //This covers old-asp tags, php short-tags, php echo tags, and normal php tags.
        $contents = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $source_code);
    
        $html .= "<br> BLENC blowfish unencrypted key: $unencrypted_key" . PHP_EOL;
        $html .= "<br> BLENC file to encode: " . $file_name . PHP_EOL;
    
        //file_put_contents('blencode-log', "---\nFILE: $file_name\nSIZE: ".strlen($contents)."\nMD5: ".md5($contents)."\n", FILE_APPEND);
    
        $redistributable_key = blenc_encrypt($contents, TARGET_DIR . '/blenc/' . $file_name, $unencrypted_key);
        $html .= "<br> BLENC size of content: " . strlen($contents) . PHP_EOL;
    
        /**
        * Server key
        * key_file.blenc
        */
        file_put_contents(TARGET_DIR . '/blenc/' . 'key_file.blenc', $redistributable_key . PHP_EOL);
        $html .= "<br> BLENC redistributable key file key_file.blenc updated." . PHP_EOL;
        exec("cat key_file.blenc >> /usr/local/etc/blenckeys");
    ?>
    

    https://github.com/codex-corp/ncryptd/blob/master/app/controllers/MagicalController.php#L479

    you should put the key to

    blenckeys

    file on your server

    Note: Sometimes you need to reload the apache, if you have "Validation of script" issues

    How to use BLENC in PHP?