2017-07-25 10 views
1

私はCで暗号化に干渉してきました。単純な代用暗号を使用していましたが、次のコードで問題が発生しました。プログラムは円滑に実行されますが、テキストファイル "Message"の内容は常に同じテキストに変更されます: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:その出力は以下のようになります/ランダム/ srand)? – BLUEPIXY

+1

知っておく必要があることがいくつかあります:最初に、関数内でローカルにする必要がある変数に引数を使用する必要はありません。 2番目のことは、[* seeding *](http://en.cppreference.com/w/c/numeric/random/srand)がなければ、常に同じ "ランダムな"数字が得られることです。 3番目は、ファイル内の何かを置き換えないことです。ファイルの内容を無条件に上書きするだけです。最後に、 "乱数"を使用するとファイルを解読することは不可能*になります。あなたは元の文字を取り戻すためにどのような値を引くべきかをどのように知っていますか? –

+1

全体として、私はあなたがいくつかのステップを取り戻す必要があると思う[いくつかの良い初心者の本を得る](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-リスト)を開き、やり直してください。 –

答えて

0

共有コードでかなりの数の問題があり、それらは:

  1. あなたはランド()/にsrand()関数を使用している場合、どのように暗号化されたテキストを復号化します。
  2. ファイル[i] = rand()+ 26; ここでは、文字の値をダンプするだけで、ファイルにダンプすることは意味がありません。テキストを暗号化する必要があります。

参考のため、シーザーサイファー[代替置換型のタイプ]で簡単なサンプルコードを添付しています。

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; 
} 
関連する問題