非常简单的地穴程序错误 [英] Very Simple Crypt Program Error

查看:131
本文介绍了非常简单的地穴程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个程序,需要一个文件作为输入和放大器;输出的每一行的解密密码。该文件包含三行加密密码。我保证密码解密,以4字母的单词。鉴于我创建了信的的char []。我有一个问题,使用低级IO读取由行的文件行,把产生的密码在一个新的文件。任何与放大器;所有的建议是AP preciated!

下面是我的code迄今:

 的#include< SYS / types.h中>
#包括LT&; SYS / stat.h>
#包括LT&;&fcntl.h GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&string.h中GT;的#define BUFFER_SIZE 1024INT主要(无效)
{   INT F =打开(pass.txt,O_RDONLY);
   如果(F℃,)
      返回0;
   焦炭BUF [1024];
   而(my_fgets(BUF,sizeof的(BUF),F))
      的printf(%S \\ n,BUF);
   关闭(F);
   为const char * const的传球=$ 1 $ 6gMKIopE $ I.zkP2EvrXHDmApzYoV.B。
   字符*的结果;
   诠释OK;
   CHAR猜想[] = {'A​​','A','A','A','\\ 0'};
   炭CH1 =猜测[0],CH 2 =猜想[1],CH 3 =猜想[2],CH 4 =猜想[3];
   做{
      对于(CH1,CH1< ='Z'; ++ CH1)
      {
         为(CH2 ='A'; CH2< ='Z'; ++ CH 2)
         {
            为(CH3 ='A'; CH3< ='Z'; ++ CH3)
            {
               为甲烷(C​​H4 ='A'; CH4< ='Z'; ++ CH4)
               {
                  结果=隐窝(猜,通);
               }            }
         }
      }
   }而(STRCMP(因此,通)!= 0);
   看跌期权(猜测);
   返回0;
}INT my_fgets(字符* BUF,INT LEN,INT F)
{
   的for(int i = 0; I< LEN,我++,BUF ++)
   {
      诠释计数=读(F,BUF,1);
      如果(!计数||(BUF [0] =='\\ n'))
      {
         BUF [0] = 0;
         返回我;
      }
   }
   返回0;
}

文本文件:

  $ 1 $ 6gMKIopE $ I.zkP2EvrXHDmApzYoV.B。
$ 1 $ pkMKIcvE $ WQfqzTNmcQr7fqsNq7K2p0
$ 1 $ 0lMKIuvE $ 7mOnlu6RZ / cUFRBidK7PK。


解决方案

code的以下块似乎是有缺陷的:

 而(my_fgets(BUF,sizeof的(BUF),F))
   的printf(%S \\ n,BUF);

什么,做是preserve只有最后一行。其他线被读出并弃去。

此外,目前尚不清楚如何 CH1 应该被初始化到做...而循环。你有它的:

 为(CH1,CH1< ='Z'; ++ CH1)
// ^^^

这不初始化 CH1 。这是一个前pression无副作用。

我建议把大部分的逻辑的密码解密在其自身的功能和阅读每一行后调用该函数。

 无效decryptPassword(字符* BUF)
{
   的printf(%S \\ n,BUF);
   为const char * const的传球=$ 1 $ 6gMKIopE $ I.zkP2EvrXHDmApzYoV.B。
   字符*的结果;
   诠释OK;
   CHAR猜想[] = {'A​​','A','A','A','\\ 0'};
   炭CH1 =猜测[0],CH 2 =猜想[1],CH 3 =猜想[2],CH 4 =猜想[3];
   做{
      对于(CH1,CH1< ='Z'; ++ CH1)
      {
         为(CH2 ='A'; CH2< ='Z'; ++ CH 2)
         {
            为(CH3 ='A'; CH3< ='Z'; ++ CH3)
            {
               为甲烷(C​​H4 ='A'; CH4< ='Z'; ++ CH4)
               {
                  结果=隐窝(猜,通);
               }
            }
         }
      }
   }而(STRCMP(因此,通)!= 0);
   看跌期权(猜测);
}INT主要(无效)
{
   INT F =打开(soc.in,O_RDONLY);
   如果(F℃,)
      返回0;
   焦炭BUF [1024];
   而(my_fgets(BUF,sizeof的(BUF),F))
      decryptPassword(BUF);
   关闭(F);
   返回0;
}

I'm creating a program that takes a file as input & outputs the decrypted password of each line. The file contains three lines of encrypted passwords. I am guaranteed that the passwords decrypt to 4 letter words. Given that i have created an char[] of the letters. I'm having a problem reading the file line by line using LOW LEVEL IO and putting the resulting password in a new file. Any & all advice is appreciated!

Here is my code so far:

#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;
}

Text file:

$1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B.
$1$pkMKIcvE$WQfqzTNmcQr7fqsNq7K2p0
$1$0lMKIuvE$7mOnlu6RZ/cUFRBidK7PK.

解决方案

The following block of code appears to be flawed:

while (my_fgets(buf, sizeof(buf), f)) 
   printf("%s\n", buf);

What that does is preserve only the last line. The other lines are read and discarded.

Also, it's not clear how ch1 is supposed to be initialized to in each iteration of the do ... while loop. You have it as:

for(ch1; ch1<='z';++ch1)
//  ^^^

That doesn't initialize ch1. It's an expression with no side effects.

I suggest putting most of the logic for password decryption in a function of its own and calling the function after reading every line.

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;
}

这篇关于非常简单的地穴程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆