Results 1 to 20 of 21

Thread: sending encrypted data via mail using smtp

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default sending encrypted data via mail using smtp

    Hello,
    I'm writing a mail utility program in my application. I make use of smtp example given in Qt3 (/examples/network/mail).

    Description of my application:
    The application is very similar to mspaint in windows. The only difference is my application creates data files while mspaint creates image files.

    Problem:
    The files created by my application can be a plain/encrypted one. I use AES::CBC( Advanced encryption standard in Cipher Block Chaining) algorithm to encryt the files.
    I'm able to send both types of files( plain/encrypt ) via mail. The problem is when I send an encrypted file, the recipient is unable to decrypt the file.

    Consider below scenario.
    PersonA sends an encrypted file to PersonB
    PersonB detaches the file
    PersonB supplies detached file through decryption functionality (AES::CBC) to get back the plain text but fails.

    I went through some of the specifications of SMTP and found that, smtp can only transfer data with ascii value less than 127. Also mentioned in the specification that we have to use base64 alogorithm to encode the text/binary data.
    Basically the structure is something like below.

    Sending email:
    File --> (Encoding base64) --> encoded file

    Receiving email:
    encoded file --> (Decoding base64) --> File

    The file can be text or binary.
    Even though I'm using the same structure as discussed above, the recipient is unable to decrypt the file (the decryption of file fails). I wonder why? I have been trying to figure out what is wrong but was unsuccessful. Can somebody please help me?

    Thanks in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    But there can be any number of reasons.
    Have you tried to isolate the encoding (to base 64 and the reverse ) and test it locally?
    Also have you tested the encryption and decryption separately?

    Regards

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    Also have you tested the encryption and decryption separately?
    Yes the encryption and description works fine separately. On the local machine I can encrypt/decrypt, works fine.
    The only problem comes when I send the encrypted file via mail and then try to decrypt the file.

    Have you tried to isolate the encoding (to base 64 and the reverse ) and test it locally?
    I didnt get you.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    I went through some of the specifications of SMTP and found that, smtp can only transfer data with ascii value less than 127. Also mentioned in the specification that we have to use base64 alogorithm to encode the text/binary data.
    Don't you do this? Encode all data(encrypted or not) prior to sending to base 64 and decode it upon receiving?

    I was under the impression that you did this.

    Regards

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    Quote Originally Posted by marcel View Post
    Don't you do this? Encode all data(encrypted or not) prior to sending to base 64 and decode it upon receiving?

    I was under the impression that you did this.

    Regards
    Encode all data by what?
    below is the structure
    Sending email:
    File --> (Encoding base64) --> encoded file

    Receiving email:
    encoded file --> (Decoding base64) --> File
    File can be plain/encoded
    I use AES::CBC to encode the file initially, then encode it again using base64 algorithm then give it to SMTP. I use Transfer encoding scheme as base64 in smtp.
    Please be more specific?

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    I use AES::CBC to encode the file initially, then encode it again using base64 algorithm then give it to SMTP. I use Transfer encoding scheme as base64 in smtp.
    This is what I was asking.
    If you encode the data to base 64 before sending it.
    Also, I asked if you tested this base 64 encoding/decoding locally.
    Maybe you don't decode from base64 ok.

    I imagine you cannot post the code, but are you sure there aren't any differences between plain files? Not even a byte?
    Do you handle well SMTP transparency and the end of data message indicator?
    Because a plain file might work even if you miss a byte, but an encoded file won't.

    Regards

  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    Quote Originally Posted by marcel View Post
    This is what I was asking.
    If you encode the data to base 64 before sending it.
    Also, I asked if you tested this base 64 encoding/decoding locally.
    Maybe you don't decode from base64 ok.
    Regards
    The decoding from base64 is taken care by smtp once I use
    Content-Transfer-Encoding: base64
    after the DATA tag used for smtp.

    I imagine you cannot post the code, but are you sure there aren't any differences between plain files? Not even a byte?
    Do you handle well SMTP transparency and the end of data message indicator?
    Because a plain file might work even if you miss a byte, but an encoded file won't.
    I can here is the small sample
    Qt Code:
    1. void SynsupMail::sendMessage()
    2. {
    3. QString message;
    4. string msg;
    5.  
    6. QFile f ( textFilename->text() ); //textFilename->text() contains file path. The file can be en
    7. //encrypted using AES::CBC or a plain text
    8. if ( f.open (IO_ReadOnly) )
    9. {
    10. // file opened successfully
    11. QTextStream t ( &f );
    12. // read the contents of the file into message
    13. while( ! t.eof() )
    14. {
    15. message += t.readLine();
    16. message += "\n";
    17. }
    18. f.close();
    19.  
    20. //encode the data using base64 using crypto++ library for Qt3, in Qt4
    21. //there is a function tobase64().
    22. StringSource (string(message.data()), true, new Base64Encoder(new StringSink(msg))) ;
    23.  
    24. Smtp *smtp = new Smtp( textFrom->text(), textTo->text(), textSubject->text(),
    25. QString(msg.data()), fileName, );
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    All the code in smtp.cpp is same as that of /examples/network/mail except this
    else if in function void Smtp::readyRead(). I made this modification to
    send the file as an attachment
    Qt Code:
    1. else if ( state == Body && responseLine[0] == '3' )
    2. {
    3. QString fileName("");
    4. QString version;
    5. version += QString("MIME-Version: 1.0\r\n");
    6.  
    7. QString Header;
    8. Header += QString("Content-Type: multipart/mixed; ") +
    9. QString("boundary=unique-boundary-1\r\n") ;
    10.  
    11. QString headerContent;
    12. headerContent += QString("--unique-boundary-1\r\n");
    13. headerContent += QString("Content-Type: application/octet-stream ") + QString("\r\n");
    14.  
    15. headerContent += QString("Content-Transfer-Encoding: base64\r\n");
    16. headerContent += QString("Content-Disposition: attachment;") +
    17. QString( "filename=" ) + _fileName ;
    18.  
    19. //t is QTextStream
    20. *t << version;
    21. *t << address;
    22. *t << Header;
    23. *t << headerContent;
    24.  
    25. *t << message;
    26. QString closeTag;
    27. closeTag += QString("--unique-boundary-1--\r\n");
    28.  
    29. *t << closeTag << ".\r\n";
    30. state = Quit;
    31. }
    To copy to clipboard, switch view to plain text mode 
    These are the things that happen,
    Plain text file--->
    plaintext file --> Encoding base64 --> Over N/w --> SMTP decodes base64 --> plaintext file to receiver
    Encrypted file (encrypted with AES::CBC)--->
    Encrypted file --> Encoding base64 --> Over N/w --> SMTP decodes base64 --> Encrypted file
    M i doing anything wrong above ?

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    Qt Code:
    1. while( ! t.eof() )
    2. {
    3. message += t.readLine();
    4. message += "\n";
    5. }
    To copy to clipboard, switch view to plain text mode 
    I see you add a newline character for each line read from the file.
    Why is that? RFC 821 specifies maximum line lengths of 998 bytes.
    Do you do this for both encrypted and plain files? Because it is not ok to modify an encrypted file like this. A plain file would work, though.

    If you do this for encrypted files, then do you also do you do the reverse process on receiving?

    Regards

  9. #9
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    Qt Code:
    1. while( ! t.eof() )
    2. {
    3. message += t.readLine();
    4. message += "\n";
    5. }
    To copy to clipboard, switch view to plain text mode 
    I see you add a newline character for each line read from the file.
    Why is that?
    A newline. So mean to say I shouldnt append the new line like above.
    Ok I'll try removing the newline "\n" from message and see if it works.

    If you do this for encrypted files, then do you also do you do the reverse process on receiving?
    No I was not doing that.

    Marcel one more doubt, I use
    Qt Code:
    1. message = QString::fromLatin1( "\n\n" ) + body + "\n";
    2. replace( message, QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
    3. replace( message, QString::fromLatin1( "\r\n.\r\n" ), QString::fromLatin1( "\r\n..\r\n" ) );
    To copy to clipboard, switch view to plain text mode 
    where message is the base64 encoded data. Do I need to use replace() here? I think replacing "\n" with "\r\n" might also modify the data. I use the same code as in smtp.cpp given in /examples/network/mail

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    Quote Originally Posted by vermarajeev View Post
    Marcel one more doubt, I use
    Qt Code:
    1. message = QString::fromLatin1( "\n\n" ) + body + "\n";
    2. replace( message, QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
    3. replace( message, QString::fromLatin1( "\r\n.\r\n" ), QString::fromLatin1( "\r\n..\r\n" ) );
    To copy to clipboard, switch view to plain text mode 
    where message is the base64 encoded data. Do I need to use replace() here? I think replacing "\n" with "\r\n" might also modify the data. I use the same code as in smtp.cpp given in /examples/network/mail
    It is not ok to alter the encrypted message in this way, unless you apply the reverse before decrypting it.

    Qt Code:
    1. replace( message, QString::fromLatin1( "\r\n.\r\n" ), QString::fromLatin1( "\r\n..\r\n" )
    To copy to clipboard, switch view to plain text mode 
    It is required.
    This is actually the transparency part.
    So you do this after encryption but prior to sending?
    Then do the reverse after you receive it.

    Regards

  11. #11
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    So you do this after encryption but prior to sending?
    Did you mean,
    For plainText
    plaintext-->base64-->replace() for transparency part -->send

    For encrypted file with AES::CBC
    encrypted file-->base64-->replace() for transparency part-->send
    Then do the reverse after you receive it.
    Ok for plain text file there in no problem in reversing the opr, but how do I reverse
    for an encrypted file as the data is binary ?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: sending encrypted data via mail using smtp

    I suggest you try to base64 encode a plain text file using Qt methods and send it over smtp to see if it is decoded correctly by your mail client. Then you can focus on encrypting the content.

  13. #13
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending encrypted data via mail using smtp

    Quote Originally Posted by wysota View Post
    I suggest you try to base64 encode a plain text file using Qt methods and send it over smtp to see if it is decoded correctly by your mail client. Then you can focus on encrypting the content.
    Yes, I have tried it and it works fine for plain text. The only problem is with encrypted file.

Similar Threads

  1. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.