943,152 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2198
  • C++ RSS
Mar 13th, 2010
-6

How to Encrypt/Decrypt using a conversion table text file

Expand Post »
Hello peepz..Please try to look at my codes and read every comments i put there..I hope someone could help me with it..Especially those it graduates and higher level to me..I'm only first year college student..We're on visual c++..This is the link to my codes, please download and post the solved one..Thank you..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kiLLeR-eyEd_14 is offline Offline
8 posts
since Mar 2010
Mar 13th, 2010
2
Re: How to Encrypt/Decrypt using a conversion table text file
Hey, please download my 100 dollars and upload it when you have made it a billion dollars.
You see whats wrong? This is a forum. We help each other. We don't work for each other.
Reputation Points: 94
Solved Threads: 26
Posting Whiz
Excizted is offline Offline
309 posts
since Oct 2009
Mar 13th, 2010
0
Re: How to Encrypt/Decrypt using a conversion table text file
well .. u could have posted the code in here using the code tags ..
Besides , its would be prudent to enunciate your problem , or else its difficult to find help in here.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Mar 13th, 2010
0
Re: How to Encrypt/Decrypt using a conversion table text file
  1. #include<iostream.h>
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4.  
  5. char encrypt(char c)
  6. {
  7. FILE *fp1, *fp2;
  8. char ascii;
  9. fp1 = fopen("subtable.txt","r");
  10. fp2 = fopen("encrypted.txt","w");
  11. //i need the code or right function here to get the character's or parameter's (char c) ascii code value from the subtable..
  12. //for example, from the main program, a file to be encrypted will be read using fgetc()..fgetc reads single character from a stream that's why we uses a loop to read all characters until the end of file (EOF)..letter A is the first character to be read in file.txt..By calling fgetc() and encrypt(), letter A will be the parameter of function encrypt()..The question is, how could i get its ascii code value from the subtable..
  13. //lettet 'A' should be returned as 'kEm'
  14. return ascii;
  15. }
  16. char decrypt(char ascii)
  17. {
  18. FILE *fp;
  19. char c;
  20. fp = fopen("encrypted.txt","r");
  21. //from this time, ascii is now the parameter..we're gonna get its character value from the subtable to decrypt it..
  22. return c;
  23. }
  24. void main(void)
  25. {
  26. FILE *fp1, *fp2;
  27. int choice;
  28. char c, encfname[50], decfname[50];
  29. cout<<"Please choose (1 - Encrypt || 2 - Decrypt): ";
  30. cin>>choice;
  31. switch(choice)
  32. {
  33. case 1:
  34. cout<<"Enter filename where to save the encrypted one: ";
  35. //for example, enter encrypted.txt
  36. cin>>encfname;
  37. fp1 = fopen("file.txt","r");
  38. fp2 = fopen(encfname,"w");
  39. c = fgetc(fp1);
  40. while(c!=EOF)
  41. {
  42. fputc(encrypt(c),fp2);
  43. c = fgetc(fp1);
  44. }
  45. break;
  46. case 2:
  47. cout<<"Enter filename where to save the decrypted one: ";
  48. cin>>decfname;
  49. fp1 = fopen("encrypted.txt","r");
  50. fp2 = fopen(decfname,"w");
  51. c = fgetc(fp1);
  52. while(c!=EOF)
  53. {
  54. fputc(decrypt(c),fp2);
  55. c = fgetc(fp1);
  56. }
  57. break;
  58. }
  59. }
Last edited by kiLLeR-eyEd_14; Mar 13th, 2010 at 8:37 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kiLLeR-eyEd_14 is offline Offline
8 posts
since Mar 2010
Mar 13th, 2010
0
Re: How to Encrypt/Decrypt using a conversion table text file
/* sample conversion in subtable.txt */

A 0x1
B 0x2
C 0x3
1 0xa
2 0xb
3 0xc
! x1
@ x2
# x3
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kiLLeR-eyEd_14 is offline Offline
8 posts
since Mar 2010
Mar 13th, 2010
0
Re: How to Encrypt/Decrypt using a conversion table text file
/* then this is the file to be encrypted saved as file.txt */

ABC

its encrypted text should be
0x10x20x3

then if decrypted would be

ABC

again
Last edited by kiLLeR-eyEd_14; Mar 13th, 2010 at 8:43 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kiLLeR-eyEd_14 is offline Offline
8 posts
since Mar 2010
Mar 13th, 2010
0
Re: How to Encrypt/Decrypt using a conversion table text file
its very simple ..
I guess u are not aware of map data structure.
Basically , when ur getting the character from the file , check it using a map and then the equivalent to that may be printed or stored in a file according to ur wish.

this is the simple code on how to use a map data structure .

C++ Syntax (Toggle Plain Text)
  1.  
  2. map<char,int> mymap;
  3. map<char,int>::iterator it;
  4.  
  5. mymap['a'] = 0;
  6. mymap['b'] = 1;
  7. mymap['c'] = 2;
  8. mymap['d'] = 3;
  9. mymap['e'] = 4;
  10. mymap['f'] = 5;
  11.  
  12.  
  13. do
  14. {
  15. for ( it=mymap.begin() ; it != mymap.end(); it++ )
  16.  
  17. {
  18. if( s[i] == (*it).first ) // s[i] holds the plain text which is
  19. { //mapped to the 1st element of map
  20. enc[i++] = (*it).second; // if it matches then the equivalent is
  21. len--; // is stored in ur enc[] which is the
  22. } //encrypted array
  23.  
  24. }
  25. } while (len != 0);


The above is not the complete code , but gives u fair idea on how to use maps for your encryption.

Also google more on maps for its usage.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Mar 14th, 2010
0
Re: How to Encrypt/Decrypt using a conversion table text file
Can you give me the whole code in accords to my problem or program, including the headers to be used..We haven't been discussed map data structure..We've only been discussed the string structure..
Last edited by kiLLeR-eyEd_14; Mar 14th, 2010 at 8:23 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kiLLeR-eyEd_14 is offline Offline
8 posts
since Mar 2010
Mar 15th, 2010
0
Re: How to Encrypt/Decrypt using a conversion table text file
technically speaking map is a STL ( structured template library ) . There are many tutorials in net which teaches you about its usage.
I can provide u the complete code , cuz u will not learn anything . If you get stuck somewhere in this process , i might bail you out.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to get binded combobox selected item?
Next Thread in C++ Forum Timeline: Garbled Returns From Function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC