2017-07-25 18 views
1

在過去的幾周裏,我一直在干擾C中的加密。我一直在使用一個簡單的替代密碼,但是我遇到了以下代碼的問題。雖然程序運行平穩,但文本文件「消息」的內容總是變爲相同的文本:C=Øžû†。我希望將文件中字符串的每個字符都改爲隨機字母。替代密碼給我在C程序中的相同文本

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h> 
#include <stdlib.h> 
#include <Windows.h> 
#include <type.h> 
#include <string.h> 

const int MAXSIZE = 50; 

void Encrypt(FILE *File, char file[MAXSIZE], int i, int j) 
{ 
    File = fopen("message.txt", "r+"); 
    for (i = 0; i < 6; i++) 
    { 
     file[i] = rand() + 26; 
     fputc(file[i], File); 
    } 

    printf("%s", file); 

    fclose(File); 
    return; 
} 

int main() 
{ 
    int i = 0; 
    int j = 0; 
    char file[MAXSIZE]; 
    FILE *File = 0; 

    Encrypt(File, file, i, j); 

    system("pause"); 
    return 0; 
} 
+2

需要[函數srand](http://en.cppreference.com/w/c/numeric/random/srand)? – BLUEPIXY

+1

有幾件事情你需要知道:第一,你不必爲函數中本地變量使用參數。第二件事是沒有[* seeding *](http://en.cppreference.com/w/c/numeric/random/srand)你將總是得到相同的「隨機」數字。第三個是你不替換文件中的任何東西,你只需*無條件地覆蓋文件的內容。最後,使用「隨機」數字使*解密文件成爲不可能*。你怎麼知道減去什麼價值來找回原始人物? –

+1

總而言之,我認爲你需要採取幾個步驟,[得到一些好的初學者書籍](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-列表)並重新開始。 –

回答

0

沒有與共享的代碼相當多的問題,它們分別是:

  1. 如果你正在使用蘭特(你將如何解密加密的文本)/ srand()函數的功能。
  2. file [i] = rand()+ 26; 在這裏,你只是傾銷任何字符值,而不是傾銷到文件,這是沒有任何意義的,你需要加密一些文本。

我附上一個簡單的示例代碼與Ceaser密碼[替代卡尺類型]供您參考。其輸出將是如下:

TESTTEST 123123 TESTTEST
tbwxatzd 164904 AOWPTBWX
TESTTEST 123123 TESTTEST

#include <stdio.h> 
#include <string.h> 

static char key[] = "helloworld"; 

int Encrypt(char *aText,char *aKey) 
{ 
    int lTextlen = strlen(aText); 
    int lKeylen  = strlen(aKey); 

    int lCount,lShift; 
    int lCount1 = 0; 

    for(lCount = 0; lCount < lTextlen;lCount++) 
    { 
     if(aText[lCount] >= 'a' && aText[lCount] <= 'z') 
     { 
      lShift = aKey[lCount1] % 26; 
      aText[lCount] = ((aText[lCount] + lShift - 97) % 26) + 97; 
     } 
     else if(aText[lCount] >= 'A' && aText[lCount] <= 'Z') 
     { 
      lShift = aKey[lCount1] % 26; 
      aText[lCount] = ((aText[lCount] + lShift - 65) % 26) + 65; 
     } 
     else if(aText[lCount] >= '0' && aText[lCount] <= '9') 
     { 
      lShift = aKey[lCount1] % 10; 
      aText[lCount] = ((aText[lCount] + lShift - 48) % 10) + 48; 
     } 
     else 
     { 
     } 
     lCount1 = (lCount1 + 1) % lKeylen; 
    } 
} 

int Decrypt(char *aText,char *aKey) 
{ 
    int lTextlen = strlen(aText); 
    int lKeylen  = strlen(aKey); 

    int lCount,lShift; 
    int lCount1 = 0; 

    for(lCount = 0; lCount < lTextlen;lCount++) 
    { 
     if(aText[lCount] >= 'a' && aText[lCount] <= 'z') 
     { 
      lShift = 26 - (aKey[lCount1] % 26); 
      aText[lCount] = ((aText[lCount] + lShift - 97) % 26) + 97; 
     } 
     else if(aText[lCount] >= 'A' && aText[lCount] <= 'Z') 
     { 
      lShift = 26 - (aKey[lCount1] % 26); 
      aText[lCount] = ((aText[lCount] + lShift - 65) % 26) + 65; 
     } 
     else if(aText[lCount] >= '0' && aText[lCount] <= '9') 
     { 
      lShift = 10 - (aKey[lCount1] % 10); 
      aText[lCount] = ((aText[lCount] + lShift - 48) % 10) + 48; 
     } 
     else 
     { 
     } 
     lCount1 = (lCount1 + 1) % lKeylen; 
    } 
} 
int main() 
{ 
    char plaintext[] = "testtest 123123 TESTTEST"; 
    printf("%s\n",plaintext); 
    Encrypt(plaintext,key); 
    printf("%s\n",plaintext); 
    Decrypt(plaintext,key); 
    printf("%s\n",plaintext); 
    return 0; 
}