Encrypt/Decrypt

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Spikinsson

    #1

    Encrypt/Decrypt

    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

    #2
    Re: Encrypt/Decrypt

    "Victor Bazarov"[color=blue]
    > Have you tried comp.sources.wa nted?[/color]
    No, but will do...
    [color=blue]
    > 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.[/color]
    I need ine that doesn't require MFC, string classes or additional DLL's and that can
    encrypt/decrypt strings, not files...

    [color=blue]
    > P.S. And, just to clarify, we don't do homeworks.[/color]
    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...


    Comment

    • emerth

      #3
      Re: Encrypt/Decrypt

      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(unsig ned 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_encryp t(in, out, inlen, &key, ivec, &num, BF_ENCRYPT);
      }

      void bfdecrypt(unsig ned 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_encryp t(in, out, inlen, &key, ivec, &num, BF_DECRYPT);
      }





      "Spikinsson " <not@gonna.tell .ya> wrote in message news:<7DiPa.144 37$7h.14154@afr odite.telenet-ops.be>...[color=blue]
      > I'm looking for a good decrypt/encrypt function, all I want is a function in this form:
      >
      > char* encrypt(char* normal)[/color]

      < ...snip... >

      Comment

      • Evan

        #4
        Re: Encrypt/Decrypt

        "Spikinsson " <not@gonna.tell .ya> wrote in message news:<7DiPa.144 37$7h.14154@afr odite.telenet-ops.be>...[color=blue]
        > 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...[/color]

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

        char* encrypt(const char* plaintext)
        {
        int len = strlen(plaintex t);
        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(cha r 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 SomthingWentHor riblyAndTerribl yWrongException ;
        }

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

        for(int i=0 ; i<len ; ++i)
        {
        cyphertext[i] = rot13letter(pla intext[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.

        Comment

        • Marduk

          #5
          Re: Encrypt/Decrypt


          "Evan" <eed132@psu.edu > wrote in message
          news:3f25c666.0 307101358.65865 001@posting.goo gle.com...[color=blue]
          > "Spikinsson " <not@gonna.tell .ya> wrote in message[/color]
          news:<7DiPa.144 37$7h.14154@afr odite.telenet-ops.be>...[color=blue][color=green]
          > > I'm looking for a good decrypt/encrypt function, all I want is a[/color][/color]
          function in this form:[color=blue][color=green]
          > >
          > > 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[/color][/color]
          delete [] after[color=blue][color=green]
          > > running the functions.
          > > My problem is I don't know the first thing about any encryption method,[/color][/color]
          so actually I'm[color=blue][color=green]
          > > searching for already made functions that are similar to the two[/color][/color]
          mentioned above. I have[color=blue][color=green]
          > > really tried to use google to find such functions but I failed, I would[/color][/color]
          very much[color=blue][color=green]
          > > appreciate if anybody could post a function, an usefull tutorial or a[/color][/color]
          link to some[color=blue][color=green]
          > > source...[/color]
          >
          > What kind of encryption are you looking for? The function could be as
          > simple as
          >
          > char* encrypt(const char* plaintext)
          > {
          > int len = strlen(plaintex t);
          > 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.
          >[/color]

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


          [color=blue]
          > 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(cha r 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 SomthingWentHor riblyAndTerribl yWrongException ;
          > }
          >
          > char* ROT13(const char* plaintext)
          > {
          > int len = strlen(plaintex t);
          > char* cyphertext = new char[len+1];
          >
          > for(int i=0 ; i<len ; ++i)
          > {
          > cyphertext[i] = rot13letter(pla intext[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.[/color]

          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.



          Comment

          Working...