Skip to content

Instantly share code, notes, and snippets.

@inactive123
Created October 6, 2011 10:32
Show Gist options
  • Save inactive123/1267087 to your computer and use it in GitHub Desktop.
Save inactive123/1267087 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
//addditive cipher
#define SHIFT 10
#define ALPHABET_COUNT 27
#define DECRYPT_STRING "BYLOBD"
#define ENCRYPT_STRING "ROBERT"
static char m_alphabet[ALPHABET_COUNT][2] = { {'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'}};
static char m_cipher_alphabet[ALPHABET_COUNT][2] = { {'\0'},{ '\0'},{ '\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},{'\0'}};
static void setup_cipher_alphabet()
{
int i = 0;
for(i = 0; i < ALPHABET_COUNT; i++)
{
if(i+SHIFT < ALPHABET_COUNT)
strcpy(m_cipher_alphabet[i], m_alphabet[i+SHIFT]);
else
{
int modulo = (i+SHIFT) % ALPHABET_COUNT;
strcpy(m_cipher_alphabet[i], m_alphabet[modulo]);
}
}
}
static void print_alphabets()
{
printf("PLAIN ALPHABET: \n");
int i = 0;
for(i = 0; i < ALPHABET_COUNT; i++)
printf("%s", m_alphabet[i]);
printf("\n");
printf("\nCIPHER ALPHABET (Shift by: %d)\n", SHIFT);
for(i = 0; i < ALPHABET_COUNT; i++)
printf("%s", m_cipher_alphabet[i]);
printf("\n");
}
static unsigned lookup_alphabet_number(unsigned char * letter)
{
switch(letter)
{
case 'A':
case 'a':
return 0;
case 'B':
case 'b':
return 1;
case 'C':
case 'c':
return 2;
case 'D':
case 'd':
return 3;
case 'E':
case 'e':
return 4;
case 'F':
case 'f':
return 5;
case 'G':
case 'g':
return 6;
case 'H':
case 'h':
return 7;
case 'I':
case 'i':
return 8;
case 'J':
case 'j':
return 9;
case 'K':
case 'k':
return 10;
case 'L':
case 'l':
return 11;
case 'M':
case 'm':
return 12;
case 'N':
case 'n':
return 13;
case 'O':
case 'o':
return 14;
case 'P':
case 'p':
return 15;
case 'Q':
case 'q':
return 16;
case 'R':
case 'r':
return 17;
case 'S':
case 's':
return 18;
case 'T':
case 't':
return 19;
case 'U':
case 'u':
return 20;
case 'V':
case 'v':
return 21;
case 'W':
case 'w':
return 22;
case 'X':
case 'x':
return 23;
case 'Y':
case 'y':
return 24;
case 'Z':
case 'z':
return 25;
}
}
static void encrypt_string(const char * m_text)
{
printf("String (%s) will be encrypted...\n", m_text);
printf("length of string: %d\n", strlen(m_text));
int i;
for(i = 0; i < strlen(m_text); i++)
{
uint32_t num_alpha = lookup_alphabet_number(m_text[i]);
int32_t k = num_alpha + SHIFT;
uint32_t modulo = k % 26;
printf("num_alpha is: %d modulo is: %d (%s)\n", num_alpha, modulo, m_alphabet[modulo]);
}
}
static void decrypt_string(const char * m_text)
{
printf("Encrypted string (%s) will be decrypted...\n", m_text);
int i;
for(i = 0; i < strlen(m_text); i++)
{
uint32_t num_alpha = lookup_alphabet_number(m_text[i]);
int32_t k = num_alpha - SHIFT;
uint32_t modulo = k % 26;
printf("num_alpha is: %d modulo is: %d (%s)\n", num_alpha, modulo, m_alphabet[modulo]);
}
}
static void print_usage()
{
printf("USAGE:\n");
printf("print_alphabet [shiftvalue]: Print alphabet/cipher alphabet by given shift value\n");
printf("encrypt_string [shiftvalue]: Encrypt string by given shift value\n");
}
void main(int argc, char **argv)
{
printf("Additive cipher program\n\n");
if(argc > 1)
{
if(strcmp(argv[1],"print_alphabets") == 0)
{
setup_cipher_alphabet();
print_alphabets();
}
else if(strcmp(argv[1],"decrypt_string") == 0)
{
decrypt_string(DECRYPT_STRING);
setup_cipher_alphabet();
print_alphabets();
}
else if(strcmp(argv[1],"encrypt_string") == 0)
{
encrypt_string(ENCRYPT_STRING);
setup_cipher_alphabet();
print_alphabets();
}
else if(strcmp(argv[1],"char_to_number") == 0)
{
printf("A becomes : %d\n", 'A' - 0x41);
printf("D becomes : %d\n", 'D' - 0x41);
}
else
{
printf("ERROR: Wrong parameter supplied!\n\n");
print_usage();
}
}
else
{
printf("ERROR: You forgot to launch this program with a parameter/argument.\n");
print_usage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment