Archived

This topic is now archived and is closed to further replies.

need a small program code to encrypt data

This topic is 5146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i need small program to do this open file 1 open file 2 file 2 is my password(1024 bytes),file 1 is the data file to encrypt(any size) now add file 1 with file 2(file 1 + file 2) if file 1 is larger than file 2 ,loop again. output the new encrypted file pls help [edited by - aru365 on November 8, 2003 5:33:04 PM]

Share this post


Link to post
Share on other sites
Open the source and key file for reading in binary, and open the destination file for writing in binary.

Read a byte from the source and key file. If you read EOF from the key, seek back to the start and read from it again. If you still read EOF, the key is too short. You can't encode the file. If you read EOF from the source, you're done, otherwise add the bytes together and write it to the destination file, and repeat.

Unencrypting is the same process except instead of adding, you subtract.

If you xor the bytes together instead, encoding a file once will encode it, and encoding it a second time with the same key will unencode it.

In C (you didn't say what language) the functions you'll need are fopen, fclose, fgetc, fputc, (or fread and fwrite) and fseek.

[edited by - smart_idiot on November 8, 2003 7:54:16 PM]

Share this post


Link to post
Share on other sites
I'm more familiar with how to do things in C++ so I wrote a little code snippet to show the idea here.


#include <fstream>
using namespace std;
int main()
{
ifstream source, key;
ofstream target;

source.open("source.txt", ios::in|ios::binary);
key.open("key.txt", ios::in|ios::binary);
target.open("target.txt", ios::out|ios::binary|ios::trunc);
char sourceChar, keyChar, temp;

//should check that they open ok but let's

//assume they did open ok

key.seekg(0, ios::end);
source.seekg(0, ios::beg);

//bail out if key file is 0 byte length

if(key.tellg()==0)
{
key.close();
source.close();
target.close();
exit(1);
}//if


key.clear();
key.seekg(0, ios::beg);
while(!source.eof())
{
source.read(reinterpret_cast<char *>(&sourceChar), sizeof(char));
//rewind key if eof

if(key.eof())
{
key.clear();
key.seekg(0, ios::beg);
}//if

key.read(reinterpret_cast<char *>(&keyChar), sizeof(char));
temp=sourceChar+keyChar;
target.write(reinterpret_cast<char *>(&temp), sizeof(char));
}//while


key.close();
source.close();
target.close();
return 0;
}//main







--{You fight like a dairy farmer!}

[edited by - Greatwolf on November 9, 2003 4:47:03 AM]

Share this post


Link to post
Share on other sites
thanks greatwolf for the code


while compiling this code i get lots errors in turbo c++(dos),

doing something wrong?

[edited by - aru365 on November 9, 2003 6:46:07 AM]

Share this post


Link to post
Share on other sites
while compiling in vc++

c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal error C1189: #error : "eh.h is only for C++!"
Error executing cl.exe.

prognew.obj - 1 error(s), 0 warning(s)


any idea?

Share this post


Link to post
Share on other sites
Depending upon the nature of your data, I would be very concerned regarding the security of the data. Any reasonable cryptanalytic technique would be able to "crack" your cypher. You should look into encryption techniques like Blowfish, IDEA, Triple-DES etc... You should be able to find some free libraries if you google.

Mike

Share this post


Link to post
Share on other sites