Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- int checkParams(const char* key, const char* text)
- {
- if (!(text && key)) { return 0; }
- while (*key)
- {
- if (!isalpha(*key++)) { return 0; }
- }
- return 1;
- }
- char* vigenere_encrypt(const char* key, const char* text)
- {
- char* result = NULL;
- // check if both text and key not NULL pointers
- if (checkParams(key, text))
- {
- // this constant holds alphabet length, 26 for English
- const int alphaLength = 26;
- // allocate strlen(text) + 1 for '\0' and to be shure multiple to sizeof char :)
- result = calloc(sizeof(char), strlen(text) + 1);
- // copy pointer to allocated memory block to work with and modify
- char* encrypt = result;
- // get length of the key for "key-cycling"
- size_t keyLength = strlen(key);
- // position of next to use for encoding symbol from key
- size_t keyPos = 0;
- while (*text)
- {
- // check if next symbol is letter, if so - encode, if no - just copy as is
- if (isalpha(*text))
- {
- // encrypt every symbol from original string by properly selected symbol from key
- *encrypt++ = ('A' + (toupper(*text++) + toupper(*(key + keyPos))) % alphaLength);
- // increase key symbol position
- ++keyPos;
- // cycle key symbol position
- keyPos %= keyLength;
- }
- else
- {
- // just copy as is if not a letter
- *encrypt++ = *text++;
- }
- }
- // properly terminate encoded string with '\0'
- *encrypt = '\0';
- }
- // return pointer to originally allocated memory block for encoded string or NULL
- return result;
- }
- char* vigenere_decrypt(const char* key, const char* text)
- {
- char* result = NULL;
- // check if both text and key not NULL pointers
- if (checkParams(key, text))
- {
- // this constant holds alphabet length, 26 for English
- const int alphaLength = 26;
- // allocate strlen(text) + 1 for '\0' and to be shure multiple to sizeof char :)
- result = calloc(sizeof(char), strlen(text) + 1);
- // copy pointer to allocated memory block to work with and modify
- char* decrypt = result;
- // get length of the key for "key-cycling"
- size_t keyLength = strlen(key);
- // position of next to use for decoding symbol from key
- size_t keyPos = 0;
- while (*text)
- {
- // check if next symbol is letter, if so - decode, if no - just copy as is
- if (isalpha(*text))
- {
- // decrypt every symbol from original string by properly selected symbol from key
- *decrypt++ = ('A' + (toupper(*text++) + alphaLength - toupper(*(key + keyPos))) % alphaLength);
- // increase key symbol position
- ++keyPos;
- // cycle key symbol position
- keyPos %= keyLength;
- }
- else
- {
- // just copy as is if not a letter
- *decrypt++ = *text++;
- }
- }
- // properly terminate decoded string with '\0'
- *decrypt = '\0';
- }
- // return pointer to originally allocated memory block for decoded string or NULL
- return result;
- }
- // all-in-one version as a bonus ;) no comments :) mode = 1 -> encode, mode = 0 -> decode
- char* vigenere(const char* key, const char* text, int mode)
- {
- char* result = NULL;
- if (checkParams(key, text))
- {
- const int alphaLength = 26;
- result = calloc(sizeof(char), strlen(text) + 1);
- char* resText = result;
- size_t keyLength = strlen(key);
- size_t keyPos = 0;
- while (*text)
- {
- if (isalpha(*text))
- {
- *resText++ = ('A' + (toupper(*text++) + (mode ? toupper(*(key + keyPos++)) : alphaLength - toupper(*(key + keyPos++)))) % alphaLength);
- keyPos %= keyLength;
- }
- else
- {
- *resText++ = *text++;
- }
- }
- *resText = '\0';
- }
- return result;
- }
- int main()
- {
- char str[] = { "ATTACKATDAWN" };
- char key[] = { "LEMON" };
- char* encoded = NULL;
- char* decoded = NULL;
- printf("Test with separate functions for encode and decode\n");
- printf("Original:\t%s\n", str);
- encoded = vigenere_encrypt(key, str);
- printf("Encoded:\t%s\n", encoded);
- decoded = vigenere_decrypt(key, encoded);
- printf("Decoded:\t%s\n", decoded);
- free(decoded);
- free(encoded);
- printf("\nTest with one function both for encode and decode\n");
- printf("Original:\t%s\n", str);
- encoded = vigenere(key, str, 1);
- printf("Encoded:\t%s\n", encoded);
- decoded = vigenere(key, encoded, 0);
- printf("Decoded:\t%s\n", decoded);
- free(decoded);
- free(encoded);
- printf("\nTest for invalid key and message\n");
- printf("Original:\t%s\n", NULL);
- encoded = vigenere_encrypt(NULL, NULL);
- printf("Encoded:\t%s\n", encoded);
- decoded = vigenere_decrypt(NULL, NULL);
- printf("Decoded:\t%s\n", decoded);
- free(decoded);
- free(encoded);
- printf("\nTUKE test:\n");
- printf("Original:\t%s\n", "Hello world!");
- encoded = vigenere_encrypt("CoMPuTeR", "Hello world!");
- printf("Encoded:\t%s\n", encoded);
- decoded = vigenere_decrypt("CoMPuTeR", encoded);
- printf("Decoded:\t%s\n", decoded);
- free(decoded);
- free(encoded);
- printf("Test passed: %d\n", vigenere_encrypt("Ach poveda kdeze si mi tak dlho ako tazko za tebou cakam", "Bezali vsetci do mastale tu kravicka stala pekne na svojom mieste a vlci cusali v kute na pyskoch mali velke zamky") == NULL);
- printf("Test passed: %d\n", vigenere_decrypt("Zase sa len po chvili ohlasi", "Kralova dcera zisla z koca a sadla na kona") == NULL);
- printf("Test passed: %d\n", vigenere_encrypt(" !\"#$ % &'()*+", "sultanate") == NULL);
- printf("Test passed: %d\n", vigenere_encrypt(" !\"#$ % &'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|", "canniness") == NULL);
- printf("Test passed: %d\n", vigenere_decrypt(" !\"#$ % &'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|", "lamented") == NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement