Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

encrypting files 2

Status
Not open for further replies.

cyprus106

Programmer
Apr 30, 2001
654
I'm looking for a simple way to encrypt files through my program, (text in particular), any ideas, suggestions or code would really be appreciated! Im using bcb 5 enterprise edition.

Thanks alot! Cyprus
 
I have a couple of routines that I use to encrypt/decrypt passwords for many of the programs that I write. It is very simple and, at best, will only keep the honest people honest. You're welcome to it, if it will help:

// encrypt a password from text to add-index
char *EncryptPassword(char *inPasswd)
{
static char pwBuffer[64];
int w, x;
// init
memset(pwBuffer, 0, sizeof(pwBuffer));
x = strlen(inPasswd);
if (x > 0)
{ // loop through the string, adding the index
for (w = 0; w < x; w++)
pwBuffer[w] = (char)(inPasswd[w] + (w + 1));
}
return(pwBuffer);
}
//---------------------------------------------------------------------------

// decrypt a password from text that was add-index encrypted
char *DecryptPassword(char *inPasswd)
{
static char pwBuffer[64];
int w, x;
// init

memset(pwBuffer, 0, sizeof(pwBuffer));
x = strlen(inPasswd);
if (x > 0)
{ // loop through the string, subtracting the index
for (w = 0; w < x; w++)
pwBuffer[w] = (char)(inPasswd[w] - (w + 1));
}
return(pwBuffer);
}
//---------------------------------------------------------------------------

 
Last I looked (about 4 years ago), there was open source for blowfish and some other encryption methods on the web. Also, when I was in college, I took a class on encryption and what I would suggest is PGP (Pretty good protection / privacy *sometheing like that*). However... if you make up a simple method as suggested above by pmaia, that should be fine as well. Remember a character ranges from 0-255 and a bunch of those numbers are not used so you could double map letters. Another method was using the unused character to represent &quot;st&quot; &quot;ch&quot; or any other common occurance 2 - 3 letter combinations. Hope this gives you some ideas.

matt
 
Forgot to mention:

My sophmore year as a comp-sci major, I had to write an encryption algorithm. I used a binary tree and set up the nodes so the leaves were the letters. I would first parse the file and get a character at a time. I would then set the tree up so the highest frequency character would be closeset to the root, second was secondmost from the root...etc. For every move left in the tree a '0' was output to a file and for every move right a '1'. What this did was first figure out how many unique characters were in a file and 2, create a unique binary representation of the character. This is a very broad explanation of what it did but just another idea to get you along the path of encryption. (On a side note... Dr. Dobbs Journal has published a cd-rom on the topic of encryption which I purchased while in college. It contains about 7 books that would cost you well over $200 and possibly $300 to purchase in the stores) It offers great explanations on encryption techniques and is highly suggested by me if encryption is going to be the core of you career. When I bought it, it was $79 *i think*.. but well worth it!)

Matt
 
you could use the simplest & cheapest method of doing it by using an XOR pattern

#define _PATTERN 0xE7
for(i = 0; i < strlen(szCharString); i++)
szCharString ^= _PATTERN;

this is a VERY simple example, but it works, and you just need to use the same for loop to decrypt it later
 
The 0 left, 1 right method (Zyrenthian) is how Huffman compression works as well, killing two birds with one stone there i guess
 
Thank you! I completly forgot the name and it was bugging the heck out of me. Been a while since I was in the algorithm class :) And the Huffman algorithm was used for compression when I was in the class. The encryption part of it was your basic encryption of A = B B = C etc so very crackable with brute force.

Thank you once again for the name of the algorithm
Matt
 
thanx, but do you have any methods to break the text down letter by letter? i tried strstr, but didnt quite know how to make a pointer to each individual letter. Cyprus
 
If you are reading in from a file you can do a cin.get() to read it in character by character. With a CFile you can do parse it character by character as well. If on the other hand you have a buffer... just loop through it a character at a time. That is the easiest way i can think of off the top of my head.

Matt
 
well if you're using a the AnsiiString type you'll need to do this:

char *tmp = SomeString.c_str();

then tmp[someindex] to access each letter

 
If you have pgp you can use the pgp plugin to do this job.

hnd
hasso55@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top