Jason
Jason

Reputation: 265

Android to PHP encryption code

This is the code that is used on the Android to encrypt a .zip file.

function encryptString($RAWDATA) {
    $key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    // encrypt string, use rijndael-128 also for 256bit key, this is obvious
    $td = mcrypt_module_open('rijndael-128', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_string = mcrypt_generic($td, strlen($RAWDATA) . '|' .
                    $RAWDATA);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    // base-64 encode
    return base64_encode($encrypted_string);
}

This is the code for the PHP to decrypt that same .zip file once it is sent to my server.

function decryptString($ENCRYPTEDDATA) {
    $key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";    
        // base-64 decode
        $encrypted_string = base64_decode($ENCRYPTEDDATA);
        // decrypt string
        $td = mcrypt_module_open('rijndael-256', '', 'ecb', '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $key, $iv);
        $returned_string = mdecrypt_generic($td, $encrypted_string);
        unset($encrypted_string);
        list($length, $original_string) = explode('|', $returned_string, 2);
        unset($returned_string);
        $original_string = substr($original_string, 0, $length);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $original_string; 

It doesn't seem to work. It will encrypt the .zip file just fine on the Android, but when I call the function in PHP

$zip_file = $path . $strFileName;
                    decryptString($zip_file);

it doesn't decrypt the .zip file. When I open up the .txt files within the .zip file they are still encrypted.

This is the 2nd encryption code that I have tried since my first attempt didn't work. Any help would be greatly apprecaited, or if you know of encrypt/decrypt code that works for Android to PHP.

Thanks!!

Upvotes: 1

Views: 707

Answers (1)

Emil Vikström
Emil Vikström

Reputation: 91892

This does nothing:

$zip_file = $path . $strFileName;
decryptString($zip_file);

You need to send in the actual file contents into decryptString, not the filename. Then you need to catch the return value from the function and write it back to the file. Try something like this:

$zip_file = $path . $strFileName;
$decrypted = decryptString(file_get_contents($zip_file));
file_put_contents($zip_file, $decrypted);

Upvotes: 2

Related Questions