I'm trying to make 2 functions, one in C and one in C++ that reads a text file and doing XOR Encryption on it.
1. I get compilation errors with the C++ code.
2. The C code gets into a infinite loop and I know why but I can't fix it.
Main:
Code: Select all
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
using namespace std;
int XOR_Encryption_C(char *, char []);
bool XOR_Encryption_CPP(char *, char []);
void main()
{
// clrscr();
char *file_Path = (char *)calloc(sizeof(char), 255);
printf("Enter file's path (C):\n");
scanf("%s", file_Path);
if (!XOR_Encryption_C(file_Path, "a"))
{
printf("Error! (C)");
getch();
}
printf("Enter file's path (C++):\n");
scanf("%s", file_Path);
if (!XOR_Encryption_CPP(file_Path, "a"))
{
printf("Error! (C++)");
getch();
}
}Code: Select all
int XOR_Encryption_C(char *FilePath, char key[])
{
FILE *fp = fopen(FilePath, "r+"); // The file must exist
if (fp)
{
char ch;
int i = 0;
while((ch = fgetc(fp) != EOF))
{
if (i < strlen(key))
ch ^= key[i++];
else
{
i = 0;
ch ^= key[i++];
}
fseek(fp, -1L, SEEK_CUR); // DOESN'T WORK!!!
fputc(ch, fp);
}
fclose(fp);
}
else
return 0;
return 1;
}If this is fixed the whole C version should be working just fine.
C++ Version:
Code: Select all
bool XOR_Encryption_CPP(char *FilePath, char key[])
{
char ch;
int i = 0;
fstream fp(FilePath, ios::in | ios::out | ios::nocreate);
while(!fp.eof())
{
fp.get(ch);
if (i < strlen(key))
ch ^= key[i++];
else
{
i = 0;
ch ^= key[i++];
}
fp.seekp(fp.tellg()-1);
fp << ch;
}
fp.close();
return true;
}Code: Select all
------ Build started: Project: test, Configuration: Debug Win32 ------
Compiling...
XorEncryption.cpp
.\XorEncryption.cpp(64) : error C2039: 'nocreate' : is not a member of 'std::basic_ios<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
.\XorEncryption.cpp(64) : error C2065: 'nocreate' : undeclared identifier
.\XorEncryption.cpp(75) : error C2666: 'std::fpos<_Statetype>::operator -' : 3 overloads have similar conversions
with
[
_Statetype=_Mbstatet
]
D:\Program Files\Microsoft Visual Studio 8\VC\include\iosfwd(98): could be 'std::fpos<_Statetype> std::fpos<_Statetype>::operator -(std::streamoff) const'
with
[
_Statetype=_Mbstatet
]
D:\Program Files\Microsoft Visual Studio 8\VC\include\iosfwd(75): or 'std::streamoff std::fpos<_Statetype>::operator -(const std::fpos<_Statetype> &) const'
with
[
_Statetype=_Mbstatet
]
or 'built-in C++ operator-(std::streamoff, int)'
while trying to match the argument list '(std::fpos<_Statetype>, int)'
with
[
_Statetype=_Mbstatet
]
Build log was saved at "file://d:\Programming\Projects\Active Projects\Visual Studio 2005\Projects\test\test\Debug\BuildLog.htm"
test - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any criticism on the code itself, suggestions to make it better are welcome!
Thanks to everyone who will help me with this problem, I've been trying to fix it myself for few hours now, I hope I'll finally get my answers.. *relief*

