OTP (One Time Pad) 暗号化で C プログラムを作成しようとしています。プログラムは、暗号化する文字列をユーザーから受け取ります。次に、キーを取得する前にキーの長さを要求する必要があります (整数)。プログラムを 100% 正しくしたいので、キーの長さに制約を課しています。ご存知のように、OTP ではキーの長さは整数でなければならず、テキストの長さを超えることはできません。では、そのようなフィルターをどのように実装できますか? コードを作成しようとしましたが、機能しません。
コードは次のとおりです。
//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);
}
「len」のスキャン中にプログラムがユーザーから整数値のみを取得し、「len」の値が暗号化される文字列の長さを超えてはならず、これらの条件が満たされない場合はループを繰り返します。誰かが私の間違いを指摘して解決策を示すことができますか? また、コードが正常に動作しない理由を教えてください。