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

Recommended Answers

All 8 Replies

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.

commented: well said! +4

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.

#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;
         }
}

/* sample conversion in subtable.txt */

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

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

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.