CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2007
    Posts
    2

    encrypt file in php

    can anybody help me with encryption file..such as .avi .jpeg .mp3....
    i know what to do..first i must convert those file to binary i think.can anybody suggest me an idea.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: encrypt file in php

    Well, you could just read the contents and then encrypt them, then write them to the file.

    PHP Code:
    $file 'path/to/file.txt';
    $contents file_get_contents($file);
    if(
    $contents){
      
    // encrypt the $contents and write it to a file.

    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Aug 2007
    Posts
    2

    Re: encrypt file in php

    Quote Originally Posted by PeejAvery
    Well, you could just read the contents and then encrypt them, then write them to the file.

    PHP Code:
    $file 'path/to/file.txt';
    $contents file_get_contents($file);
    if(
    $contents){
      
    // encrypt the $contents and write it to a file.

    well..actually i am pretty lost right now.can you give me a little bit of a direction..if i do something like below.i got an error.can i give a key to the encryption so that i can decrypt it back.

    Warning: fopen(away-enlarge.jpg) [function.fopen]: failed to open stream: No such file or directory in D:\web\tesis\3.php on line 24
    away-enlarge.jpg

    $filename = $_FILES['file']['name'];
    $handle = fopen($filename,'b');
    $contents = file_get_contents($filename);

  4. #4
    Join Date
    Aug 2005
    Posts
    133

    Re: encrypt file in php

    Actually you need to check if the file awas correctly uploaded
    Code:
    if (is_uploaded_file($_FILES['file']['tmp_name'])) { //was the file uploaded?
      $filecontents = gile_get_contents($_FILES['file']['tmp_name']);
      unlink($_FILES['file']['tmp_name']); // delete the temp file
      $key = "topSecretPassKey"; 
    $crypted = encrypte($filecontents,$key); 
    // then do what you want with it
    
    }
    function GenerationCle($Texte,$CleDEncryptage) 
      { 
      $CleDEncryptage = md5($CleDEncryptage); 
      $Compteur=0; 
      $VariableTemp = ""; 
      for ($Ctr=0;$Ctr<strlen($Texte);$Ctr++) 
        { 
        if ($Compteur==strlen($CleDEncryptage))
          $Compteur=0; 
        $VariableTemp.= substr($Texte,$Ctr,1) ^ substr($CleDEncryptage,$Compteur,1); 
        $Compteur++; 
        } 
      return $VariableTemp; 
      } 
    
    function encrypt($Texte,$Cle) 
      { 
      srand((double)microtime()*1000000); 
      $CleDEncryptage = md5(rand(0,32000) ); 
      $Compteur=0; 
      $VariableTemp = ""; 
      for ($Ctr=0;$Ctr<strlen($Texte);$Ctr++) 
        { 
        if ($Compteur==strlen($CleDEncryptage)) 
          $Compteur=0; 
        $VariableTemp.= substr($CleDEncryptage,$Compteur,1).(substr($Texte,$Ctr,1) ^ substr($CleDEncryptage,$Compteur,1) ); 
        $Compteur++;
        } 
      return base64_encode(GenerationCle($VariableTemp,$Cle) );
      } 
    
    function decrypt($Texte,$Cle) 
      { 
      $Texte = GenerationCle(base64_decode($Texte),$Cle);
      $VariableTemp = ""; 
      for ($Ctr=0;$Ctr<strlen($Texte);$Ctr++) 
        { 
        $md5 = substr($Texte,$Ctr,1); 
        $Ctr++; 
        $VariableTemp.= (substr($Texte,$Ctr,1) ^ $md5); 
        } 
      return $VariableTemp; 
      }
    Louis-Philippe Frenette
    Arobas Informatique Granby
    http://www.arobasinformatique.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
  •  





Click Here to Expand Forum to Full Width

Featured