Encrypt/Decrypt

Discussion in 'C++' started by Spikinsson, Jul 10, 2003.

  1. Spikinsson

    Spikinsson Guest

    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...
     
    Spikinsson, Jul 10, 2003
    #1
    1. Advertising

  2. Spikinsson

    Spikinsson Guest

    "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...
     
    Spikinsson, Jul 10, 2003
    #2
    1. Advertising

  3. Spikinsson

    emerth Guest

    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" <> wrote in message news:<7DiPa.14437$-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... >
     
    emerth, Jul 10, 2003
    #3
  4. Spikinsson

    Evan Guest

    "Spikinsson" <> wrote in message news:<7DiPa.14437$-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 = 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.
     
    Evan, Jul 10, 2003
    #4
  5. Spikinsson

    Marduk Guest

    "Evan" <> wrote in message
    news:...
    > "Spikinsson" <> wrote in message

    news:<7DiPa.14437$-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 = 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.
     
    Marduk, Jul 11, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    497
    Jonathan Allen
    Sep 25, 2004
  2. Mark

    Encrypt/decrypt info from form

    Mark, Feb 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,513
    William LaMartin
    Feb 6, 2004
  3. =?Utf-8?B?TWlrZQ==?=
    Replies:
    0
    Views:
    958
    =?Utf-8?B?TWlrZQ==?=
    Aug 31, 2005
  4. Microsoft

    Password encrypt/decrypt

    Microsoft, Oct 5, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    7,560
    =?Utf-8?B?c3Jpbmk=?=
    Oct 5, 2005
  5. Replies:
    1
    Views:
    434
    Daniel Martin
    Jun 16, 2007
Loading...

Share This Page