Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Encrypt/Decrypt

Reply
Thread Tools

Encrypt/Decrypt

 
 
Spikinsson
Guest
Posts: n/a
 
      07-10-2003
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...


 
Reply With Quote
 
 
 
 
Spikinsson
Guest
Posts: n/a
 
      07-10-2003
"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...


 
Reply With Quote
 
 
 
 
emerth
Guest
Posts: n/a
 
      07-10-2003
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" <(E-Mail Removed)> wrote in message news:<7DiPa.14437$(E-Mail Removed)-ops.be>...
> I'm looking for a good decrypt/encrypt function, all I want is a function in this form:
>
> char* encrypt(char* normal)


< ...snip... >
 
Reply With Quote
 
Evan
Guest
Posts: n/a
 
      07-10-2003
"Spikinsson" <(E-Mail Removed)> wrote in message news:<7DiPa.14437$(E-Mail Removed)-ops.be>...
> 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[i] = plaintext[i] + 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[i] = rot13letter(plaintext[i]);
}
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.
 
Reply With Quote
 
Marduk
Guest
Posts: n/a
 
      07-11-2003

"Evan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Spikinsson" <(E-Mail Removed)> wrote in message

news:<7DiPa.14437$(E-Mail Removed)-ops.be>...
> > 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[i] = plaintext[i] + 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[i] = rot13letter(plaintext[i]);
> }
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments