Image
 
linkedin_logo.png rss_logo.jpg
twitter_logo.png youtube_logo.jpg
Latest Additions
 
EH-Net Login
Welcome Guest.






Lost Password?
No account yet? Register
Who's Online
We have 57 guests and 3 members online
EH-Net News Feeds
Latest Additions
 
Advertisement

You are here: Home arrow Forum arrow Ethical Hacking Discussions and Related Certificationsarrow Programmingarrow Shift Cipher in C
EH-Net
May 25, 2012, 11:39:16 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Advertise on EH-Net!! - Reasonable Rates, Highly Targeted Audience.
 
   Home   Help Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Shift Cipher in C  (Read 4057 times)
0 Members and 1 Guest are viewing this topic.
shednik
Jr. Member
**
Offline Offline

Posts: 75


View Profile
« on: September 14, 2009, 11:51:26 AM »

Hello all,

I have a fairly simple program below that is to utilize the shift cipher to encrypt an entered string.  I have it working if I used a number when entering a value for k, but the requirement for the assignment is to use a Letter to denote 1-26 for the value of k.  I can't seem to figure out what is wrong with my converChar function, if anyone has any thoughts please let me know..,I'm not a great programmer so I may have missed something small.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define  MAXCHARS  255
#define  MAXS    4

int convertChar(char *a);
void encrypt();
void decrypt();

int main()
{
    char response[MAXS],plaintext[MAXCHARS];
   
    printf("Shift Cipher Program\n\nWould you like to (E)ncrypt or (D)ecrypt a message or (Q)uit.  ");
    scanf("%s",response);
   
    while (response[0] != 'q' && response[0] != 'Q') {
          switch (response[0]) {
                 case 'e':
                 case 'E':
                      encrypt();
                      break;
                 case 'd':
                 case 'D':
                      decrypt();
                      break;
                 default:
                         printf("error: invalid command\n");
                         break;
          }//end of switch
          printf("\nWould you like to (E)ncrypt or (D)ecrypt a message or enter Q to quit.  ");
          scanf("%s",response);
    }//end of while
    return 0;
}//end of main


void encrypt()
{
     int i=0,k;
     char msg[MAXCHARS],*letter;

     printf("Please enter the plain text to encrypt in all CAPS and press enter\n");
     scanf ("%s",msg);
     printf("Please enter the alpha key(k) you would like to use  ");
     scanf ("%s",letter);
     k = convertChar(letter);
     while (msg[i]!= '\0') {
           if ((msg[i]+k) > 'Z')
           {
              msg[i] = ((((msg[i]+k) - 'Z')-1) + 'A'); //wrap to the beginning
              }
           else
               msg[i] = (msg[i]+k);
               
           i++;
     }
     printf("\n%s", msg);
     }

void decrypt()
{
     int i=0,k;
     char msg[MAXCHARS],*letter;

     printf("Please enter the cipher text to decrypt in all CAPS and press enter\n");
     scanf ("%s",msg);
     printf("Please enter the alpha key(k) you would like to use  ");
 
     scanf ("%s",letter);
     k = convertChar(letter);
     
      while (msg[i]!= '\0') {
          if ((msg[i]-k) < 'A'){
           
              msg[i] = ('Z' - (('A' - (msg[i]-k))-1)); } //wrap to the end.
           else {
           
               msg[i] = (msg[i]-k); }   
          i++;
     }
     printf ("%s\n",msg);
}                       

int convertChar(char *a)
{
    static char *letters[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    int i=0;
   
    while(i<26){
if (strcmp(letters[i],a)){
            printf("%d",i);
return (i+1); }
else
i++;
}
return 0;
}


Logged

CCNA, MCP, A+, N+

WIP: Masters of Infosec, CEH, & Mastering C
mambru
Jr. Member
**
Offline Offline

Posts: 98


View Profile
« Reply #1 on: September 15, 2009, 08:11:36 PM »

There's an error in your logic in the if statement of your convertChar() function. You should check the documentation for the strcmp() function and understand its return values, it has 3 different: greater than zero, less than zero, and zero. Also remember how the the if statement is decided, one you understand all these you'll find your mistake.

Other function that you may use instead of strcmp() and a sequential search, is strchr().
Logged
gnix
Newbie
*
Offline Offline

Posts: 7


View Profile WWW
« Reply #2 on: September 16, 2009, 09:29:58 PM »

Be careful when you use the scanf function to read a string and you don't specify the length of a string. Often, a format with %s is vulnerable to a buffer overflow attack.

gnix
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines
Joomla Bridge by JoomlaHacks.com
Valid XHTML 1.0! Valid CSS!
Page created in 0.275 seconds with 22 queries.
 

gk_static-ad_feb2012.jpg
Global Knowledge: Build Security Skills to Protect & Defend

els_130x200fixed2.gif
eLearnSecurity Student Course Now Live!
5% Off with Code
ELS-EH-5

SANS Deals 4 EH-Netters
$150 OFF Any SANS Course in Any Format!
Coupon Code: EHN_Connect Including SANS Security West 2012 & SANSFIRE 2012
Recent Forum Topics

cbtnuggets_logo_125.jpg
Try CBT Nuggets Free!

Vote For EH-Net

Add to Technorati Favorites
technorati fave

 
         
Advertisement

© 2012 The Ethical Hacker Network
Joomla! is Free Software released under the GNU/GPL License.