// 20060207_angou.cpp

// 1バイト文字の文の置換法による暗号復号用のプログラム。

// 暗号化する文章は最初のインデントと最後のピリオッド等は削除し、

// 文の先頭などの大文字は、文意に影響ない範囲で小文字に変更し、

// 文章の最後の文字を文章の最後に充分追加しておく。

 

#include<iostream.h>

#include<stdio.h>

 

int main(void)

{

    const int length = 7;  //暗号コードの長さ

        int code[length] = {6,3,5,1,7,2,4};  //暗号後順列定義

        int decode[length] = {0}; //復号用順列初期化子

        FILE *fpr,*fpw;

        char c[length];

        char cc;

        int flag = 0;

 

//準備

        for(int k = 0; k < length; k++)

        {

            code[k] -= 1;

                c[k] = ' ';

        }

        for( k = 0; k < length; k++)

        {

                decode[code[k]] = k;

    }

    cout << "暗号化:0  or  複合化:1 \n";

        cin >> flag;

 

    switch(flag)

    {

      case 0:  fpr = fopen("E:/Cpp/20060207_angou_R.dat","r");

                   fpw = fopen("E:/Cpp/20060207_angou_codeW.dat","w");

 

               //文読込・暗号化・書込/表示

                           cc = getc(fpr);

               while( cc != EOF )

                   {

                       for(int k = 0; k < length; k++)

                           {

                               c[code[k]] = cc;

                                           cc = getc(fpr);

                                           if( cc == EOF )break;

                           }

                           for(k = 0; k < length; k++)

                           {

                               fprintf(fpw,"%c",c[k]);

                                   cout << c[k];

                           }

                   }

                   cout << "\n\n";

 

               //終了処理

               fclose(fpr);

                   fclose(fpw);

              

                           break;

 

      case 1:  fpr = fopen("E:/Cpp/20060207_angou_codeW.dat","r");

                   fpw = fopen("E:/Cpp/20060207_angou_decodeW.dat","w");

 

               //文読込・復号化・書込/表示

                           cc = getc(fpr);

               while( cc != EOF )

                   {

                       for(int k = 0; k < length; k++)

                           {

                               c[decode[k]] = cc;

                                           cc = getc(fpr);

                                           if( cc == EOF)break;

                           }

                           for(k = 0; k < length; k++)

                           {

                               fprintf(fpw,"%c",c[k]);

                                   cout << c[k];

                           }

                   }

                   cout << "\n\n";

 

               //終了処理

               fclose(fpr);

                   fclose(fpw);

               

                           break;

 

      default: cout << "エラー";

                   break;

        }

 

        return(0);

}