Thread: C encryption/decryption auto cipher key

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    1

    C encryption/decryption auto cipher key

    Hi all,

    I am working on a encryption/decryption program that uses the auto key ciper method of encryption. Here is my encrypt func:

    Code:
    unsigned int encrypt(char x){
      char a;
        if ( i == 0 ){
        a = 'K';}
        else if( i == 1 ){
        a = 'E';}
        else if( i == 2 ){
        a = 'Y';}
        else if( i > 2 ){
            a = x;}
        i++;
      return x + a;
    }
    Here is my decrypt:

    Code:
    unsigned int decrypt(char x){
      char a;
        if( i == 0 ){
        a = 'K';}
        else if( i == 1 ){
        a = 'E';}
        else if( i == 2 ){
        a = 'Y';}
        else if( i > 2 ){
            a = (x/2);}
      i++;
      if (c != EOF && c != ('\n')){
      return x - a;}
      else {return 0;}
    }
    When I pipe a message through encrypt then through decrypt I should get the original message but I only get the first 3 characters (because of the first three K E Y keywords) and any numbers in the message. Any other characters don't come out correctly. What am I missing? Thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by arriver5 View Post
    Hi all,

    I am working on a encryption/decryption program that uses the auto key ciper method of encryption. Here is my encrypt func:


    When I pipe a message through encrypt then through decrypt I should get the original message but I only get the first 3 characters (because of the first three K E Y keywords) and any numbers in the message. Any other characters don't come out correctly. What am I missing? Thanks in advance.
    The code you posted is missing where you declared "i".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You are doubling a value then halving it again and not getting the original value back. This is a sign of arithmetic overflow. char is a signed on your system. The (common) value for 'A' is 65. Doubling that gives 130, which is beyond 7-bits, i.e., the "sign" bit is now set, so it's interpreted as a negative number, which in the (common) 2's complement system for representing negative values is 130 - 256 = -126. When this is halved, you get -63 (or 256 - 63 = 193 unsigned).

    BTW, this is not an autokey cipher. You're supposed to add in the letter that appears n chars back in the message, where n is the length of the primary key ("KEY"). And if you want the ciphertext to be printable you'll have to limit it to printable characters (maybe ' ' to '~');

    And you should try to write it without the global variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption and Decryption
    By dbz8gokugohan in forum C Programming
    Replies: 7
    Last Post: 12-02-2010, 07:40 AM
  2. encryption / decryption
    By ghatoz in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2010, 06:45 AM
  3. Help Decryption - Shift Cipher
    By Sn1per in forum C Programming
    Replies: 8
    Last Post: 11-06-2009, 11:10 AM
  4. Vigenere Cipher decryption help needed
    By magic101 in forum C Programming
    Replies: 2
    Last Post: 02-07-2009, 07:25 PM
  5. Encryption/Decryption Help
    By coolage in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2008, 01:53 AM

Tags for this Thread