Welcome to Dream.In.Code
Click Here
Getting PHP Help is Easy!

Join 118,873 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,827 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Php Encrypted file attachment problem

 
Reply to this topicStart new topic

Php Encrypted file attachment problem

Rating  5
raunak.vaghela
post 10 Jan, 2007 - 07:59 PM
Post #1


New D.I.C Head

*
Joined: 10 Jan, 2007
Posts: 3


My Contributions


I am trying to automate process of encrypting a file and sending it through email. Where I have got success for below things:

I am able to encrypt file, and I am able to send file as attachment.

But I am facing a problem with file that is being received is not exactly as the file I sent. When I compared original encrypted file and received file from email, I found out that the received file truncates all data after it encounters “0x00” in file. When I tried to encode encrypted file with base64, I am able to received data in Ascii form, but I can’t get the original formatted file.

Can you help me for this problem?

How do I avoid truncation of data after encounter of 00 in file while attaching it?
What content-type should I give when I encode file with base64? As content type Base64 is not allowed in firewall while downloading the file.

If you can help me with this problem as soon as possible, I would really be greatful to you.

I AM USING MIME.CLASS FOR CODE THAT I FOUND ON INTERNET
CODE
/* ---------------------------------------------------------
  Attach a 'file' to e-mail message
  Pass a file name to attach.
  This function returns a success/failure code/key of current
  attachment in array (+1). Read attach() below.
--------------------------------------------------------- */
function fattach($path, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') {
    $this->errstr = "";
    if (!file_exists($path)) {
        $this->errstr = "File does not exist";
        return 0;
    }
    // Read in file
    $fp = fopen($path, "rb");    # (b)inary for Win compatability
    if (!$fp) {
        $this->errstr = "fopen() failed";
        return 0;    //failed
    }
    $contenttype .= ";\r\n\tname=".basename($path).";\r\n";
    
    //if(filesize($path))
    while(!feof($fp))
            $data .= fread($fp, 2048);
  
  //$data=readfile($path);
  echo "<BR>".$data."<BR>";
    return $this->attach($data,
            $description,
            $contenttype,
            $encoding,
            $disp);    
}

/* ---------------------------------------------------------
  Attach data provided by user (rather than a file)
  Useful when you want to MIME encode user input
  like HTML. NOTE: This function returns key at which the requested
  data is attached. IT IS CURRENT KEY VALUE + 1!!
  Construct the body with MIME parts
--------------------------------------------------------- */
function attach($data, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') {
    $this->errstr = "";
    if (empty($data)) {
        $this->errstr = "No data to be attached";
        return 0;
    }
    if (trim($contenttype) == '') $contenttype = OCTET;
    if (trim($encoding) == '') $encoding = BASE64;
    if ($encoding == BIT7)
    {$emsg = $data;}
    elseif ($encoding == QP)
    {$emsg = $$this->qp_func($data);}
    elseif ($encoding == BASE64) {
        if (!$this->base64_func)     # Check if there is user-defined function
          {
           //$emsg = base64_encode($data);    
           $emsg = chunk_split(base64_encode($data));
            }
        else
        {$emsg = $this->base64_func($data);}
    }
    
    //Check if content-type is text/plain and if charset is not specified append default CHARSET
    if (preg_match("!^".TEXT."!i", $contenttype) && !preg_match("!;charset=!i", $contenttype))
        $contenttype .= ";\r\n\tcharset=".CHARSET;
    $msg = sprintf("Content-Type: %s Content-Transfer-Encoding: %s%s%s%s",
    $contenttype.CRLF,
    $encoding.CRLF,
    ((($description) && (BODY != $description))?"Content-Description: $description".CRLF:""),
    ($disp?"Content-Disposition: $disp".CRLF:""),
    CRLF.$emsg.CRLF);
    BODY==$description? $this->mimeparts[0] = $msg: $this->mimeparts[] = $msg;
    return sizeof($this->mimeparts);
}


[mod edit]What is is with people and their adversion to using code tags?!
User is offlineProfile CardPM

Go to the top of the page


bcswebstudio
post 24 Jul, 2008 - 12:26 PM
Post #2


New D.I.C Head

*
Joined: 24 Jul, 2008
Posts: 2

raunak- did you find a solution to this? I'm hoping to do exactly the opposite: pull attachments off of encrypted emails.... any advice?
User is offlineProfile CardPM

Go to the top of the page

JeroenFM
post 26 Jul, 2008 - 02:13 AM
Post #3


D.I.C Head

Group Icon
Joined: 30 Jun, 2008
Posts: 179



Thanked 9 times

Dream Kudos: 100
My Contributions


Might be me, but I don't see any encryption happening at all - just base64 which is just a special encoding
User is offlineProfile CardPM

Go to the top of the page

Moonbat
post 26 Jul, 2008 - 06:58 AM
Post #4


D.I.C Head

Group Icon
Joined: 30 Jun, 2008
Posts: 248



Thanked 14 times

Dream Kudos: 575
My Contributions


0x00 is a null character. I guess when your email thing detects it, it stops showing anything after it.

But to be honest, Base64 isn't really encryption. You might as well XOR the thing, or ROT13 it. That will at least get rid of your 0x00 errors.

This post has been edited by Moonbat: 26 Jul, 2008 - 06:59 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 02:13AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month