2013-04-13 12 views
-2

我正在嘗試在OTP(一次性時間片)加密上創建一個C程序。該程序需要一個字符串從用戶加密。然後它必須在獲取密鑰之前詢問密鑰的長度(整數)。由於我希望程序100%正確,所以我對密鑰的長度進行了限制。正如我們所知,在OTP中,密鑰的長度必須是整數,不能超過文本的長度。那麼,我們如何才能實現這樣一個過濾器?我試着創建代碼,但它不工作。如何限制用戶在C中輸入大於指定長度的字符和整數?

下面的代碼:

//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個」值不應該超過字符串的長度加密,並重申循環,如果這些條件不滿意。有人能指出我的錯誤並顯示解決方案嗎?另外,請解釋爲什麼代碼不能正常工作。

+0

當然,你的意思是關鍵必須是不短於文本?否則,你不得不重複密鑰,這會使加密變得脆弱。 –

+0

@Arlene請解釋您遇到的具體問題。 – 2013-04-13 11:55:56

+0

@NicholasWilson: - 理想情況下,要使密碼100%無法破解,密鑰應該與文本一樣長。由於這在所有情況下都是不可能的,因此密鑰應儘可能長,但在「否」情況下可能會超過要加密的文本的長度。 –

回答

0

檢查scanf("%d", len);是否有某處&失蹤?也初始化len

更新: - 對不起來晚了, OKK,居然還有就是你需要糾正一些東西,對不起,我沒有在第一篇文章指出來。

1)更正標題包含分隔符。使用的<stdio.h>代替"stdio.h"

2)初始化len = 0

3)齊平標準輸入您已經閱讀後。如果你真的輸入一個字符,那麼scanf(%d,&len)不會清除它/讀取它。所以你會陷入一個無限循環,因爲在標準輸入中出現char charcking。最後一個循環也是如此。幸運的是,在這裏你不會被卡住,但循環會過早完成。

4)isalphabet=NULL,isalphabet是int,NULL基本上是一個帶有0x0值的void指針,你應該給0賦一個int。

5)所以你進入循環isalphabet = 0,數字不是字母表,你沒有觸及isalphabet,while循環結束,isalphabet==NULL,在這裏也許它會運行的真實和再次的時間循環開始。那麼什麼時候環路破壞條件設置爲isalphabet

6)爲什麼在c代碼中使用cstring?爲什麼不string.h(但是這更多的是一種個人的選擇:))的

7)修正len > strlen(len >= strlen

我編輯了一點點,檢查是否正常工作

//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); 
} 
+0

我用scanf(「%d」,&len)替換scanf(「%d」,len)編輯了代碼,但仍然不起作用。 –

+0

謝謝親愛的朋友。代碼實際上起作用了!似乎有些成員沒有理解我的問題,或者沒有答案,並已將其標記爲2。但您花了很大力氣找出解決方案。這是一個很大的幫助。再一次,非常感謝你。 –

+0

高興地幫助,永不停止嘗試,沒有問題是愚蠢的:) – abasu

0

首先,scanf函數(「%d」,len);應該是

scanf("%d", &len); 

strlen的(str)返回的長度,比較應

len > (strlen(str) -1) 

和比較用於如果是α與否,可以在while循環中進行。 檢查此代碼。

#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); 
} 
+0

感謝您編輯的代碼。我編譯並運行了代碼,但'len'接受的長度大於字符串的長度。另外,如果頑皮的用戶輸入任何字符(例如:'a')作爲'len'的值,程序將進入不適當的狀態。 –

相關問題