矩形転置暗号について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
あや
記事: 28
登録日時: 4年前

矩形転置暗号について

#1

投稿記事 by あや » 4年前

平文を4行8列の暗号鍵で矩形転置で暗号化したいのですが、出力結果が画像のようになってしまいます。ソースコードのどこが間違っているのか教えてください。よろしくお願いします。
添付ファイル
83995404-37F0-45A1-A54E-2D1B51CF1A72.jpeg
これが出力結果です。
D04277A0-279D-40C5-839F-E7140E115246.jpeg
これがソースコードです。
7A17673F-D1FC-4010-815A-4043F40CDDA3.png
これが平文です。

YuO
記事: 947
登録日時: 13年前
住所: 東京都世田谷区

Re: 矩形転置暗号について

#2

投稿記事 by YuO » 4年前

オフトピック
画像ではなくコピペ等、テキストでデータを書いてください。
非常に読みにくく、比較もしにくいです。
とりあえず、平文1行目と暗号化された文を比較しましたが、問題無く暗号化されているように見えます。

コード:

NASA debuted a color picture fro
NASA deb/uted a c/olor pic/ture fro

N | A | S | A |   | d | e | b
u | t | e | d |   | a |   | c
o | l | o | r |   | p | i | c
t | u | r | e |   | f | r | o

Nuot/Atlu/Seor/Adre/    /dapf/e ir/bcco
NuotAtluSeorAdre    dapfe irbcco
何が問題になっているのでしょうか。

あや
記事: 28
登録日時: 4年前

Re: 矩形転置暗号について

#3

投稿記事 by あや » 4年前

NASA debuted a color picture froまでは暗号化されているのですが、それ以降が正しく暗号化されていないので、どこが間違っているのか教えてください。よろしくお願いします。

あや
記事: 28
登録日時: 4年前

Re: 矩形転置暗号について

#4

投稿記事 by あや » 4年前

以下がコードです。

コード:

#include <iostream>
#include <fstream>
using namespace std;



#define M 4 // 矩形の行数
#define N 8 // 矩形の列数
/****************************************
メイン関数
****************************************/
void main() {
	ifstream inFile;    //入力ファイル
	ofstream outFile;   //出力ファイル
	char buffer[M][N];   //文字格納のための配列
	int row = 0;      
	int col = 0;
	

	// 入力ファイルと出力ファイルをオープンする
	inFile.open("plain.txt");
	outFile.open("crypt.txt");
	
	//入力ファイルから、最初の1文字を受け取る
	char ch = inFile.get();
	
	//ファイルの終わり(EOF)でない限り、以下を繰り返す
	while (!inFile.eof()) {
		buffer[row][col] = ch;
		col++;
		if (col == N) {
			col = 0;
			row++;
		}
		ch = inFile.get();

		if (row == M) {
			for (int j = 0; j < N; j++) {
				for (int i = 0; i < M; i++) {
					outFile << buffer[i][j];
					buffer[i][j] = 0;
				}
				col = 0;
				row = 0;
			}
		}
	}

		
	//ファイルを閉じる
	inFile.close();
	outFile.close();
}

YuO
記事: 947
登録日時: 13年前
住所: 東京都世田谷区

Re: 矩形転置暗号について

#5

投稿記事 by YuO » 4年前

あや さんが書きました:
4年前
それ以降が正しく暗号化されていないので
これはどうやって判断しましたか。
自分で考えた「正しい」はどのような結果ですか。

次の32文字も、

コード:

m the Sp/irit rov/er#*on T/uesday s/howing gray rocks

m the Sp
irit rov
er#*on T
uesday s

mieu/ res/ti#s/ht*d/e oa/ roy/So  /pvTs
で問題無いように思えますが (#はCR、*はLF)。


なんとなく、入力ファイルの改行コードがCRLFで、istream::getがCRとLFを別々に読み出しているのだけれど、
改行を無視して考えているか、NewLineひとつと考えて「正しい」を想定していそうな気がします。

あや
記事: 28
登録日時: 4年前

Re: 矩形転置暗号について

#6

投稿記事 by あや » 4年前

暗号化するときは、改行は2文字分として扱うのでしょうか?

YuO
記事: 947
登録日時: 13年前
住所: 東京都世田谷区

Re: 矩形転置暗号について

#7

投稿記事 by YuO » 4年前

あや さんが書きました:
4年前
暗号化するときは、改行は2文字分として扱うのでしょうか?
暗号化とは関係ありません。
・ファイル等における改行には1文字の場合と2文字の場合があり
・2文字の改行についてコンパイラによって1文字の改行\nとして扱われたり2文字の組\r\nとして扱われたりする
ということです。

あや
記事: 28
登録日時: 4年前

Re: 矩形転置暗号について

#8

投稿記事 by あや » 4年前

以下が平文です.
NASA debuted a color picture from the Spirit rover
on Tuesday showing gray rocks peppering a Martian
lake bed awash in its natural hues of red pink
and orange Mission scientists said they were
bowled over by the spectacular quality of
the image taken with a dual camera system
called pancam that is mounted on a mast jutting
up from the rover I think my reaction has been
one of shock and awe said Jim Bell the team
member in charge of pancam Using special software
mission scientists can fly though the image zooming
in on rocks and other landscape features of interest
It is approximately the color that you would see
with your eyes if you were standing there Bell said
The resolution of course is pretty much what you
would see Pancam has 20 20 vision It is three to
four times better than any previous mission that has gone
to Mars in fact these pictures are the highest resolution
highest detailed pictures of Mars ever obtained They
are absolutely spectacular

この平文を行数4,列数8で矩形転置で暗号化したいのですが,最後のabsolutely spectacularのbsolutely spectacularの部分が暗号化されません.
平文の末尾に到達した場合は,矩形の残りの部分にスペースが入った状態にしてから暗号文を取り出して終了するようにしたいです.

以下が現在のコードです.どこを直せばいいのか教えてください.よろしくお願いします.

コード:

#include <iostream>
#include <fstream>
using namespace std;
#define M 4 
#define N 8 
void main() {
	ifstream inFile;    
	ofstream outFile; 
	char buffer[M][N];   
	int row = 0;      
	int col = 0;
	
	inFile.open("plain.txt");
	outFile.open("crypt.txt");
	
	char ch = inFile.get();
		
	while (!inFile.eof()) {
		buffer[row][col] = ch;
		col++;
		if (col == N) {
			col = 0;
			row++;
		}
		ch = inFile.get();

		if (row == M) {
			for (int j = 0; j < N; j++) {
				for (int i = 0; i < M; i++) {
					outFile << buffer[i][j];
					buffer[i][j] = 0;
				}
				col = 0;
				row = 0;
			}
		}
	}

    inFile.close();
	outFile.close();
}

かずま

Re: 矩形転置暗号について

#9

投稿記事 by かずま » 4年前

あや さんが書きました:
4年前
平文の末尾に到達した場合は,矩形の残りの部分にスペースが入った状態にしてから暗号文を取り出して終了するようにしたいです.
inFile.close() の前に次のコードを追加すればよいのではありませんか?

コード:

	if (col != 0 || row != 0) {
		while (1) {
			buffer[row][col] = ' ';
			if (++col == N) {
				col = 0;
				if (++row == M) break;
			}
		}
		for (int j = 0; j < N; j++)
			for (int i = 0; i < M; i++) outFile << buffer[i][j];
	}
あるいは、最初に buffer をスペースで初期化しておき、
途中でも、0 の代わりにスペースで埋め直せば、
最後は、出力するだけで済みます。if (col || row) は必要ですが。

出力のバイト数は 32 の倍数になりますね。

あや
記事: 28
登録日時: 4年前

Re: 矩形転置暗号について

#10

投稿記事 by あや » 4年前

かずまさん。
正しい出力になりました。
詳しく教えていただきありがとうございました☺️

返信

“C言語何でも質問掲示板” へ戻る