•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 429,968 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,651 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 921 | Replies: 2
![]() |
•
•
Join Date: Feb 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Hello to all programmers:
I have been working on the following program but I am encountering some difficulties any help whatsoever would be greatly appreciated. My difficulty is that when I run the program, it does not perform the encryption nor decryption on the file I type in. Is something wrong with my functions? I can't figure the problem!
My instructions are:
Write a c++ program,
1. Prompts the user for a choice of encrypt or decrypt, prompts for the input and output filenames
2. has a function for encryption
a. generates a random key
b. stores the key in the encrypted file
c. encrypts the data from the input file by performing xor of the key with every byte from the input file
3. has a function for decryption
a. reads the key from the encrypted file
b. decrypts the data from the input file by performing xor of the key wit every byte from the file
4. performs the encryption or decryption based on the user's choice.
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
#include<fstream>
usingnamespace std;
void encrypt(char inname[80], char outname[80]);
void decrypt(char inname[80], char outname[80]);
int decryptencrypt;
char inname[80], outname[80];
void main()
{
srand((unsigned)time(NULL));
cout << "Enter 1 to decrypt" << endl;
cout << "Enter 2 to encrypt" << endl;
cin >> decryptencrypt;
cout << "Enter the input filename" << endl;
cin >> inname;
cout << "Enter the output filename" << endl;
cin >> outname;
if(decryptencrypt == 1)
{
decrypt( inname, outname);
}
else if(decryptencrypt == 2)
encrypt( inname, outname);
}
void encrypt(char inname[80], char outname[80]){
ifstream in;
ofstream out;
in.open(inname);
out.open(outname);
char key = rand() % 255;
out << key;
char c;
in.get(c);
while(in != 0)
{
out << (c ^ key);
in.get(c);
}
return;
}
void decrypt(char inname[80], char outname[80]){
ifstream in;
ofstream out;
in.open(inname);
out.open(outname);
char key;
in.get(key); // Read the key out of the encrypted file
char c;
in.get(c);
while(in != 0)
{
out << (c ^ key); // Do XOR to restore it to its original values
in.get(c);
}
return;
}
I have been working on the following program but I am encountering some difficulties any help whatsoever would be greatly appreciated. My difficulty is that when I run the program, it does not perform the encryption nor decryption on the file I type in. Is something wrong with my functions? I can't figure the problem!
My instructions are:
Write a c++ program,
1. Prompts the user for a choice of encrypt or decrypt, prompts for the input and output filenames
2. has a function for encryption
a. generates a random key
b. stores the key in the encrypted file
c. encrypts the data from the input file by performing xor of the key with every byte from the input file
3. has a function for decryption
a. reads the key from the encrypted file
b. decrypts the data from the input file by performing xor of the key wit every byte from the file
4. performs the encryption or decryption based on the user's choice.
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
#include<fstream>
usingnamespace std;
void encrypt(char inname[80], char outname[80]);
void decrypt(char inname[80], char outname[80]);
int decryptencrypt;
char inname[80], outname[80];
void main()
{
srand((unsigned)time(NULL));
cout << "Enter 1 to decrypt" << endl;
cout << "Enter 2 to encrypt" << endl;
cin >> decryptencrypt;
cout << "Enter the input filename" << endl;
cin >> inname;
cout << "Enter the output filename" << endl;
cin >> outname;
if(decryptencrypt == 1)
{
decrypt( inname, outname);
}
else if(decryptencrypt == 2)
encrypt( inname, outname);
}
void encrypt(char inname[80], char outname[80]){
ifstream in;
ofstream out;
in.open(inname);
out.open(outname);
char key = rand() % 255;
out << key;
char c;
in.get(c);
while(in != 0)
{
out << (c ^ key);
in.get(c);
}
return;
}
void decrypt(char inname[80], char outname[80]){
ifstream in;
ofstream out;
in.open(inname);
out.open(outname);
char key;
in.get(key); // Read the key out of the encrypted file
char c;
in.get(c);
while(in != 0)
{
out << (c ^ key); // Do XOR to restore it to its original values
in.get(c);
}
return;
}
You need to start filling your program with a load of couts, so you can find out where it's going wrong.
Just as a side note, you may need to read and write in binary mode, we'll get to that later though.
In the mean time find out if each part is doing what it should be.
Just as a side note, you may need to read and write in binary mode, we'll get to that later though.
In the mean time find out if each part is doing what it should be.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
> usingnamespace std;
Whatever crappy code colouring tool you're using, stop using it and use the board code tags.
Few people are going to be bothered to trawl through unindented code which isn't even compilable, and has been corrupted in transmission.
http://www.daniweb.com/techtalkforum...cement8-3.html
> help with encryption! asap
Your urgency isn't our problem
http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
As for the rest of the problem, start with a program that simply copies one file to another without any changes. If you can't manage that, then you're wasting time trying to do encryption.
Oh, and main() really does return an int. Use it or be doomed.
Whatever crappy code colouring tool you're using, stop using it and use the board code tags.
Few people are going to be bothered to trawl through unindented code which isn't even compilable, and has been corrupted in transmission.
http://www.daniweb.com/techtalkforum...cement8-3.html
> help with encryption! asap
Your urgency isn't our problem
http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
As for the rest of the problem, start with a program that simply copies one file to another without any changes. If you can't manage that, then you're wasting time trying to do encryption.
Oh, and main() really does return an int. Use it or be doomed.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- How to create SHA1 Encryption program (VB.NET)
- AIM encryption (Viruses, Spyware and other Nasties)
- Storing passwords using reversible encryption in Active Directory (Windows NT / 2000 / XP / 2003)
- c++ windows program to demonstrate encryption (C++)
- No Encryption in IE 6.0/XP Home (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Just started C++. Problems with MS Visual C++ 2005 Express.
- Next Thread: Help in terminating a program



Linear Mode