我正在创建一个以文件为输入的程序&输出每行的解密密码。该文件包含三行加密密码。我保证密码解密为4个字母的单词。鉴于我已经创建了字母的char []。我使用LOW LEVEL IO逐行读取文件并将生成的密码放在新文件中时遇到问题。任何&所有建议表示赞赏!
到目前为止,这是我的代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUFFER_SIZE 1024
int main(void)
{
int f = open("pass.txt", O_RDONLY);
if (f < 0)
return 0;
char buf[1024];
while (my_fgets(buf, sizeof(buf), f))
printf("%s\n", buf);
close(f);
const char *const pass = "$1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B.";
char *result;
int ok;
char guess[] = {'a','a','a','a','\0'};
char ch1=guess[0], ch2=guess[1], ch3=guess[2], ch4=guess[3];
do{
for(ch1; ch1<='z';++ch1)
{
for(ch2='a';ch2<='z';++ch2)
{
for(ch3='a';ch3<='z';++ch3)
{
for(ch4='a';ch4<='z';++ch4)
{
result = crypt(guess, pass);
}
}
}
}
}while(strcmp(result, pass) != 0);
puts(guess);
return 0;
}
int my_fgets(char* buf, int len, int f)
{
for (int i = 0; i < len; i++,buf++)
{
int count = read(f, buf, 1);
if (!count || (buf[0] == '\n'))
{
buf[0] = 0;
return i;
}
}
return 0;
}
文字档案:
$1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B.
$1$pkMKIcvE$WQfqzTNmcQr7fqsNq7K2p0
$1$0lMKIuvE$7mOnlu6RZ/cUFRBidK7PK.
答案 0 :(得分:0)
以下代码块似乎存在缺陷:
ch1
这样做只保留最后一行。其他行被读取并丢弃。
此外,还不清楚do ... while
循环的每次迭代中应该如何初始化for(ch1; ch1<='z';++ch1)
// ^^^
。你有它:
ch1
这不会初始化void decryptPassword(char* buf)
{
printf("%s\n", buf);
const char *const pass = "$1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B.";
char *result;
int ok;
char guess[] = {'a','a','a','a','\0'};
char ch1=guess[0], ch2=guess[1], ch3=guess[2], ch4=guess[3];
do{
for(ch1; ch1<='z';++ch1)
{
for(ch2='a';ch2<='z';++ch2)
{
for(ch3='a';ch3<='z';++ch3)
{
for(ch4='a';ch4<='z';++ch4)
{
result = crypt(guess, pass);
}
}
}
}
}while(strcmp(result, pass) != 0);
puts(guess);
}
int main(void)
{
int f = open("soc.in", O_RDONLY);
if (f < 0)
return 0;
char buf[1024];
while (my_fgets(buf, sizeof(buf), f))
decryptPassword(buf);
close(f);
return 0;
}
。这是一种没有副作用的表达。
我建议将大部分密码解密逻辑放在自己的函数中,并在读完每一行后调用该函数。
public MainForm()
{
InitializeComponent();
display();
network = new Network(this);
btn = new Button();
}
private void btnClick(object sender, EventArgs e)
{
btn = (Button)sender;
if (btn.Name == "button1")
{
col = 1;
row = 1;
}
else if(btn.Name == "button2"){
col = 2;
row = 1;
}else if(btn.Name == "button3"){
col = 3;
row = 1;
}else if(btn.Name == "button4"){
col = 1;
row = 2;
}else if(btn.Name == "button5"){
col = 2;
row = 2;
}else if(btn.Name == "button6"){
col = 3;
row = 2;
}else if(btn.Name == "button7"){
col = 1;
row = 3;
}else if(btn.Name == "button8"){
col = 2;
row = 3;
}
else if (btn.Name == "button9")
{
col = 3;
row = 3;
}
MakeMove(row, col);
}
public void MakeMove(int row, int col){
if ((statusBar.Text == "Connecting...") ||
(statusBar.Text == "Waiting for connection..."))
return;
//_____________________________________________________________________________________________
//
// Move network move
//_____________________________________________________________________________________________
//wServer = true;
//MessageBox.Show("wServer is: " + wServer);
if (((wServer == true) && (turn == true) && (isNetworkPlay == false)) ||
((wClient == true) && (turn == false) && (isNetworkPlay == false)))
{
//MessageBox.Show("retgr");
network.SendMove(row, col);
}
else
{
if (((wServer == true) && (turn == false) && (isNetworkPlay == false)) ||
((wClient == true) && (turn == true) && (isNetworkPlay == false)))
return;
}
//checks whos turn it is and sets the button to X or O
//THIS IS WHERE I AM HAVING TROUBLE
if (!turn)
{
//btn.Text = "X";
SetControlPropertyValue(btn, "Text", "X");
}
else
{
//btn.Text = "O";
SetControlPropertyValue(btn, "Text", "O");
}
turn = !turn;
//btn.Enabled = false;
SetControlPropertyValue(btn, "Enabled", false);
turnCount++;
//checks who wins
checkWinner();
display();
isNetworkPlay = false;
}![enter image description here][2]