Decoding base64 data and extracting images

Discussion in 'ASP General' started by gRizwan, Sep 20, 2005.

  1. gRizwan

    gRizwan Guest

    Hello all,

    We have a problem on a webpage. That page is sent some email data in
    base64 format. what we need to do is, decode the base64 data back to
    original shape and extract attached image from it.

    Any help will be highly appriciated.

    Thanks

    Rizwan
     
    gRizwan, Sep 20, 2005
    #1
    1. Advertisements

  2. gRizwan

    gRizwan Guest

    I forgot to mention, we are using ASP.

    Thanks again,

    Rizwan
     
    gRizwan, Sep 20, 2005
    #2
    1. Advertisements

  3. gRizwan

    McKirahan Guest

    Below is a VBScript that does what you want but requires "base64.exe"
    from http://www.fourmilab.ch/webtools/base64/

    You copy-and-paste the base63 (endoded) data into the textarea then click
    the Decode button which saves it to disk (as "base64.enc") then decodes it
    (as "base64.dec"). After which you can rename it to a filename with the
    correct extension.

    I use a variation of this to look at the base64 text included in SPAM. I
    use a text editor to view the decoded data to see it it might be harmful.


    <html>
    <head>
    <title>base64.hta</title>
    <script type="text/vbscript">
    Option Explicit
    Sub Decode()
    '*
    '* Declare Constants
    '*
    Const cHTA = "base64.hta"
    Const cDEC = "base64.dec"
    Const cENC = "base64.enc"
    Const cWSS = "%comspec% /C base64.exe -d $1 $2"
    '*
    '* Encoded File Save
    '*
    Dim strENC
    strENC = document.getElementById("encoded").value
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objCTF
    Set objCTF = objFSO.CreateTextFile(cENC,True)
    objCTF.Write(strENC)
    Set objCTF = Nothing
    Set objFSO = Nothing
    '*
    '* Decoded File Make
    '*
    Dim strWSS
    strWSS = Replace(cWSS,"$1",cENC)
    strWSS = Replace(strWSS,"$2",cDEC)
    'Alert(strWSS)
    Dim objWSS
    Set objWSS = CreateObject("WScript.Shell")
    objWSS.Run strWSS, 7, True
    Set objWSS = Nothing
    End Sub
    </script>
    <head>
    <body>
    <form>
    <b>Copy-and-paste encoded data below then click Decode.</b>
    <br>
    <textarea name="encoded" id="encoded" cols="80" rows="20">
    </textarea>
    <br>
    <input type="button" value="Decode" onclick="Decode()">
    </form>
    </body>
    </html>

    Pure VBScript solutions may exist; try Google.
     
    McKirahan, Sep 20, 2005
    #3
  4. Here's a pure VBS solution:
    http://www.pstruh.cz/tips/detpg_Base64/
     
    Chris Hohmann, Sep 20, 2005
    #4
  5. gRizwan

    gRizwan Guest

    Thanks for your reply McKirahan,

    Infact we need some script which do this decoding for us in ASP page.
    Exe will not help us.

    Rizwan
     
    gRizwan, Sep 20, 2005
    #5
  6. gRizwan

    gRizwan Guest

    Thanks for your reply Chris Hohmann,

    Yes last nite after a lots of search i found same link you mentioned.
    It looks working, will see in detail later.

    But the second part of the question is still there. i.e. Extract
    attached Images/Files from that email, which was encoded in base64.
    Let's suppose, we have decoded data from base64 to original shape, Now
    how will we extract attachments from that email in ASP?

    Thanks again
     
    gRizwan, Sep 20, 2005
    #6
  7. gRizwan

    McKirahan Guest

    How is the "page is sent some email data"?

    Could you post an URL that has a sample?
     
    McKirahan, Sep 20, 2005
    #7
  8. gRizwan

    gRizwan Guest

    This is sample, what we get (i have truncated MMS data)

    <bspostevent>
    <field name="RemoteNetwork" type = "string">vodafone-uk</field>
    <field name="LastName" type = "string"></field>
    <field name="MobileNetwork" type = "string">vodafone-uk</field>
    <field name="MMS" type =
    "data">TUlNRS1WZXJzaW9uOiAxLjANClN1YmplY3Q6IA0KRnJvbTog...........</field>
    <field name="State" type = "string">1</field>
    <field name="MobileNumber" type = "string">+447796170669</field>
    <field name="Text" type = "string"></field>
    <field name="Now" type = "date">2004-06-18 20:28:07 +0000
    </field>
    </bspostevent>
     
    gRizwan, Sep 20, 2005
    #8
  9. gRizwan

    gRizwan Guest

    There's a big "MMS" tag data in base64.
    lets suppose we are able to decode the base64 to original shape. Then
    it will be just like an email, with attachmenr(s) of image(s). We need
    to extract those image(s).

    if you need complete base64 data, i can mail you google not allowing to
    paste here.

    can you help?
     
    gRizwan, Sep 20, 2005
    #9
  10. gRizwan

    McKirahan Guest

    How do you know that it's an image?
    What would be the filename?
    Are there multiple per page?
    How, exactly, would you want the process to work?
     
    McKirahan, Sep 20, 2005
    #10
  11. gRizwan

    McKirahan Guest

    The partial base64 string that you sent decodes to:

    MIME-Version: 1.0
    Subject:
    From:

    I now understand that the Web page contains a field (MMS)
    that contains a base64 string of an e-mail message --
    your original description was unclear on this.

    I gather:
    1) that you visit a Web page;
    2) that the Web page contains a single <bspostevent> tag;
    3) that this tag contains a base64 string in the "MMS" field;
    4) that this base64 string is an e-mail message;
    5) that the e-mail message contains an image;
    6) that you want to save or view the image -- which?

    A URL, if possible, would be most helpful.
     
    McKirahan, Sep 20, 2005
    #11
  12. gRizwan

    gRizwan Guest

    Below is the data decoded by the function we talked above (
    http://www.pstruh.cz/tips/detpg_Base64/ )
    Now this is almost a mail message with attachment(s). No, there can be
    some different files too not just images. but most probably it will
    include image(s). There might be more than one too.

    You can see example of what we get at http://pakitfriends.com/mms.xml
    infact this kind of file is sent to our webpage with POST method. we
    have done XML parsing part and have extracted the MMS data in a
    variable. Now we suppose Decoding is correctly done by that code.

    * We dont know the name of the image(s)
    * yes, we want to save the attachment(s)


    ********** Decoded Data ***********
    MIME-Version: 1.0
    Subject:
    From: +447796170669
    To: +447719336699
    Mxtelecom-Id: 15881964
    Date: 2004-06-18 21:28:13
    Content-Transfer-Encoding: binary
    Content-Type: multipart/related;
    boundary="F3X/kupo3+S3vrIYnnksMQAAAAM="; start="<mms.smil>";
    type="application/smil"

    --F3X/kupo3+S3vrIYnnksMQAAAAM=
    Content-Type: application/smil; name=mms.smil
    Content-Id: <mms.smil>
    Content-Transfer-Encoding: 7bit

    <smil>
    <body>
    <par dur="5s">
    <ref src="t0" />
    </par>
    <par dur="5s">
    <text src="t1%3At1.txt" />
    </par>
    <par dur="5s">
    <img src="i1%3ATeifi.jpg" />
    </par>
    </body>
    </smil>

    --F3X/kupo3+S3vrIYnnksMQAAAAM=
    Content-Type: application/smil; name=t0
    Content-Transfer-Encoding: binary
    Content-Id: <t0>

    <smil>
    <head>
    <layout>
    <root-layout height="127" width="111"/>
    <region id="Image" top="50%" left="0%" height="50%" width="100%"/>
    <region id="Text" top="0%" left="0%" height="50%" width="100%"/>
    </layout>
    </head>
    <body>
    <par dur="60000ms">
    <text src="cid:t1:t1.txt" region="Text"/>
    <img src="cid:i1:Teifi.jpg" region="Image"/>
    </par>
    </body>
    </smil>

    --F3X/kupo3+S3vrIYnnksMQAAAAM=
    Content-Type: text/plain; name=t1%3At1.txt
    Content-Location: t1%3At1.txt

    Apicheck
    --F3X/kupo3+S3vrIYnnksMQAAAAM=
    Content-Type: image/jpeg; name=i1%3ATeifi.jpg
    Content-Transfer-Encoding: binary
    Content-Location: i1%3ATeifi.jpg

    ÿØÿáExif
    *****************************
     
    gRizwan, Sep 20, 2005
    #12
  13. gRizwan

    McKirahan Guest

    Below is the data decoded by the function we talked above (
    http://www.pstruh.cz/tips/detpg_Base64/ )
    Now this is almost a mail message with attachment(s). No, there can be
    some different files too not just images. but most probably it will
    include image(s). There might be more than one too.

    You can see example of what we get at http://pakitfriends.com/mms.xml
    infact this kind of file is sent to our webpage with POST method. we
    have done XML parsing part and have extracted the MMS data in a
    variable. Now we suppose Decoding is correctly done by that code.

    * We dont know the name of the image(s)
    * yes, we want to save the attachment(s)


    ********** Decoded Data ***********
    MIME-Version: 1.0
    Subject:
    From: +447796170669
    To: +447719336699
    Mxtelecom-Id: 15881964
    Date: 2004-06-18 21:28:13
    Content-Transfer-Encoding: binary
    Content-Type: multipart/related;
    boundary="F3X/kupo3+S3vrIYnnksMQAAAAM="; start="<mms.smil>";
    type="application/smil"

    --F3X/kupo3+S3vrIYnnksMQAAAAM=
    Content-Type: application/smil; name=mms.smil
    Content-Id: <mms.smil>
    Content-Transfer-Encoding: 7bit

    <smil>
    <body>
    <par dur="5s">
    <ref src="t0" />
    </par>
    <par dur="5s">
    <text src="t1%3At1.txt" />
    </par>
    <par dur="5s">
    <img src="i1%3ATeifi.jpg" />
    </par>
    </body>
    </smil>

    --F3X/kupo3+S3vrIYnnksMQAAAAM=
    Content-Type: application/smil; name=t0
    Content-Transfer-Encoding: binary
    Content-Id: <t0>

    <smil>
    <head>
    <layout>
    <root-layout height="127" width="111"/>
    <region id="Image" top="50%" left="0%" height="50%" width="100%"/>
    <region id="Text" top="0%" left="0%" height="50%" width="100%"/>
    </layout>
    </head>
    <body>
    <par dur="60000ms">
    <text src="cid:t1:t1.txt" region="Text"/>
    <img src="cid:i1:Teifi.jpg" region="Image"/>
    </par>
    </body>
    </smil>

    --F3X/kupo3+S3vrIYnnksMQAAAAM=
    Content-Type: text/plain; name=t1%3At1.txt
    Content-Location: t1%3At1.txt

    Apicheck
    --F3X/kupo3+S3vrIYnnksMQAAAAM=
    Content-Type: image/jpeg; name=i1%3ATeifi.jpg
    Content-Transfer-Encoding: binary
    Content-Location: i1%3ATeifi.jpg

    ÿØÿáExif
    *****************************
     
    McKirahan, Sep 21, 2005
    #13
  14. gRizwan

    McKirahan Guest

    Sorry, there was no response in my last reply.

    Below is the data decoded by the function we talked above (
    http://www.pstruh.cz/tips/detpg_Base64/ )
    Now this is almost a mail message with attachment(s). No, there can be
    some different files too not just images. but most probably it will
    include image(s). There might be more than one too.

    You can see example of what we get at http://pakitfriends.com/mms.xml
    infact this kind of file is sent to our webpage with POST method. we
    have done XML parsing part and have extracted the MMS data in a
    variable. Now we suppose Decoding is correctly done by that code.

    * We dont know the name of the image(s)
    * yes, we want to save the attachment(s)


    Apparently the image (in this case) is named "Teifi.jpg".

    The decoded text can be saved but that's not what you want.

    I don't see the attached image encoded in the MMS data.
     
    McKirahan, Sep 21, 2005
    #14
  15. gRizwan

    gRizwan Guest

    Yes, This is what decoded by the code.

    what to do now?
     
    gRizwan, Sep 21, 2005
    #15
  16. gRizwan

    McKirahan Guest

    I suspect that we need to first understand e-mail and headers.

    Also, can you provide a description of the entire process?
     
    McKirahan, Sep 21, 2005
    #16
  17. gRizwan

    gRizwan Guest

    * we have a webpage
    * another site gets an MMS data in XML
    * that site sends it to our webpage through POST
    * we receive that and get the MMS tag's data

    * Now we need to decode it from base64 encoding.
    * then we need to get the attachments & save those

    thast all.

    Now, i am stuck here.
     
    gRizwan, Sep 21, 2005
    #17
  18. gRizwan

    McKirahan Guest

    Looking at the "Decoded Data",
    I don't believe that the attachments are embedded in it.

    Can you have them send you the original e-mail with attachments?
     
    McKirahan, Sep 21, 2005
    #18
  19. gRizwan

    gRizwan Guest

    there software automatically converts to this thing. so we'll have to
    play with this.
    Don't you think this decoding algorithm might be faulty?
     
    gRizwan, Sep 21, 2005
    #19
  20. gRizwan

    gRizwan Guest

    there software automatically converts to this thing. so we'll have to
    play with this.
    Don't you think this decoding algorithm might be faulty?
     
    gRizwan, Sep 21, 2005
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.