Results 1 to 5 of 5

Thread: Simple C/C++ program

  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Simple C/C++ program

    It was not long time ago that I wrote a simple C/C++ program that uses XOR encryption/decryption ( the detailed post in this topic was interesting). However, I am not so good at programming, I'm just at intermediate or may be even at beginner level. Nowe, someone quoted form seeing the following code that I don't know nothing about escaping special characters, can any one point out

    Qt Code:
    1. #include<iostream>
    2. #include<cstdlib>
    3. #include<fstream>
    4. #include<cstdio>
    5. using namespace std;
    6. void encryptdecrypt(string& data,string& key)
    7. {
    8. int keypos = 0;
    9.  
    10. for(int i = 0;i<data.length();i++)
    11. {
    12. data[i] = data[i] ^ key[keypos];
    13. keypos++;
    14. if(keypos >= key.length())
    15. keypos = 0;
    16. }
    17. }
    18. int main(int argc,char** argv)
    19. {
    20. string key = "";
    21.  
    22. char c;
    23. string data = "";
    24. if(argc == 1)
    25. {
    26. cout << "Usage: progname filename";
    27. return 0;
    28. }
    29. cout << "Enter a password : ";
    30. while((c = getchar()) != '\n' )
    31. {
    32. key += c;
    33. }
    34.  
    35. FILE *fp;
    36. fp = fopen(argv[1],"rb");
    37. if(fp == NULL)
    38. {
    39. cout << "Sorry, File could not be opened.";
    40. return 0;
    41. }
    42.  
    43. while(1)
    44. {
    45. c = fgetc(fp);
    46. if(feof(fp))
    47. break;
    48. else
    49. data+=c;
    50.  
    51. }
    52.  
    53. encryptdecrypt(data,key);
    54.  
    55. fclose(fp);
    56.  
    57. FILE *fout;
    58. fout = fopen(argv[1],"wb");
    59. if(fout == NULL)
    60. {
    61. cout << "Sorry, File could not be opened.";
    62. return 0;
    63. }
    64. for(int i = 0;i<data.length();i++)
    65. fputc(data[i],fout);
    66.  
    67. fclose(fout);
    68. getchar();
    69. return 0;
    70.  
    71. }
    To copy to clipboard, switch view to plain text mode 

    Also, how can I improve this small program ...
    Thanks.
    Last edited by ct; 22nd August 2007 at 17:57. Reason: Indented Code
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  2. #2
    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: Simple C/C++ program

    So why don't you ask this "someone" to point the problem out for you? I don't see any special characters that need encoding here...

  3. #3
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple C/C++ program

    Well, first of all it was a comment in the blog and my be insignificant. But I really want to know that I am doing things the *right* way and heading the right direction.
    What do you mean by special character needing encryption ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  4. #4
    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: Simple C/C++ program

    Not encryption. Encoding. And I don't know what the commenter meant in this particular situation. Maybe it was about escaping \0 characters so that you don't get invalid end of string prematurely. But this solely depends on how you store and use the data.

  5. #5
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple C/C++ program

    Sorry, could not be in touch due to internet problem.. Aggh...that was a typo..I meant encoding all the way .. Thanks, yes that person might be referring to '\0'. I found a similar program in Bruce Scheigner's Applied Cryptography , in the beginning chapters..and it's short and elegent.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

Similar Threads

  1. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19
  2. how to use progressBar in simple program
    By jyoti in forum Newbie
    Replies: 1
    Last Post: 1st December 2006, 12:16
  3. Writing a simple Draw program
    By neomax in forum General Discussion
    Replies: 1
    Last Post: 23rd November 2006, 06:53
  4. Release my simple program to other users ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 9th July 2006, 23:42
  5. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 16:11

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.