help wiv encryption

This is a discussion on help wiv encryption within the C Programming forums, part of the General Programming Boards category; here is what i av written Code: /*program encrypts messages*/ #include <stdio.h> #include <ctype.h> main() { char str[255]; int i; ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    help wiv encryption

    here is what i av written

    Code:
    /*program encrypts messages*/
    #include <stdio.h>
    #include <ctype.h>
    main()
    {
    char str[255];
    int i;
    int delt = 'a' - '9';
    printf("Enter a string less than 80 characters:\n");
    gets( str );
    i = 0;
    while (str[i]){
    if ((str[i] >= 'a') && (str[i] <= 'z'))
    str[i] -= delt; 
    ++i;
    }
    printf("The entered string is (encrypted simply):\n");
    puts( str );
    return 0;
    }
    i need it perfected and mayb fool proof and i need tips on decrypting it also thanks
    Last edited by princeyeni; 06-19-2009 at 08:14 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Read this
    << !! Posting Code? Read this First !! >>
    Edit your post to use code tags.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    using gets() is not recommended at that can lead to overflows! Also what do you think the value of delt is doing here to your string elements? Do you know what the value of delt is? Also are you only trying to encrypt lower case letters?

    Just some things to reflect on. Hopefully that helps you.

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Wiv? Av? It sounds like you know more about encryption than you're letting on!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    'a' to 'z' aren't guaranteed to be contiguous. Which means that your encryption basically won't work if the user is using a different encoding scheme (rare, I'll grant you, but not impossible).

    Here's a simple encryption program I whipped up just for the fun of it. Use isalpha() instead of isprint() for less radical encryption.
    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <ctype.h>
    
    int is_encryptable(int c);
    void crypt_line(const char *line, const char *map, int map_entries);
    
    int main() {
        char map[UCHAR_MAX];
        int x, y, map_entries = 0;
        char line[BUFSIZ];
    
        for(x = 0; x < UCHAR_MAX; x ++) {
            if(is_encryptable(x)) {
                map[map_entries ++] = x;
            }
        }
    
        for(;;) {
            printf("crypt> ");
            if(!fgets(line, sizeof line, stdin)) break;
    
            printf("   ==> ");
            crypt_line(line, map, map_entries);
        }
    
        putchar('\n');
    
        return 0;
    }
    
    int is_encryptable(int c) {
        return isprint(c) && c != '\n';
    }
    
    void crypt_line(const char *line, const char *map, int map_entries) {
        int x, y;
    
        for(x = 0; line[x]; x ++) {
            if(is_encryptable(line[x])) {
                /* linear search, ugh */
                for(y = 0; map[y]; y ++) {
                    if(line[x] == map[y]) {
                        putchar(map[map_entries - y - 1]);
                    }
                }
            }
            else {
                putchar(line[x]);
            }
        }
    }
    It simply collects all printable characters into one map. Every time a character is encrypted, it "reverses" the position in the map table and uses that instead. (By reverse I mean length - x - 1.)
    Code:
    $ ./encrypt
    crypt> Greetings.
       ==> W,99*507+p
    crypt> W,99*507+p
       ==> Greetings.
    crypt>
    $
    Presumably you're aware that this sort of encryption isn't really that secure.

    Anyway, for more interesting encyption, you can use the bitwise NOT operator (~). But that's even easier to guess and the result frequently isn't printable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Nice use of your map[]. Yeah I see you are using a sequential search, for something like this perhaps a binary search would be in order. But hardly will notice any inpact on an array this small, but if you had to do 1000s of these then parhaps a little bit of a boost here. Since your map[] is sorted, this should work.


    Code:
    binary_search(map[], int x, int left, int right, int map_entries)
    {
      while (right >= left)
        {
         int m = (left + right) /2;
         if (x  == map[m]) 
            putchar(map[map_entries - m - 1];
         /*move up the array */  
         if ( x < map[m]) right = m - 1;
         /*move down the array */
         else left = m + 1;
        }
       return -1;
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, indeed. I was just trying to come up with a simplish encryption method that was sort of similar to the OP's encryption method.

    Also, BTW, this is C so Boost wouldn't work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. help needed with edit control & encryption
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2006, 07:21 PM
  3. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 09:16 PM
  4. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 09:51 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21