Category Archives: aes

Aes encryption for php crashing my webserver

for some reason index .php is not working. Here is my code. I was using the example found on http://aesencryption.net/#PHP-aes-encryption-example

    <?php
    include 'AES.php's;
    $inputText = "My text to encrypt";
    $inputKey = "My text to encrypt";
    $blockSize = 256;
    $aes = new AES($inputText, $inputKey, $blockSize);
    $enc = $aes->encrypt();
    $aes->setData($enc);
    $dec=$aes->decrypt();
    echo "After encryption: ".$enc."<br/>";
    echo "After decryption: ".$dec."<br/>";
    ?>

and for some reason it gives me a 404

c custom php extension crash apache

I'm editing some php extension (source) that will encrypt file / source code using DES, then i change the encryption algorithm to aes using library by Karl Malbrain. the file got encrypted, but after that the Apache got crashed. it also happen when trying to decrypt the file, the file get decrypted but then the apache also crashed.

the code that got edited,

int encrypt_file(char *input_file, char *output_file, const char *key TSRMLS_DC)
{
    php_stream *input_stream, *output_stream;
    php_stream_statbuf stat_ssb;
    int fsize, bsize, i;
    char input[16], output[16];
    uchar expkey[4 * Nb * (Nr + 1)];
    ExpandKey (key, expkey);
    /* Open input file */
    input_stream = php_stream_open_wrapper(input_file, "r",
        ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
    if (!input_stream) {
        php_error_docref(NULL TSRMLS_CC, E_ERROR,
                                     "Unable to open file `%s'", input_file);
        return -1;
    }

    /* Get input file size */
    if (php_stream_stat(input_stream, &stat_ssb)) {
        php_stream_close(input_stream);
        return -1;
    }

    fsize = stat_ssb.sb.st_size;
    if (fsize <= 0) {
        php_stream_close(input_stream);
        return -1;
    }

    /* Open output file */
    output_stream = php_stream_open_wrapper(output_file, "w+",
        ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
    if (!output_stream) {
        php_error_docref(NULL TSRMLS_CC, E_ERROR,
                                     "Unable to open file `%s'", output_file);
        php_stream_close(input_stream);
        return -1;
    }

    php_stream_write(output_stream, OUTPUT_FILE_HEADER,
                                                 sizeof(OUTPUT_FILE_HEADER)-1);

    if (fsize % 16 == 0) {
        bsize = fsize / 16;
    } else {
        bsize = fsize / 16 + 1;
    }

    for (i = 0; i < bsize; i++) {
        memset(input, 0, 16);
        (void)php_stream_read(input_stream, input, 16);
        Encrypt(input,key,output);
        php_stream_write(output_stream, output, 16);
    }

    php_stream_close(input_stream);
    php_stream_close(output_stream);

    return 0;
}


PHP_FUNCTION(do_encode_file)
{
    char *input_file = NULL, *output_file = NULL;
    int input_len, output_len;
    char *itmp, *otmp;
    int retval;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", 
               &input_file, &input_len, &output_file, &output_len) == FAILURE) {
        return;
    }

    itmp = malloc(input_len + 1);
    otmp = malloc(output_len + 1);

    if (!itmp || !otmp) {
        if (itmp) free(itmp);
        if (otmp) free(otmp);
        RETURN_FALSE;
    }

    memcpy(itmp, input_file, input_len);
    itmp[input_len] = '\0';

    memcpy(otmp, output_file, output_len);
    otmp[output_len] = '\0';

    retval = encrypt_file(itmp, otmp, __authkey TSRMLS_CC);

    free(itmp);
    free(otmp);

    if (retval == -1) {
        RETURN_FALSE;
    }

    RETURN_TRUE;
}

thank..