954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to Encrypt/Decrypt using a conversion table text file

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

kiLLeR-eyEd_14
Newbie Poster
8 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

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.

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>

char encrypt(char c)
{
        FILE *fp1, *fp2;
        char ascii;
        fp1 = fopen("subtable.txt","r");
        fp2 = fopen("encrypted.txt","w");
//i need the code or right function here to get the character's or parameter's (char c) ascii code value from the subtable..
//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..
//lettet 'A' should be returned as 'kEm'
        return ascii;
}
char decrypt(char ascii)
{
        FILE *fp;
        char c;
        fp = fopen("encrypted.txt","r");
//from this time, ascii is now the parameter..we're gonna get its character value from the subtable to decrypt it..
        return c;
}
void main(void)
{
        FILE *fp1, *fp2;
        int choice;
        char c, encfname[50], decfname[50];
        cout<<"Please choose (1 - Encrypt || 2 - Decrypt): ";
        cin>>choice;
        switch(choice)
        {
                case 1:
                        cout<<"Enter filename where to save the encrypted one: ";
//for example, enter encrypted.txt
                        cin>>encfname;
                        fp1 = fopen("file.txt","r");
                        fp2 = fopen(encfname,"w");
                        c = fgetc(fp1);
                        while(c!=EOF)
                        {
                                fputc(encrypt(c),fp2);
                        c = fgetc(fp1);
                        }
						break;
                case 2:
                        cout<<"Enter filename where to save the decrypted one: ";
                        cin>>decfname;
                        fp1 = fopen("encrypted.txt","r");
                        fp2 = fopen(decfname,"w");
                        c = fgetc(fp1);
                        while(c!=EOF)
                        {
                                fputc(decrypt(c),fp2);
                        c = fgetc(fp1);
                        }
						break;
         }
}
kiLLeR-eyEd_14
Newbie Poster
8 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

/* sample conversion in subtable.txt */

A 0x1
B 0x2
C 0x3
1 0xa
2 0xb
3 0xc
! x1
@ x2
# x3

kiLLeR-eyEd_14
Newbie Poster
8 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

/* 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

kiLLeR-eyEd_14
Newbie Poster
8 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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 .

map<char,int> mymap;
map<char,int>::iterator it;

mymap['a'] = 0;
 mymap['b'] = 1;
 mymap['c'] = 2;
 mymap['d'] = 3;
 mymap['e'] = 4;
 mymap['f'] = 5; 


do
{
for ( it=mymap.begin() ; it != mymap.end(); it++ )

    {
        if( s[i] == (*it).first )        // s[i] holds the plain text which is     
        {                                     //mapped  to the 1st element of map
            enc[i++] = (*it).second;    // if it matches then the equivalent is 
            len--;                                // is stored in ur enc[] which is the 
          }                                    //encrypted array

    }
} 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.

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 

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

kiLLeR-eyEd_14
Newbie Poster
8 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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.

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You