Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: sending encrypted data via mail using smtp

  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
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.

  10. #10
    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

  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

    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.

  12. #12
    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

  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

    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 ?

  14. #14
    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

    Load the encrypted message in a QByteArray and replace the required bytes.

    Regards

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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

    The order while sending should be:
    1. encrypt file
    2. encode it with base64
    3. divide it into blocks 64 (or 80 or whatever) bytes each
    4. concatenate blocks using newlines (\r\n)
    5. pass the result into the mail stream adding a base64 header

    when receiving a mail:
    1. parse headers
    2. concatenate lines
    3. base64 decode into QByteArray
    4. decrypt
    5. pass the result into the stream

  16. #16
    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
    Load the encrypted message in a QByteArray and replace the required bytes.

    Regards
    Hey marcel, I'll make changes what you have suggested and let you know the status.
    Thanks

  17. #17
    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

    Hi marcel, I modified the code as you said. I'm unable to
    revert the operation after receiving the file, the contents
    gets deleted, Can you please check the code below.


    //Sending message------>
    Qt Code:
    1. void SynsupMail::sendMessage()
    2. {
    3. string msg;
    4. QFile f ( textFilename->text() );
    5. if ( f.open (IO_ReadOnly) )
    6. {
    7. // file opened successfully
    8. dat = f.readAll();
    9. }
    10. else
    11. {
    12. // problems opening the file - emit a warning message
    13. QMessageBox::warning( this, tr("error"),
    14. tr("Error opening file %1").arg( textFilename->text() ) );
    15. enableSubmit();
    16. return;
    17. }
    18. f.close();
    19. StringSource ( string(QString(dat).data()), true, new Base64Encoder(new StringSink(msg)));
    20. Smtp *smtp = new Smtp( textFrom->text(), textTo->text(),
    21. textSubject->text(),
    22. QString(msg.data()), fileName );
    23. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Smtp::Smtp( const QString &from, const QString &to,
    2. const QString &subject,
    3. const QString &body, const QString &fileName)
    4. {
    5. address = QString::fromLatin1( "From: " ) + from +
    6. QString::fromLatin1( "\nTo: " ) + to +
    7. QString::fromLatin1( "\nSubject: " ) + subject + "\n";
    8.  
    9. message = QString::fromLatin1( "\n\n" ) + body + "\n";
    10.  
    11. /*replace( message, QString::fromLatin1( "\n" ),
    12. QString::fromLatin1( "\r\n" ) );*/
    13. replace( message, QString::fromLatin1( "\r\n.\r\n" ),
    14. QString::fromLatin1( "\r\n..\r\n" ) );
    15. replace( address, QString::fromLatin1( "\n" ),
    16. QString::fromLatin1( "\r\n" ) );
    17. replace( address, QString::fromLatin1( "\r\n.\r\n" ),
    18. QString::fromLatin1( "\r\n..\r\n" ) );
    19. }
    To copy to clipboard, switch view to plain text mode 
    //Receiving message------>
    Qt Code:
    1. int SysupClass::openFile( const char* infile )
    2. {
    3. QFile f ( infile );
    4. if ( f.open (IO_ReadOnly) )
    5. {
    6. // file opened successfully
    7. dat = f.readAll();
    8. }
    9. else
    10. {
    11. f.close();
    12. return -1;
    13. }
    14. f.close();
    15.  
    16. //Here I might not be correct to reverse the operation
    17. //i.e removing \r\n..\r\n
    18. /*QString ss = dat;
    19. replace( ss, QString::fromLatin1( "\r\n..\r\n" ),
    20.   QString::fromLatin1( "\r\n.\r\n" ) );*/
    21.  
    22. QFile f1 ( strcat(infile, ".mod") );
    23. if ( f1.open (IO_WriteOnly|IO_Truncate) )
    24. {
    25. // file opened successfully
    26. f1.writeBlock(ss, ss.length());
    27. }
    28. f1.close();
    29.  
    30. //decrypt the file with AES::CBC mode
    31. }
    To copy to clipboard, switch view to plain text mode 
    wysota--->
    I'm unable to understand these points
    3. divide it into blocks 64 (or 80 or whatever) bytes each ---- need some more explanation
    4. concatenate blocks using newlines (\r\n) ----- ---- need some more explanation
    5. pass the result into the mail stream adding a base64 header --------- ---- need some more explanations
    If possible please correct me with my code above and tell me how can I achieve the above three points

  18. #18
    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
    The order while sending should be:
    1. encrypt file
    2. encode it with base64
    3. divide it into blocks 64 (or 80 or whatever) bytes each
    4. concatenate blocks using newlines (\r\n)
    5. pass the result into the mail stream adding a base64 header

    when receiving a mail:
    1. parse headers
    2. concatenate lines
    3. base64 decode into QByteArray
    4. decrypt
    5. pass the result into the stream
    Did you mean this below code

    while sending
    Qt Code:
    1. int MAX_BLK_SIZE = 64;
    2. string sSrcBlk = dat;
    3. int hManyBlks = sSrcBlk.length()/MAX_BLK_SIZE;
    4. char** pSrcBlk = new char*[hManyBlks + 1];
    5.  
    6. for( int i=0; i < hManyBlks; ++i )
    7. {
    8. pSrcBlk[i] = new char[MAX_BLK_SIZE + 1];
    9. memcpy(pSrcBlk[i], sSrcBlk.data(), MAX_BLK_SIZE);
    10. }
    11.  
    12. //append the block
    13. for( int i=0; i < hManyBlks; ++i )
    14. QString blks = QString( pSrcBlk[i] ) + QString("\r\n");
    15.  
    16. //send blks through mail stream adding base64 header
    To copy to clipboard, switch view to plain text mode 

    while receiving
    Qt Code:
    1. 1) parse -- How to do this ?
    2. 2) contcatenate lines -- How to do this ?
    3. 3) base64 decode into ByteArray -- I can do it
    4. 4) decrypt ---- I can do it
    5. 5) pass the result into the stream -- I can do it
    To copy to clipboard, switch view to plain text mode 
    Last edited by vermarajeev; 13th August 2007 at 15:30.

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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

    Quote Originally Posted by vermarajeev View Post
    I'm unable to understand these points
    3. divide it into blocks 64 (or 80 or whatever) bytes each ---- need some more explanation
    4. concatenate blocks using newlines (\r\n) ----- ---- need some more explanation
    5. pass the result into the mail stream adding a base64 header --------- ---- need some more explanations
    If possible please correct me with my code above and tell me how can I achieve the above three points
    Ad 3. and 4. base64 encoded data is a string of characters. The usual thing to do is to divide the long line into shorter lines.
    Ad 5. Make the result part of your outgoing mail embedding it into proper attachment headers.

    Something like:
    Qt Code:
    1. QString base64encoded; // your encoded data here
    2. while(base64encoded.length()>64){
    3. slist << base64encoded.left(64);
    4. base64encoded.remove(0, 64);
    5. }
    6. slist << base64encoded; // remainder
    7.  
    8. QString result = slist.join("\x0a\x0C"); // \r\n
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by vermarajeev View Post
    while receiving
    Qt Code:
    1. 1) parse -- How to do this ?
    2. 2) contcatenate lines -- How to do this ?
    3. 3) base64 decode into ByteArray -- I can do it
    4. 4) decrypt ---- I can do it
    5. 5) pass the result into the stream -- I can do it
    To copy to clipboard, switch view to plain text mode 
    Oh come on... I'm not going to write a mail client for you Use your head - think.

  20. #20
    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

    Thank you wysota,
    It is a long time that I have send the post and till then I have been trying to solve the problem. And the good thing is I'm just near to solution. I'm able to send the encrypted data, and also able to decode it but only partial i.e only half. I think there is a problem using std::string with binary data.

    I'm just trying to figure out and let you know if I get.
    I'm really sorry to ask for solution without myself going in depth.

    Thanks

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.