Here is a program that encodes and decodes a text file. What I need to do
is write a C++ program that requests 3 different file names. One filename
is for the source file to be encoded, another is the name of the 'encoded'
output file, and the final filename is an offset file. On a character by
character basis, the program needs to look at the first character of the
source file and offset it by the first character in the offset file. The
second character in the source is offset by the second character in the
secret offset file. I need to deal with the possibility that your source
is longer than the offset file in which case it just needs start over or
repeat the secret offset file from the beginning.
The one thing I am having trouble with is offsetting the source file with
the offset file. Here is my code. I need to implement the offset file. Any
ideas?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
#define cypher (char) ASCII + 1
#define decypher (char) ASCII - 1
int main()
{
char ASCII;
cout << "1) Encode\n2) Decode\n<q to quit>: ";
int coding;
cin >> coding;
if(coding == 1)
{
ifstream source;
ofstream clone;
source.open("la zy_dog.txt", ios::in);
clone.open("cyp her.txt", ios::out | ios::trunc);
while(!source.e of())
{
char NewASCII;
ASCII = source.get();
if (source.fail())
return 0;
NewASCII = cypher;
clone.put(NewAS CII);
}
source.close();
clone.close();
return 0;
}
else if(coding == 2)
{
ifstream origin;
ofstream copy;
origin.open("cy pher.txt", ios::in);
copy.open("decy pher.txt", ios::out | ios::trunc);
while(!origin.e of())
{
char NewASCII;
ASCII = origin.get();
if (origin.fail())
return 0;
NewASCII = decypher;
copy.put(NewASC II);
}
origin.close();
copy.close();
return 0;
}
else
return 0;
} 4 2565
I dont see the point of that offset file, i get what you mean by it, but its
not a good way for encryption.
in ur earlier post u asked "I want to do it very simply, as in adding 1 to
every ASCII character";
maybe you better stick with ur earlier suggestion, which is simpler in
implementation.
so you can change encoding for example :
while(!source.e of())
{
char char_read;
char_read = source.get();
clone.put(++cha r_read);
}
"Gactimus" <ga******@xrs.n et> wrote in message
news:bE******** ********@tornad o.tampabay.rr.c om... Here is a program that encodes and decodes a text file. What I need to do is write a C++ program that requests 3 different file names. One filename is for the source file to be encoded, another is the name of the 'encoded' output file, and the final filename is an offset file. On a character by character basis, the program needs to look at the first character of the source file and offset it by the first character in the offset file. The second character in the source is offset by the second character in the secret offset file. I need to deal with the possibility that your source is longer than the offset file in which case it just needs start over or repeat the secret offset file from the beginning.
The one thing I am having trouble with is offsetting the source file with the offset file. Here is my code. I need to implement the offset file. Any ideas?
#include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std;
#define cypher (char) ASCII + 1 #define decypher (char) ASCII - 1
int main() { char ASCII; cout << "1) Encode\n2) Decode\n<q to quit>: "; int coding; cin >> coding;
if(coding == 1) { ifstream source; ofstream clone;
source.open("la zy_dog.txt", ios::in); clone.open("cyp her.txt", ios::out | ios::trunc);
while(!source.e of()) { char NewASCII; ASCII = source.get(); if (source.fail()) return 0; NewASCII = cypher; clone.put(NewAS CII); }
source.close(); clone.close(); return 0; }
else if(coding == 2) { ifstream origin; ofstream copy;
origin.open("cy pher.txt", ios::in); copy.open("decy pher.txt", ios::out | ios::trunc);
while(!origin.e of()) { char NewASCII; ASCII = origin.get(); if (origin.fail()) return 0; NewASCII = decypher; copy.put(NewASC II); }
origin.close(); copy.close(); return 0; }
else return 0; }
"Someonekic ked" <so***********@ comcast.net> wrote in
news:cJ******** ************@co mcast.com: I dont see the point of that offset file, i get what you mean by it, but its not a good way for encryption.
It may not be a good way but it's the way I have to do it.
in ur earlier post u asked "I want to do it very simply, as in adding 1 to every ASCII character";
Yeah, I got that program working, but in a different way than you suggested.
the following code read all characters from someFile.dat, and put them in an
array ( called data ), the length of that array is the total number of
characters in the file plus 1; the last character in the array is the null
character :
ifstream inData("someFil e.dat",ios::bin ary);
inData.seekg(0, ios::end); // move the pointer to end of file.
int length = inData.tellg(); // reads position of the pointer.
inData.seekg(0) ; // move pointer to beginning of file.
char* data = new char; // declaring the array.
inData.read(dat a,length); // filling the array.
data[length] = '\0'; // setting last character of the array to
null.
the benefits of that, you read all characters at once, and then u can
manipulate them like you want without accessing the source file, and for
example if you do
cout << data << endl; // you will get all the source file.
you can use this twice to get two different arrays, source_data of the
source ( of length source_length) and offset_data the offset file ( of
length offset_length).
then to write characters to the encoded file,
int count = 0;
while (count < source_length)
encoded << source_data[count] << offset_data[ count % offset_length ];
"Gactimus" <ga******@xrs.n et> wrote in message
news:bE******** ********@tornad o.tampabay.rr.c om... Here is a program that encodes and decodes a text file. What I need to do is write a C++ program that requests 3 different file names. One filename is for the source file to be encoded, another is the name of the 'encoded' output file, and the final filename is an offset file. On a character by character basis, the program needs to look at the first character of the source file and offset it by the first character in the offset file. The second character in the source is offset by the second character in the secret offset file. I need to deal with the possibility that your source is longer than the offset file in which case it just needs start over or repeat the secret offset file from the beginning.
The one thing I am having trouble with is offsetting the source file with the offset file. Here is my code. I need to implement the offset file. Any ideas?
#include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std;
#define cypher (char) ASCII + 1 #define decypher (char) ASCII - 1
int main() { char ASCII; cout << "1) Encode\n2) Decode\n<q to quit>: "; int coding; cin >> coding;
if(coding == 1) { ifstream source; ofstream clone;
source.open("la zy_dog.txt", ios::in); clone.open("cyp her.txt", ios::out | ios::trunc);
while(!source.e of()) { char NewASCII; ASCII = source.get(); if (source.fail()) return 0; NewASCII = cypher; clone.put(NewAS CII); }
source.close(); clone.close(); return 0; }
else if(coding == 2) { ifstream origin; ofstream copy;
origin.open("cy pher.txt", ios::in); copy.open("decy pher.txt", ios::out | ios::trunc);
while(!origin.e of()) { char NewASCII; ASCII = origin.get(); if (origin.fail()) return 0; NewASCII = decypher; copy.put(NewASC II); }
origin.close(); copy.close(); return 0; }
else return 0; }
"Gactimus" writes: Here is a program that encodes and decodes a text file. What I need to do is write a C++ program that requests 3 different file names. One filename is for the source file to be encoded, another is the name of the 'encoded' output file, and the final filename is an offset file. On a character by character basis, the program needs to look at the first character of the source file and offset it by the first character in the offset file. The second character in the source is offset by the second character in the secret offset file. I need to deal with the possibility that your source is longer than the offset file in which case it just needs start over or repeat the secret offset file from the beginning.
The one thing I am having trouble with is offsetting the source file with the offset file. Here is my code. I need to implement the offset file. Any ideas?
<snip>
The usual way to encipher along these lines it to do an exclusive or.
enc = plain ^ key;
Where enc is the enciphered output char, plain is from the input file and
key is from what you call "offset".
You can then retrieve the original text by writing:
plain = enc ^ key;
where plain is the original, that is, deciphered, text.
There are several ways to handle the key file being too short. The easiest
for a beginner might be to simply close and re-open the file. If that
doesn't appeal to you, you can "seek" within a file.
----
If you don't like the above, look very closely at what "offset" means. You
have defined it internally or someone else has defined it for you. In your
solution, note that the "signedness " of char is *not* defined in the C++
standard. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kamilche |
last post by:
I've looked at a few alternatives for encryption with Python, and
didn't come up anything very speedy.
I've written an encryption algorithm in pure Python that can process
22 megs of data a second. I know it's not secure, but it should be
enough to ward off casual hacking. Does someone know of something
speedier?
--Kamilche
|
by: Christopher Benson-Manica |
last post by:
Is there anything wrong with my attempt (below) at implementing
something resembling a smart pointer?
template < class T >
class SmartPointer
{
private:
T *t;
public:
|
by: Brady |
last post by:
Hi,
I'm having a problem reading and writing to a file. What I'm trying
to do is read a file, modify the portion of the file that I just read,
and then write the modified data back to the same location in the
file.
What is happening, is I can read the file, either in entirety or only
part of it. And no matter what I try setting the position to, using
fsetpos, it always gets set back to the very beginning of the file. I
|
by: Bonj |
last post by:
I was in need of an encryption algorithm to the following requirements:
1) Must be capable of encrypting strings to a byte array, and decyrpting
back again to the same string
2) Must have the same algorithm work with strings that may or may not be
unicode
3) Number of bytes back must either be <= number of _TCHARs in *
sizeof(_TCHAR), or the relation between output size and input size can be
calculated simply. Has to take into account the...
|
by: Jen |
last post by:
I'm using Microsoft's own VB.NET FTP Example:
http://support.microsoft.com/default.aspx?scid=kb;en-us;832679
I can get the program to create directories, change directories, etc.,
but I can't get it to upload a file to the FTP server. I just get a
"Cannot connect to remote server" error after this TRY:
s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
| |
by: RobKinney1 |
last post by:
Hello everyone... This may sound really stupid, but it is something I have
been working on all day and haven't found a solution yet.
If you go into Notepad and try the keystroke ALT+4, you get a nice little
diamond. Infact, you can do this all the way up to 127 showing different
characters including those similar with the ASCII table for letters and
numbers.
"Oh," I thought, "this could be the simple way for very simple encoding of...
|
by: hazmaz |
last post by:
I've been trying to write a basic encryption algorithm, not really
intended for use as a secure means of protecting data, but to help me
learn things like bitwise operations.
Just wondering, how easy would it be to crack what I've come up with:
FILE * inFile;
FILE * outFile;
this->inputfile = infn;
int k = 0;
|
by: John Williams |
last post by:
I've written a simple program to do XOR encryption as my first foray
into understanding how encryption works. The code compiles fine, however
it segmentation faults on every run. using gdb to debug it let me
narrow the problem down to the Cipher function I think it faults at line
84 or 85. The program makes it's first read/cipher/write pass without
issue but the second pass kills it. Using gdb to print the variables
left showed me the...
|
by: Wilson |
last post by:
i am learning to program using c++ and was set a task of making a
simple encryption algorithim. I choose to start with one where simply
each letter is replaced with its equivilent in the alphabet when
written backmards, i am hoping to make this more complex soon. Below
is the program i wrote but it does not work, it simply returns the
exact same text you enter. Could you please advise on how to sort
this, and also suggest any ways of...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |