Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    What to encrypt an uploaded text file

    I want to upload text files to a given website and I would like to encrypt them after they have been uploaded. I've googled around but just don't see a PHP encrypt function that works on files (saw only for data streams) - any suggestions?
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You're not giving enough information.

    What have you searched for?

    Do you need one way or two way encryption?

    What are the sizes of the files (can you read them in, encrypt and them save them again)?

    Etc.

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Use the streams.
    PHP Code:
    <?php
    $passphrase 
    'secret';

    // Generate those as you like.
    $iv substr(md5('iv' $passphrasetrue), 08);
    $key substr(md5('some_salt' $passphrasetrue) . md5($passphrase 'more_salt'true), 024);
    $opts = array(
        
    'iv'    => $iv,
        
    'key'    => $key
    );

    // Create a temporary file so we have soemthing to read from. The file will contain this script.
    $temp_file_name tempnam('''encryption_test_');
    $temp_file fopen($temp_file_name'wb');
    $this_file fopen(__FILE__'rb');
    stream_copy_to_stream($this_file$temp_file);
    fclose($this_file);
    fclose($temp_file);

    print 
    "---------\nFile Contents\n---------\n";
    readfile($temp_file_name);
    print 
    "\n---------\n";

    // Open the file you want to encrypt
    $my_file fopen($temp_file_name'rb');

    // Open the encrypted file. You will create a new file and copy the contents of your file, encrypted.
    $encrypted_file_name tempnam('''encryption_test_');
    $encrypted_file fopen($encrypted_file_name'wb');

    // Set encryption filter.
    stream_filter_append($encrypted_file'mcrypt.twofish'STREAM_FILTER_WRITE$opts);
    stream_copy_to_stream($my_file$encrypted_file);

    // Close the files.
    fclose($encrypted_file);
    fclose($my_file);

    print 
    "---------\nEncrypted File Contents\n---------\n";
    readfile($encrypted_file_name);
    print 
    "\n---------\n";

    // Delete the unencrypted file.
    unlink($temp_file_name);

    // Decrypt file.

    $my_file fopen($encrypted_file_name'rb');
    stream_filter_append($my_file'mdecrypt.twofish'STREAM_FILTER_READ$opts);
    $output fopen('php://output''w');
    print 
    "---------\nDecrypted File Contents\n---------\n";
    stream_copy_to_stream($my_file$output);
    print 
    "\n---------\n";
    fclose($my_file);
    fclose($output);

    unlink($encrypted_file_name);
    ?>
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    New Coder
    Join Date
    Oct 2007
    Posts
    16
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yes I did think about the stream method. I was hoping there was a function that would just work on a file itself but it looks like I will have to go with using the stream method.

    I do need to encrypt the file and then decrypt it - I'll come up with a stream method and reference your code example - thanks.
    Free Website Uptime Monitoring
    http://www.247SiteAlert.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •