Caesar Cipher in C

A simple C implementation of the Caesar Cipher. Supports full wrap around for Alpha Numerics. Does Modulus for shifts larger than 26 for Alphas and 10 for Digits.

Background on Caesar Cipher: https://en.wikipedia.org/wiki/Caesar_cipher

/*
 Copyright 2019 Robert C. Ilardi

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

//To Compile: gcc CaesarCipher.c -o CaesarCipher

/*
 Usage: ./CaesarCipher [SHIFT] [MESSAGE]

 A nice generic Caesar Cipher C program I just wrote
 for the hell of it.

 Use negative numbers to reverse the shifting.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef unsigned int bool;

const bool TRUE=1;
const bool FALSE=0;

int main(int argc, char* argv[])
{
	int shift, tmpShift;
	char *mesg;
	char *outputMesg;
	char ch;
	bool isAlpha, isUpper, isLower, isDigit;

	if (argc!=3)
	{
		fprintf(stderr, "Usage: %s [SHIFT] [MESSAGE]\n", argv[0]);
		return EXIT_FAILURE;
	}
	
	shift=atoi(argv[1]);
	mesg=argv[2];

	printf("Shift Amount: %d\n", shift);
	printf("Message: %s\n", mesg);

	outputMesg = (char*)malloc(strlen(mesg)+1);

	for (int i=0; i<strlen(mesg); i++)
	{
		ch=mesg[i];
		isUpper = (ch >= 'A' && ch <='Z');
		isLower = (ch >= 'a' && ch <='z');
		isAlpha = ((ch >= 'A' && ch <='Z') || (ch >= 'a' && ch <='z'));
		isDigit = (ch >= '0' && ch <= '9');

		if (isAlpha)
		{
			if (shift > 0)
			{
				if (shift>26)
				{
					tmpShift=shift % 26;
				}
				else
				{
					tmpShift=shift;
				}

				if (isUpper && (ch + tmpShift) > 'Z')
				{
					ch = 'A' + ((tmpShift - ('Z' - ch)) - 1);
				}
				else if (isLower && (ch + tmpShift) > 'z')
				{
					ch = 'a' + ((tmpShift - ('z' - ch)) - 1);
				}
				else
				{
					ch += tmpShift;
				}
			}
			else if (shift < 0)
			{
				if (abs(shift)>26)
				{
					tmpShift=shift % 26;
				}
				else
				{
					tmpShift=shift;
				}

				if (isUpper && (ch + tmpShift) < 'A')
				{
					ch = 'Z' - (('A' - ch) + abs(tmpShift) - 1);
				}
				else if (isLower && (ch + tmpShift) < 'a')
				{
					ch = 'z' - (('a' - ch) + abs(tmpShift) - 1);
				}
				else
				{
					 ch += tmpShift;
				}
			}
		}
		else if (isDigit)
		{
			if (shift>0)
			{
				if (shift>10)
				{
					tmpShift=shift % 10;
				}
				else
				{
					tmpShift=shift;
				}

				if ((ch + tmpShift) > '9')
				{
					ch = '0' + ((tmpShift - ('9' - ch)) - 1);
				}
				else
				{
					ch += tmpShift;
				}
			}
			else if (shift<0)
			{
				if (abs(shift)>10)
				{
					tmpShift=shift % 10;
				}
				else
				{
					tmpShift=shift;
				}

				if ((ch + tmpShift) < '0')
				{
					ch = '9' - (('0' - ch) + abs(tmpShift) - 1);
				}
				else
				{
					ch += tmpShift;
				}
			}
		}

		outputMesg[i]=ch;
	}

	outputMesg[strlen(outputMesg)]='\0';

	printf("Output Message: %s\n", outputMesg);

	free(outputMesg);

	return EXIT_SUCCESS;
}

This entry was posted in Computer Fun Stuff, Development, Programming General, Security, Technology. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.