-2

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.

Kirk
  • 16,182
  • 20
  • 80
  • 112
Arlene Batada
  • 1,565
  • 2
  • 11
  • 11
  • Surely you mean the key has to be no *shorter* than the text? Otherwise you'd have to repeat the key, which makes the encryption crackable. – Nicholas Wilson Apr 13 '13 at 11:46
  • @Arlene Please explain the specific problem you are having. –  Apr 13 '13 at 11:55
  • @NicholasWilson:- Ideally, to make the cipher 100% uncrackable, the key should be as long as the text. Since this is not possible in all the cases, the key should be as long as possible but in NO CASE can exceed the length of the text to be encrypted. – Arlene Batada Apr 13 '13 at 15:12
  • @Armin : Compile and run the code and you'll understand what I mean. When it asks you "What is the length of the key you are entering?:-", input any character (eg:- 'a'). You'll find the program going insane. This is the reason I'm trying to create a filter and I need help. – Arlene Batada Apr 13 '13 at 15:12

2 Answers2

0

check scanf("%d", len); is there somewhere a & missing? also initialize len

Update:- sorry for being late, okk, actually there's few things you need to correct, sorry i didn't point them in first post.

1) correct the header inclusion delimiter. Use <stdio.h> instead of "stdio.h"

2) initialize len = 0

3) flush stdin after you have read. If you really enter a char, the scanf(%d,&len) is not going to clear it/read it. So you will be stuck in an infinite loop as because of a char stucking in stdin. same goes for the last for loop. Fortunately here you will not be stuck, but the loop will finish prematurely.

4) isalphabet=NULL, isalphabet is int, NULL basically a void pointer with 0x0 value, you should assign 0 to an int.

5) So you came inside the loop with isalphabet=0, the number is not a alphabet, you did not touch the isalphabet, and end of while loop, isalphabet==NULL, here perhaps it will run as true and again the while loop starts. So when is the loop breaking condition getting set to isalphabet?

6) Also why use the cstring in c code? why not string.h (but this is more of a personal choice :))

7) correcting the len > strlen( to len >= strlen

I edited a little bit, check if it works

//An Under-Construction program to implement OTP (One time Pad) encryption     
#include <stdio.h>
#include <string.h>
#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);
            fflush(stdin);

            if( isalpha(len) ) // Checks if len is a character
            {
                isalphabet=1; //isalphabet becomes NULL if it is a character and not an integer
            }


        } while (len >= strlen(str) || isalphabet == 1); //reiterate the loop until the conditions are satisfied

        int i;
        for(i = 0; i < len; i++)
    {
        printf("%dth digit of Key= ", i+1);
        scanf("%d", &key[i]);
    }
    return(0);  
}
abasu
  • 2,454
  • 19
  • 22
  • I've edited the code by replacing scanf("%d", len) with scanf("%d", &len), but still it's not working. – Arlene Batada Apr 13 '13 at 15:08
  • Thank you dear friend. The code actually works!! Seems like some members didn't comprehend my question or didn't have an answer to it and have marked it negative by 2. But you took pains to find out the solution. It was a great help. Once again, thank you so much. – Arlene Batada Apr 14 '13 at 11:24
  • happy to help, never stop trying and no question is dumb enough :) – abasu Apr 14 '13 at 13:15
0

First, scanf("%d", len); should be

scanf("%d", &len);

The strlen(str) returns the length, and the comparison should be

len > (strlen(str) -1)

and the comparisons for if is an alpha or not, could be done inside the while loop. Check this code.

#include "stdio.h"
#include "string.h"
#include "ctype.h"

#define TRUE 1
#define FALSE 0

int main()
{
    char str[10000];
    char key[10000];

    printf("Enter the string:- ");
    scanf("%[^\n]", str); //stops scanning when user presses return

    int len = 0;
    int isalphabet= TRUE;
    int apply = 1;
    do
    {
        printf("What is the length of the key you are entering?:-");
        scanf("%d", &len);

        isalphabet= isalpha(len);
        apply = (len > (strlen(str) -1)) ;

    } while (!apply || isalphabet); //reiterate the loop until the conditions are satisfied

    int i;
    for(i = 0; i < len; i++)
    {
        printf("%dth digit of Key= ", i+1);
        scanf("%c\n", &key[i]);
    }

    for(i = 0; i < len; i++){
        printf("key[%d] = %c\n", i, key[i]);
    }
    return(0);  
}
  • Thank you for the edited code. I compiled and ran the code, but 'len' accepts the length greater than that of the string. Also, if a naughty user enters any character (eg:- 'a') in the as a value for 'len', the program enters into an inadequate state. – Arlene Batada Apr 13 '13 at 15:07