带Crypt的低级IO

时间:2015-06-02 22:40:43

标签: c encryption crypt unistd.h low-level-io

我正在尝试将从文件的每一行获取的加密字符串与AAAA-ZZZZ进行比较,直到找到其匹配的密码。我保证用户密码是4个字符。我想要做的是使用LowLevel IO接收文件并输出到每个行的解密密码的新文件。我不是C编程中最好的,所以请保持温和。我需要指导如何创建从AAAA一直到ZZZZ的数组或列表,然后将每个数组或列表与文件行的解密版本进行比较。

  1. 如何逐行解密文件并将其保存到char []
  2. 如何将每一行与另一个char []进行比较,直至找到密码
  3. 例如:

    如果该行是$ 1 $ 6gMKIopE $ I.zkP2EvrXHDmApzYoV.B。下一行是$ 1 $ pkMKIcvE $ WQfqzTNmcQr7fqsNq7K2p0。假设解密后得到的密码是ABSZ和TAZE,新文件将导致第一行为ABSZ,第二行为TAZE。

    这是我到目前为止所做的:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>${greeting}</h1>
    </body>
    </html>
    

    这是文件:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    void pdie(const char *);
    void die(const char *);
    
    #define BUFFER_SIZE 1024 
    
    int main(void)
    {
       char *pass;
       int rfd;   
       int wfd;   
       char buffer[BUFFER_SIZE];   
       char *bp;   
       int bufferChars;   
       int writtenChars;  
    
       if ((rfd = open("pass.txt", O_RDONLY, 0)) < 0)
          pdie("Open failed");
    
       if ((wfd = open("passout.txt", O_WRONLY | O_CREAT | O_TRUNC,
                       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
          pdie("Open failed");
    
       while (1)
       {   
          if ((bufferChars = read(rfd, buffer, BUFFER_SIZE)) > 0)
          {
             printf("%s", buffer);
    
             bp = buffer; 
             pass = crypt(getpass(all(4,4,'a','z')), *bp);  
    
             printf(pass);
             while (bufferChars > 0)
             {
                if ((writtenChars = write(wfd, bp, bufferChars)) < 0)
                   pdie("Write failed");
    
                bufferChars -= writtenChars;
                bp += writtenChars;
             }
          }
          else if (bufferChars == 0)  
             break;
          else   
             pdie("Read failed");
       }
       close(rfd);
       close(wfd);
    
       return 0;
    }
    
    
    void pdie(const char *mesg) {
    
       perror(mesg);
       exit(1);
    }
    
    void die(const char *mesg) {
    
       fputs(mesg, stderr);
       fputc('\n', stderr);
       exit(1);
    }
    
    int inc(char *c,char begin, char end){
        if(c[0]==0) return 0;
        if(c[0] == end){   // This make the algorithm to stop at char 'f'
            c[0]=begin;     // but you can put any other char            
            return inc(c+sizeof(char), begin, end);
        }   
        c[0]++;
        return 1;
    }
    
    int all(int a, int n,char begin, char end){
        int i,j;
        char *c = malloc((n+1)*sizeof(char));
        for(i=a;i<=n;i++){
            for(j=0;j<i;j++) c[j]=begin;
            c[i]=0;
            do {
                printf("%s\n",c);
            } while(inc(c,begin,end));
        }
        free(c);
    }
    

0 个答案:

没有答案