11/22/2011

Crypto-tools: Input Validation

Last time we wrote some functions which will work only if the input string is valid - all capital letters and no other characters allowed. That's because in cryptography, in order to make some (mostly classic) ciphers work, you often need to encrypt just pure information, without extra spaces, commas etc. The functions for encryption and decryption can be pretty complicated, and we shouldn't have to write additional lines inside them just to make sure the input is valid. Instead, we will write a simple function which will convert our input to our-functions-friendly.

Our new function will take two arguments: char *input and char *output. We will parse input string, take every letter, convert it to uppercase and add it to the output string. At the end, output will be our-functions-friendly string. I'll call this function alpha_clean, and it will be defined in our header, cryptotools.h.

Here is how it looks like:
void alpha_clean(char *input, char *output) {
  while (*input) {
    if (*input>='A' && *input<='Z') {
      *output=*input;
      ++output;
    }
    else
      if (*input>='a' && *input<='z') {
        *output=*input-'a'+'A';
        ++output;
      }
    ++input;
  }
  *output='\0';
}
Now, look at it. We use our pointers as iterators again (which is common practice when you work with strings). While the input string is not over, we check the current character from the input. If it's inside [A-Z], then it's already an uppercase letter, so we will just add it to the output string, and increase output pointer (making it ready for the next character). Otherwise, we should check if the current input character is a lowercase letter; if it is, we will convert it to uppercase and add it to the end of our output string. If it's not, we do not change the output. After the current input character is checked, we increase our input pointer.

In the end, we make sure to finish the output string, adding ASCII zero character.

That's it. Make sure that you have enough memory reserved for the output string. The easiest way is to define input and output as arrays of the same length.

If you have any questions, just comment. Keep on coding!

No comments:

Post a Comment

If you have anything useful to say (ideas, suggestions, questions, business proposals, or just "Thank you!"), please feel free to comment! Also, you can use my e-mail and/or find me on Twitter!