-
-
Save tanayseven/8933117 to your computer and use it in GitHub Desktop.
/* | |
The MIT License (MIT) | |
Copyright (c) 2014 Tanay PrabhuDesai | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include <math.h> | |
#define VAL(PTR) (*PTR) | |
typedef char* STRING; | |
typedef char** STRING_REF; | |
void autokey_encrypt_decrypt(STRING_REF str, char key, int encrypt){ | |
int str_length, itr_str, next_key,current_key, remainder; | |
str_length = strlen(VAL(str)); | |
for(itr_str = 0 ; itr_str < str_length ; ++itr_str){ | |
if(isalpha(VAL(str)[itr_str])){ | |
VAL(str)[itr_str] = toupper(VAL(str)[itr_str]); | |
} | |
} | |
next_key = toupper(key)-'A'; | |
for(itr_str = 0 ; itr_str < str_length ; ++itr_str){ | |
if(isalpha(VAL(str)[itr_str])){ | |
current_key = next_key; | |
if(encrypt){ | |
next_key = VAL(str)[itr_str]-'A'; | |
VAL(str)[itr_str] = (VAL(str)[itr_str]-'A'+current_key)%26+'A'; | |
} | |
else{ | |
remainder = (VAL(str)[itr_str]-'A'-current_key)%26+'A'; | |
VAL(str)[itr_str] = remainder < 'A' ? remainder + 26 : remainder ; | |
next_key = VAL(str)[itr_str]-'A'; | |
} | |
} | |
} | |
} | |
void flush_stdin() | |
{ | |
while(fgetc(stdin) != '\n'); | |
} | |
int main(int argc, char **argv) | |
{ | |
int length; | |
char key; | |
char *str; | |
printf("Enter the key to be used for encryption: "); | |
scanf("%c",&key); | |
printf("Enter the length of the string: "); | |
scanf("%d",&length); | |
str = (char*)malloc((length+1)*sizeof(char)); | |
flush_stdin(); | |
printf("Input the string you want to encrypt: "); | |
fgets(str,length+1,stdin); | |
flush_stdin(); | |
autokey_encrypt_decrypt(&str,key,1); | |
printf("\nThe text after encrypting is: %s",str); | |
autokey_encrypt_decrypt(&str,key,0); | |
printf("\nAfter decrypting the same encrypted text: %s",str); | |
printf("\n"); | |
free(str); | |
return 0; | |
} |
I tried running this code on my laptop using codeblocks, however, I'm not getting output, that is, the text after encrypting and after decryption sections are not getting displayed even though the input sections of the code are working
I think you should might have to link the Math library in addition to just running the command. I'm not sure about the solution for this, I wrote this code longback!
Okay thank you for your feedback sir
Sir, I have noticed that the code on works when the Plain Text is one complete word say AMALGAMATION but not for sentences such as LETS HEAD THERE. Seems like the space between words leads to this issue, in your provided example I can see you used the plain text ATTACK IS TODAY and you got output
TBH I, honestly have no time to fix this. Feel free to fix it if you want to.
SAMPLE OUTPUT:
Enter the key to be used for encryption: m
Enter the length of the string: 15
Input the string you want to encrypt: attack is today
The text after encrypting is: MTMTCM SA LHRDY
After decrypting the same encrypted text: ATTACK IS TODAY