Encrypt/Decrypt

S

Spikinsson

I'm looking for a good decrypt/encrypt function, all I want is a function in this form:

char* encrypt(char* normal)
{
...
return encrypted;
}

and

char* decrypt(char* encrypted)
{
...
return normal;
}

I realize there would be memory allocation and I would have to free() or delete [] after
running the functions.
My problem is I don't know the first thing about any encryption method, so actually I'm
searching for already made functions that are similar to the two mentioned above. I have
really tried to use google to find such functions but I failed, I would very much
appreciate if anybody could post a function, an usefull tutorial or a link to some
source...
 
S

Spikinsson

"Victor Bazarov"
Have you tried comp.sources.wanted?
No, but will do...
BTW, I went on Google and entered 'encrypt decrypt C++ source'
(without the quotes, of course) and immediately got more than
9000 pages. The very first one had a whole project for AES
(whatever that is) encryption/decryption. I don't believe that
among those 9000 links there weren't _any_ that you could use.
I need ine that doesn't require MFC, string classes or additional DLL's and that can
encrypt/decrypt strings, not files...

P.S. And, just to clarify, we don't do homeworks.
I understand and I'm not asking you for this, but as far as the encryption goes it is
really difficult to find a usefull website that doesn't get too complicated...
 
E

emerth

The openssl library (www.openssl.org) has many
encryption algorithms and is GPL'd (or some similar
licence). It works on Linux/UNIX/*BSD and Win32.

Here is some code that I put together on Linux, it
should run on Win32 with header name changes.
It just wraps openssl's Blowfish encryption with a
bit of housekeeping in IMHO nicer function calls.
Link against libssl.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <openssl/blowfish.h>

/******************************************************************
* ARGS:
* keydata == ascii text, the encryption passphrase
* keydatalen == how long keydata is
* in == the data to be encrypted
* out == the encrypted data.
* length(in) == length(out), apparently
* inlen == length of the in array
******************************************************************/
void bfencrypt(unsigned char *keydata, int keydatalen, char *in, char
*out, unsigned int inlen) {
BF_KEY key;
unsigned char ivec[32];
int num=0;
// set up for encryption
BF_set_key(&key, keydatalen, keydata);
memset(ivec, '\0', 32);
BF_cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_ENCRYPT);
}

void bfdecrypt(unsigned char *keydata, int keydatalen, char *in, char
*out, unsigned int inlen) {
BF_KEY key;
unsigned char ivec[32];
int num=0;
// set up for decryption
BF_set_key(&key, keydatalen, keydata);
memset(ivec, '\0', 32);
BF_cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_DECRYPT);
}





Spikinsson said:
I'm looking for a good decrypt/encrypt function, all I want is a function in this form:

char* encrypt(char* normal)

< ...snip... >
 
E

Evan

Spikinsson said:
I'm looking for a good decrypt/encrypt function, all I want is a function in this form:

char* encrypt(char* normal)
{
...
return encrypted;
}

and

char* decrypt(char* encrypted)
{
...
return normal;
}

I realize there would be memory allocation and I would have to free() or delete [] after
running the functions.
My problem is I don't know the first thing about any encryption method, so actually I'm
searching for already made functions that are similar to the two mentioned above. I have
really tried to use google to find such functions but I failed, I would very much
appreciate if anybody could post a function, an usefull tutorial or a link to some
source...

What kind of encryption are you looking for? The function could be as
simple as

char* encrypt(const char* plaintext)
{
int len = strlen(plaintext);
char* cyphertext = new char[len+1];

for(int i=0 ; i<len ; ++i)
{
cyphertext = plaintext + 1;
}
cyphertext[len] = 0; // to null-terminate...
return cyphertext;
}

(Of course, this would ideally be done with std::string and such.) To
decrypt simply change the +1 to a -1; you could even pass this in as
an argument and use one function for both encrypting or decrypting.
I'm think that this will wrap around ) right, but I'm not sure. In any
case, if you're just using text, as long as you don't shift it more
than 64 spaces you should be OK.

Here's a slightly more elaborate function (actually two of them, but
ROT13 provides the interface for what you use); it will both encode
and decode, but only letters. (These could be put into a class to keep
them together and the scope nice.)

char rot13letter(char c)
{
if(!isalpha(c)) return c; // non-letters are returned
if(c<='M') return c+13; // A through M maps to N through Z
if(c<='Z') return c-13; // N through Z maps to A through M
if(c<='m') return c+13; // and the same thing for lowercase
if(c<='z') return c-13; // ""
throw SomthingWentHorriblyAndTerriblyWrongException;
}

char* ROT13(const char* plaintext)
{
int len = strlen(plaintext);
char* cyphertext = new char[len+1];

for(int i=0 ; i<len ; ++i)
{
cyphertext = rot13letter(plaintext);
}
cyphertext[len] = 0; // to null-terminate...
return cyphertext;
}


Note also that I'm guessing these would conventionally be implemented
as void functions that operate directly on plaintext rather than
passing out a whole other string. This has severl advantages:
*Often you won't need access to plaintext after you encrypt it, so
reusing that space is preferred (so you don't waste either the space
it would occupy or the allocation time)
*It gives your client function a better idea of what memory is in use,
which makes it less likely that you'll forget to delete[] the return.
*It takes away no flexibility, as the client function can always
duplicate the plaintext string itself before passing it in.
 
M

Marduk

Evan said:
"Spikinsson" <[email protected]> wrote in message
I'm looking for a good decrypt/encrypt function, all I want is a function in this form:

char* encrypt(char* normal)
{
...
return encrypted;
}

and

char* decrypt(char* encrypted)
{
...
return normal;
}

I realize there would be memory allocation and I would have to free() or delete [] after
running the functions.
My problem is I don't know the first thing about any encryption method, so actually I'm
searching for already made functions that are similar to the two mentioned above. I have
really tried to use google to find such functions but I failed, I would very much
appreciate if anybody could post a function, an usefull tutorial or a link to some
source...

What kind of encryption are you looking for? The function could be as
simple as

char* encrypt(const char* plaintext)
{
int len = strlen(plaintext);
char* cyphertext = new char[len+1];

for(int i=0 ; i<len ; ++i)
{
cyphertext = plaintext + 1;
}
cyphertext[len] = 0; // to null-terminate...
return cyphertext;
}

(Of course, this would ideally be done with std::string and such.) To
decrypt simply change the +1 to a -1; you could even pass this in as
an argument and use one function for both encrypting or decrypting.
I'm think that this will wrap around ) right, but I'm not sure. In any
case, if you're just using text, as long as you don't shift it more
than 64 spaces you should be OK.


Substitution ciphers are evil. Never use them. (26 possible keys, 256 with
ASCII...that's breakabale in about 0,01s on a 386)


Here's a slightly more elaborate function (actually two of them, but
ROT13 provides the interface for what you use); it will both encode
and decode, but only letters. (These could be put into a class to keep
them together and the scope nice.)

char rot13letter(char c)
{
if(!isalpha(c)) return c; // non-letters are returned
if(c<='M') return c+13; // A through M maps to N through Z
if(c<='Z') return c-13; // N through Z maps to A through M
if(c<='m') return c+13; // and the same thing for lowercase
if(c<='z') return c-13; // ""
throw SomthingWentHorriblyAndTerriblyWrongException;
}

char* ROT13(const char* plaintext)
{
int len = strlen(plaintext);
char* cyphertext = new char[len+1];

for(int i=0 ; i<len ; ++i)
{
cyphertext = rot13letter(plaintext);
}
cyphertext[len] = 0; // to null-terminate...
return cyphertext;
}


Note also that I'm guessing these would conventionally be implemented
as void functions that operate directly on plaintext rather than
passing out a whole other string. This has severl advantages:
*Often you won't need access to plaintext after you encrypt it, so
reusing that space is preferred (so you don't waste either the space
it would occupy or the allocation time)
*It gives your client function a better idea of what memory is in use,
which makes it less likely that you'll forget to delete[] the return.
*It takes away no flexibility, as the client function can always
duplicate the plaintext string itself before passing it in.


Check out introduction to cryptography by Miller. It's not very complicated,
yet expect it to be complicated if you want to implement encryption
algoritms. My personal suggestion would be blowfish or DES, as they are
very easy to implement from scratch. You can always use cryptolibs, but if
security is an important issue, you are bound to get into gory maths.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top