Rot13 Encoding/Decoding : Part 1 of a Series

Written by:typedeaF
Published by:Nightscript
Published on:2004-07-16 15:33:25
Topic:c

Rot13 Encoder/Decoder (AKA shift cipher) written in C.
The first example in a mini-series on basic cryptology in C.

#include <stdio.h>
#include <ctype.h>

main(int argc, char *argv[])
{
    FILE *input = stdin;
    char *prog = argv[0];
    int ch;

    printf("ttRot13 Example Program by typedeaF @ www.osix.netn");

    if(argc == 2)
        if((input = fopen(*++argv, "r")) == NULL)
        {
        fprintf(stderr, "ERROR: %s can not open file %s.n", prog, *argv);
            return 1;
        }

        while((ch = fgetc(input)) != EOF)
        {
            if(isalpha(ch))
            {
                if(toupper(ch) < ('A' + 13))
                    putchar(ch+13);
                else
                    putchar(ch-13);
            }
            else
               putchar(ch);
        }
        fclose(input);

        return 0;
}


What we have here is a simple common method for encoding plain text ASCII character data. Rot13 simply evaluates the byte of data. If it's alphabetic -- falling between 'A-Z' or 'a-z', then we encode it, if not we simply leave it alone. This means characters like spaces and digits are not encoded at all. Once it is determined that we have an alphabetic character -- by using the ctype function isalpha(), we check to see which half of the alphabet the character is in.

With Rot13, for letters that are in the first half (first 13 letters, A-M) we shift that character forward 13 places. Thus 'A' becomes 'N'. In C this is very simple because the ASCII code for the alphabet is linear. Thus if 'A' has a value of 65 then 'A' + 3, or 65 + 3, will be the ASCII value for 'D', 68. This makes the math very easy. To shift any letter forward 13 we simply add 13 to its ASCII value, and for the second half of the alphabet we simply subtract 13 from the ASCII value, or shift backwards 13. The toupper() function is necessary to ensure that we can check for the range of the character in one sweep. Basically since 'a' and 'A' have two separate ASCII values, we are converting the character to upper case then checking if it is above or below 'N'. There are many many ways to do this routine and I feel that since we already included ctype.h we might as well get some use out of it.

The first half of the program and the functions I will not explain. This article assumes you understand file handling and basic C syntax. If not then try to grasp the meaning from the example or go check out an article on C by OSI or Academy.

PS.
post some comments on how YOU would do the conversion and comparisons.

This is an article from http://www.osix.net - view the original at: http://www.osix.net/modules/article/?id=557