矩形転置暗号について
矩形転置暗号について
平文を4行8列の暗号鍵で矩形転置で暗号化したいのですが、出力結果が画像のようになってしまいます。ソースコードのどこが間違っているのか教えてください。よろしくお願いします。
Re: 矩形転置暗号について
オフトピック
画像ではなくコピペ等、テキストでデータを書いてください。
非常に読みにくく、比較もしにくいです。
非常に読みにくく、比較もしにくいです。
Re: 矩形転置暗号について
NASA debuted a color picture froまでは暗号化されているのですが、それ以降が正しく暗号化されていないので、どこが間違っているのか教えてください。よろしくお願いします。
Re: 矩形転置暗号について
以下がコードです。
#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();
}
Re: 矩形転置暗号について
これはどうやって判断しましたか。
自分で考えた「正しい」はどのような結果ですか。
次の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
なんとなく、入力ファイルの改行コードがCRLFで、istream::getがCRとLFを別々に読み出しているのだけれど、
改行を無視して考えているか、NewLineひとつと考えて「正しい」を想定していそうな気がします。
Re: 矩形転置暗号について
以下が平文です.
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の部分が暗号化されません.
平文の末尾に到達した場合は,矩形の残りの部分にスペースが入った状態にしてから暗号文を取り出して終了するようにしたいです.
以下が現在のコードです.どこを直せばいいのか教えてください.よろしくお願いします.
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: 矩形転置暗号について
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];
}
途中でも、0 の代わりにスペースで埋め直せば、
最後は、出力するだけで済みます。if (col || row) は必要ですが。
出力のバイト数は 32 の倍数になりますね。