練習問題/解答例/Caesar暗号解読/C
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
-ソースコード
#include <stdio.h>
#include <string.h>
//シフトする順
unsigned char* table = "abcdefghijklmnopqrstuvwxyz .,-a";
//呼び出すごとにstrをシフト
void shift(char* str){
int i;
while(*str != '\0'){
for(i=0; i<30; i++){
if(table[i] == *str){
*str = table[i+1];
break;
}
}
str++;
}
}
//hintの含まれる文字列に解読に成功するとシフト量を返す。...
int decode(char* str, char* hint){
int i;
for(i=1; i<=29; i++){
shift(str);
if(strstr(str, hint) != NULL)
return i;
}
return -1;
}
int main(){
int i;
unsigned char buf[] =
"qdq-gi.q-a ziatmxxitmdqibtqi-ustbi "
"ri.qmoqrcxi.qbubu zir -ibtqi-qp-qaai "
"ripmymsqkir -ibtqi-qy dmxi ri.cnxuoi "
"rruoumxakir -ibtqiqzmobyqzbkii-q.qmxi"
" -imyqzpyqzbi rixmeaki -puzmzoqai -i-"
"qscxmbu zaimzpir -i btq-iymbbq-a;iz -"
"iatmxximzgi.q-a zinqiuzimzgiemgipuao-u"
"yuzmbqpimsmuzabir -ia. za -uzsiacotiim"
"i.qbubu zj";
if((i = decode(buf, "person")) != -1)
printf("%s\nshit=%d\n", buf, i);
else
fprintf(stderr, "faild in decode.\n");
}
-実行結果
$./a.out
every person shall have the right of peaceful petition
for the redress of damage, for the removal of public
officials, for the enactment, repeal or amendment of la...
ordinances or regulations and for other matters; nor shall
any person be in any way discriminated against for
sponsoring such a petition.
shit=18
終了行:
-ソースコード
#include <stdio.h>
#include <string.h>
//シフトする順
unsigned char* table = "abcdefghijklmnopqrstuvwxyz .,-a";
//呼び出すごとにstrをシフト
void shift(char* str){
int i;
while(*str != '\0'){
for(i=0; i<30; i++){
if(table[i] == *str){
*str = table[i+1];
break;
}
}
str++;
}
}
//hintの含まれる文字列に解読に成功するとシフト量を返す。...
int decode(char* str, char* hint){
int i;
for(i=1; i<=29; i++){
shift(str);
if(strstr(str, hint) != NULL)
return i;
}
return -1;
}
int main(){
int i;
unsigned char buf[] =
"qdq-gi.q-a ziatmxxitmdqibtqi-ustbi "
"ri.qmoqrcxi.qbubu zir -ibtqi-qp-qaai "
"ripmymsqkir -ibtqi-qy dmxi ri.cnxuoi "
"rruoumxakir -ibtqiqzmobyqzbkii-q.qmxi"
" -imyqzpyqzbi rixmeaki -puzmzoqai -i-"
"qscxmbu zaimzpir -i btq-iymbbq-a;iz -"
"iatmxximzgi.q-a zinqiuzimzgiemgipuao-u"
"yuzmbqpimsmuzabir -ia. za -uzsiacotiim"
"i.qbubu zj";
if((i = decode(buf, "person")) != -1)
printf("%s\nshit=%d\n", buf, i);
else
fprintf(stderr, "faild in decode.\n");
}
-実行結果
$./a.out
every person shall have the right of peaceful petition
for the redress of damage, for the removal of public
officials, for the enactment, repeal or amendment of la...
ordinances or regulations and for other matters; nor shall
any person be in any way discriminated against for
sponsoring such a petition.
shit=18
ページ名: