I'm trying to create a C program on OTP (One Time Pad) encryption. The program takes a string to be encrypted from the user. Then it has to ask for the length of the key before taking the key (in integer). Since I want the program to be 100% correct, I'm putting a constraint on the length of key. As we know, in OTP the length of the key has to be in integers can't exceed the length of the text. So, how can we implement such a filter? I've tried creating the code but it is not working.
Here's the code:
//An Under-Construction program to implement OTP (One time Pad) encryption
#include "stdio.h"
#include "cstring"
#include "ctype.h"
int main()
{
char str[10000], key[10000];
printf("Enter the string:- ");
scanf("%[^\n]", str); //stops scanning when user presses return
int len /* stores the number of digits of the OTP key*/, isalphabet=0;
do
{
printf("What is the length of the key you are entering?:-");
scanf("%d", &len);
if( isalpha(len) ) // Checks if len is a character
{
isalphabet=NULL; //isalphabet becomes NULL if it is a character and not an integer
}
} while (len > strlen(str) || isalphabet == NULL); //reiterate the loop until the conditions are satisfied
for(int i = 0; i < len; i++)
{
printf("%dth digit of Key= ", i+1);
scanf("%d", &key[i]);
}
return(0);
}
I want the program to take only integer value from the user while scanning 'len' and also the value of 'len' shouldn't exceed the length of the string to be encrypted, and reiterate the loop if these conditions aren't satisfied. Can someone point out my mistake and show the the solution? Also, please explain me the reason as to why isn't the code working as it should.