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

Want to change this to two keys one for encryption and other for decryption

Dear All,
I have been stuck and confused while bringing changes in this program which i got from http://www.dreamincode.net/code/snippet76.htm

This program uses a simple 0xFACA character and adds and subtracts for doing encryption and decryption.

Now what i want to use is asymmetric encryption technique or we can say part of it.

let say we have a a plain text x= 88
for encryption we will use cipher text crypt =(x*37)%200, so the crypt will be = 56
now we want to decrypt it so decrypt = ( crypt*173) % 200, which will give 88, which is the plain text again,

37 and 173 are modular inverse base 200.

when apply the above formula in the above mentioned program it does work, please help me solve this issue.

Best Regards!

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 

Can you show us the program with the changes you've made?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

i dont know about a C++ method but perhaps you could use python's MD5 module, which handles most encryption methods besides MD5

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 
adds and subtracts for doing encryption and decryption

If your program justs shifted character x units to the right for encryption then you need the same translation to the left for decryption

You cant use 2 different keys for simple shifting encrypt/decrypt

Unless for the decrypt, you have a formula that converts a
different key to the same key as the encrypt key.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
Can you show us the program with the changes you've made?
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <stdio.h>


bool password();

void doCrypt(char *cool)
{
     int cryp;//the main int this function uses
    char x;//to get the chars from the file
    ifstream in;//the input stream
    ofstream out;//the output stream
    in.open(cool);//opening the input file
    if(in.fail()){//check if it's not there
         cout<<"Couldn't open the IN file\n";//if it's not, print
        return;
    }
    cout<<"enter the OUT file name: ";//enter the output filename
    cin>>cool;//get it
    out.open(cool);//open the output file
    in.get(x);//get the first char of input
    if(in.eof())//if read beyong end of file
         return;//quit
    while(x != EOF){//while x is not beyong End Of File
         cryp=x-0xFACA;//the crypting
        out<<(char)cryp;//print the crypted char
        in.get(x);//get another char
        if(in.eof())//if read beyong end of file
             break;
    }
    in.close();
    out.close();
    return;
}
void deCrypt(char *daf)
{
     int decr;//the main int
    char x;//the input char
    ifstream in;//input stream
    ofstream out;//output stream
    in.open(daf);//open the inout stream
    if(in.fail()){//see if it's not there
         cout<<"Error couldn't open the IN file\n";//print
        return;
    }
    cout<<"Enter the OUT file name: ";//ask for the out filename
    cin>>daf;
    out.open(daf);//open the outfile
    in.get(x);//get the input char
    if(in.eof())//if read beyong end of file
         return;
    while(x!=EOF){//while x is not beyong end of file
  decr=x+0xFACA;//the dectypring
        out<<(char)decr;
        in.get(x);//get the next char
        if(in.eof())//if read beyong the eon of file
             break;
    }
    return;
}
int main(void)
{
    if (password()) cout << "Access Granted: \n";
    else
    {
    cout << "You are not Authorized to use this program!\n";
    system("pause");
    return 0;
    }
    char *cool=new char[20];//the name pointer
    char nav;              //"navigation char"
    cout << "*---------------------------------------------*\n";
    cout << "| This is an encryption program.  I'm not     |\n";
    cout << "| taking credit for this.  This was a         |\n";
    cout << "| recent submission on planet-source-code     |\n";
    cout << "| I just added password protection to it.     |\n";
    cout << "*---------------------------------------------*\n\n";
    system("pause");
    cout << "\n\n";
    cout << "Use \"m\" for the Menu\n";//print instructions
     while(true){
         cin >> nav;
         switch(nav){
             case 'm':
                 cout << "COMMAND\tFUNCTION\n";
                cout << "e      \tencrypt\n"
                          "d      \tdecrypt\n"
                        "m      \t menu \n"
                        "q      \t quit \n";
                break;
             case 'e':
                 cout << "Enter the file name: \nExample: \"file\".txt\n";
                cin >> cool;
                doCrypt(cool);
                break;
            case 'd':
                 cout << "Enter the file name: ";
                cin >> cool;
                deCrypt(cool);
                break;
            case 'q':
                 exit(0);
                 break;
            default:
                 cout << "I do not understand you!\n";
                break;
        }
    }
}

bool password()
{
     char string1[50];
     cout << "Enter in the encryption password: \n";
     gets(string1);

     if (strcmp(string1, "outlaw")) return 0;
     return 1;
}


I changed the red to cryp=(x * 37)% 200; //the crypting


and the green to decr=(x * 173)% 200;//the dectypring


It encrypts fine but in decryption i think it gets confused.

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 

I tested the program you posted, encrypting and decrypting is working fine, only one major remark I have is that the program hangs at the end of the encryption/decryption progress.

This:

in.get(x);//get the first char of input
if(in.eof())//if read beyong end of file
     return;//quit
while(x != EOF) {//while x is not beyong End Of File
    cryp=x-0xFACA;//the crypting
    out<<(char)cryp;//print the crypted char
    in.get(x);//get another char
    if(in.eof())//if read beyong end of file
         break;
}


should rather be changed to something like this (also change the other parts in your code which look like the above code):

if(!in.is_open())
   return;
while(in.get(x)) {//while x is not beyong End Of File
    cryp=x-0xFACA;//the crypting
    out<<(char)cryp;//print the crypted char
}


BTW, when I write encryption/decryption functions, I would let them return some value (for examplebool, or the number of bytes encrypted/decrypted) in order to check whether the encrypting/decrypting went fine.
Also the use of old-style header files, and the mixing of two I/O libraries is annoying me:
Change this:

#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <stdio.h>


to this:

#include <iostream>
#include <fstream>


(and update your code to not use the C style I/O), also read this which is about (standard) alternatives to system("pause"); .

Where are you freeing this memory? char *cool=new char[20];

This code fragment will allow an array overrun, when more than 19 characters are entered:

case 'e':
   cout << "Enter the file name: \nExample: \"file\".txt\n";
   cin >> cool;
   doCrypt(cool);
   break;


, so rather change cin >> cool; to: cin.getline(cool, 20); .

In your password function I see these lines:

cout << "Enter in the encryption password: \n";
gets(string1);


However the fist is correct, but the second is a bad coding practice, it allows (again) the array to be overrun, array overruns are bad, because your program just starts writing in some memory past the end of the array, which can cause (in the best case) your program to crash, or in a more worse case: the entire operating system to crash (though most operating systems today offer memory protection, you shouldn't rely on this when writing your program).
Quick fix: change gets(string1); to: cin.getline(string1, 50) .

Edit::I changed the red to cryp=(x * 37)% 200; //the crypting
and the green to decr=(x * 173)% 200;//the dectypring
It encrypts fine but in decryption i think it gets confused. Oops! I just checked the code you posted, because I asked you to post the changed code, but the encryption/decryption routines were not changed in the code, I'll change these and test the whole thing again.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Your encryption algorithm should work fine, I tested it using the following code:

#include <iostream>
#include <climits>

int main()
{
    int encrypt;
    int decrypt;
    
    for(int i=0; i<CHAR_MAX; ++i) {
        encrypt = (i * 37) % 200;
        decrypt = (encrypt * 173) % 200;
        
        if(decrypt != i)
            std::cout << "ASCII: " << i << ": NOT OK" << std::endl;
        else
            std::cout << "ASCII: " << i << ": OK" << std::endl;
    }
    
    return 0;
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Your encryption algorithm should work fine, I tested it using the following code:

#include <iostream>
#include <climits>

int main()
{
    int encrypt;
    int decrypt;
    
    for(int i=0; i<CHAR_MAX; ++i) {
        encrypt = (i * 37) % 200;
        decrypt = (encrypt * 173) % 200;
        
        if(decrypt != i)
            std::cout << "ASCII: " << i << ": NOT OK" << std::endl;
        else
            std::cout << "ASCII: " << i << ": OK" << std::endl;
    }
    
    return 0;
}


Dear Sir,
I havent used C code for last 5 years, one of my friend helps me in solving the problem,

If possible kindly change my all program ( the deficiencies ) and put the new code that you made and tested in the old program so that i can see how it works, to embed the new code in old program is difficult then writing a new one,

Please put all the new code in the old program and a new program which will do enc dec with the different keys will be made,

Then i will be able to tet and verify.

I think most of the problem is solved.

But untill i see all the code in a program at its own place i cant say anything

Thanks in advance!

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 

Dear Sir,
I want to get plain text from a file
then
encrypt it and save it in a file

and then
decrypt it and save in the file
and compare the decrypted text with the orignal plain text

so that i can check wether the program is working fine or not

please help me out of this big problem

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 

Dear Sir, I want to get plain text from a file then encrypt it and save it in a file

and then decrypt it and save in the file and compare the decrypted text with the orignal plain text

so that i can check wether the program is working fine or not

please help me out of this big problem

First of all I want you to NOT PM me with your questions to do it for you, it won't work, I'm a free person and I'm free to do what I want, and not to do what I don't want (let that be clear).
What I want is to help you, what I don't want is spend my time to create a full-fledged encryption solution for you (or in other words: I rather provide paths to the solution, not the entire solution itself), so that you can turn it in, and pass (or fail :P) using my work, that wouldn't be fair, not? The point of the assignments you get is to show off your knowledge, show what you can. This means that you have to do the work, passing using someone else's work won't get you far in real life. The point is that you do the entire thing yourself (with some small help and hints from the forum members (the so-called "gurus") here), and that you finally (when you finished the program) can say: I did it!

[edit]
So my immediate advice would be: go on and start to clean up and fix your current code using the tips I provided in my previous post.
If you're really lost, you will maybe want to consider to rewrite the whole program, but that should be the last option on your list (you can fix it, if you want it).
When you've fixed your code, post down your new code and come up with specific questions in case you got stuck at some point.
[/edit]

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Dear Sir,
You are very right and i admit with the core of my heart what u said.


I am a Network/System Administrator and is far far away from the programming languages as my field of profession is different.


I tried to run your small program and it run successfuly

but the problem is that u r using oops object oriented programming and i told u sir that i m new to c basically a biginer.

So thats why i said that
I am thankful for your consideration thats why i m really obliged.


Thanks!

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 
but the problem is that u r using oops object oriented programming and i told u sir that i m new to c basically a biginer.

As far as I know I only usedcout, which you also did (you even mixed C with C++ I/O (which is also OOP), so I don't see where the problem is :)

Do you want to rewrite the whole program in C or what (as your thread seems to be in the C++ forum) ?

And I don't care you're a beginner, as long as you're willing to put effort in it I'm happy :)

[edit]
Just out of curiosity, you're a Network/System Administrator, but why is there need to write your own encryption program then?
Can't you just use something which you can safely rely on?
Maybe something like TrueCrypt ?
[/edit]

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

very right sir finally i will be running this program in linux
by now i m running this program in dev c++

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 
very right sir finally i will be running this program in linux

Is that the only reason you want to undertake all the work to write an encryption program?
Even if TrueCrypt is available for Linux as well ?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

yes sir u r very right there are may option as well
but i m working on ns2 for simulation wireless sensor network
i want to put this in n ode and base station algo
i want to use asymetric key end dec which is lesser used in researh work in wsn

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 

by now i am using this program

#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <stdio.h>


bool password();

void doCrypt(char *cool)
{
     int cryp;//the main int this function uses
    char x;//to get the chars from the file
    ifstream in;//the input stream
    ofstream out;//the output stream
    in.open(cool);//opening the input file
    if(in.fail()){//check if it's not there
         cout<<"Couldn't open the IN file\n";//if it's not, print
        return;
    }
    cout<<"enter the OUT file name: ";//enter the output filename
    cin>>cool;//get it
    out.open(cool);//open the output file
    in.get(x);//get the first char of input
    if(in.eof())//if read beyong end of file
         return;//quit
    while(x != EOF){//while x is not beyong End Of File
         cryp=(x*37)%200;//the crypting
        out<<(char)cryp;//print the crypted char
        in.get(x);//get another char
        if(in.eof())//if read beyong end of file
             break;
    }
    in.close();
    out.close();
    return;
}
void deCrypt(char *daf)
{
     int decr;//the main int
    char x;//the input char
    ifstream in;//input stream
    ofstream out;//output stream
    in.open(daf);//open the inout stream
    if(in.fail()){//see if it's not there
         cout<<"Error couldn't open the IN file\n";//print
        return;
    }
    cout<<"Enter the OUT file name: ";//ask for the out filename
    cin>>daf;
    out.open(daf);//open the outfile
    in.get(x);//get the input char
    if(in.eof())//if read beyong end of file
         return;
    while(x!=EOF){//while x is not beyong end of file
         decr=(x*173)%200;//the dectypring
        out<<(char)decr;
        in.get(x);//get the next char
        if(in.eof())//if read beyong the eon of file
             break;
    }
    return;
}
int main(void)
{
    if (password()) cout << "Access Granted: \n";
    else
    {
    cout << "You are not Authorized to use this program!\n";
    system("pause");
    return 0;
    }
    char *cool=new char[20];//the name pointer
    char nav;              //"navigation char"
    cout << "*---------------------------------------------*\n";
    cout << "| This is an encryption program.  I'm not     |\n";
    cout << "| taking credit for this.  This was a         |\n";
    cout << "| recent submission on planet-source-code     |\n";
    cout << "| I just added password protection to it.     |\n";
    cout << "*---------------------------------------------*\n\n";
    system("pause");
    cout << "\n\n";
    cout << "Use \"m\" for the Menu\n";//print instructions
     while(true){
         cin >> nav;
         switch(nav){
             case 'm':
                 cout << "COMMAND\tFUNCTION\n";
                cout << "e      \tencrypt\n"
                          "d      \tdecrypt\n"
                        "m      \t menu \n"
                        "q      \t quit \n";
                break;
             case 'e':
                 cout << "Enter the file name: \nExample: \"file\".txt\n";
                cin >> cool;
                doCrypt(cool);
                break;
            case 'd':
                 cout << "Enter the file name: ";
                cin >> cool;
                deCrypt(cool);
                break;
            case 'q':
                 exit(0);
                 break;
            default:
                 cout << "I do not understand you!\n";
                break;
        }
    }
}

bool password()
{
     char string1[50];
     cout << "Enter in the encryption password: \n";
     gets(string1);

     if (strcmp(string1, "outlaw")) return 0;
     return 1;
}


######################################

the plain test file contains line

i am new to c programming

after enc it gives

U¸½!¸F‰¸\k¸?¸k½!!UF

after decryting this enc file

it doesnt give req out put means the real plaint test which is

i am new to c programming
instead it gives
iÈAmÈnEwÈtoÈcÈProgrAmming

and this is the problem i m facing

please solve this for me
after that i will organize the program as u said earlier

my priority is the output is the result

which i am unable to get

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 

Dear tux4life,
i have made changes u ssuggested i got error in some and confused about enc and dec as no output to file and also read my query number 16

best regards!

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 

any help will be applreciated by helpers

khanalone1
Newbie Poster
22 posts since Aug 2009
Reputation Points: 6
Solved Threads: 0
 

How I would go trough the process of designing such a program:
Create a simple menu, which will allow the user to choose whether he wants to encrypt or decrypt. (you've done this)

Create two separate functions: one to encrypt and one to decrypt.
(you've done this)

But here's where my approach starts to differ from yours:
As I am, I first of all test it without all the file handling, this means that I would let the user enter a line of text on the keyboard, and print out the encrypted/decrypted sentence.
When this works correctly, I move on to writing/changing code to be able to operate (encrypt / decrypt) successfully on files.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Seems similar to this thread. Tux's advice is good. Split everything up into smaller tasks.

http://www.daniweb.com/forums/thread221572.html

I'll add the same advice that I gave the person in the thread I linked. Don't hard-code the keys (37, 173, 200). Have variables for them and give them descriptive names. And when you're debugging, make them easy.

37 * 173 = 6401.
6401 % 200 = 1

So that's good. But things are easier to check if you have something like (2, 3, 5):

2 * 3 = 6
6 % 5 = 1

The math is much easier to do in your head. Have a nice easy input file, where it's short enough and the math is easy enoguh to do on scratch paper. So encrypt "abc" or something, then check it, then decrypt it. It's easier to debug that way.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You