c - CS50 Pset 2. 我的crack.c 代码编译并运行但不打印任何内容
问题描述
到目前为止,我正在研究 Pset 2 crack.c,并且我已经设法获得了一般概念。但是,我的代码仍然无法正常工作。它编译并运行,但不打印任何内容。
我不完全确定这里出了什么问题,我可能忽略了一些东西?
#include <stdio.h>
#include <cs50.h>
#define _XOPEN_SOURCE
#include <unistd.h>
#include <crypt.h>
#include <string.h>
char plaintext[4];
char salt [3];
string alphapool;
string newhash;
int main (int argc, string argv[]) {
if (argc != 2)
{
printf("Error: Input 2 command line arguments\n");
return 1;
}
else
{
alphapool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
salt[0] = argv[1][0]; salt[1] = argv[1][1]; salt[2] = '\0';
//match a plaintext guess to an array in possibilities (alpha pool)
//for one letter
for(int i = 0; i < 26+26; i++)
{ //single letter
plaintext[0] = alphapool[i];
plaintext[1] = '\0';
newhash = crypt(plaintext, salt);
if ((strncmp(argv[1], newhash,13)) == 0)
{
printf("Match found, password is %s",plaintext);
return true;
}
}
for(int i = 0; i < 26+26; i++)
{//double letter
plaintext[0] = alphapool[i];
for(int j = 0; j < 26+26; j++)
{
plaintext[1] = alphapool[i];
plaintext[2] = '\0';
newhash = crypt(plaintext, salt);
if ((strncmp(argv[1], newhash,13)) == 0)
{
printf("Match found, password is %s", plaintext);
return true;
}
}
}
for(int i = 0; i < 26+26; i++)
{//three letter
plaintext[0] = alphapool[i];
for(int j = 0; j < 26+26; j++)
{
plaintext[1] = alphapool[i];
for (int k = 0; k < 26+26; k++)
{
plaintext[2] = alphapool[i];
plaintext[3] = '\0';
}
newhash = crypt(plaintext, salt);
if ((strncmp(argv[1], newhash,13)) == 0)
{
printf("Match found, password is %s", plaintext);
return true;
}
}
}
for(int i =0; i < 26+26; i++)
{
//four letter word
plaintext[0] = alphapool[i];
for(int j = 0; j < 26+26; j++)
{
plaintext[1] = alphapool[i];
for (int k = 0; k < 26+26; k++)
{
plaintext[2] = alphapool[i];
for (int l = 0; l < 26+26; l++)
{
plaintext[3] = alphapool[i];
}
newhash = crypt(plaintext, salt);
if ((strncmp(argv[1], newhash,13)) == 0)
{
printf("Match found, password is %s", plaintext);
return true;
}
}
}
}
return 0;
}
}
我希望生成的明文应该是产生与给定哈希匹配的哈希的值。
解决方案
输出的字符串可能已缓冲,您应该fflush
在从程序返回之前使用 (cf man page) 强制将缓冲区写入终端。
推荐阅读
- javascript - React + Redux:设置 redux 状态或更改 redux 状态后页面不重新渲染
- powershell - 如何批量下载文件并从名称列表中重命名它们
- spring-boot - 带有 ssl (https) 的 Zull 代理 Eureka 客户端没有注册到 Eureka Server
- c# - C# 下载具有给定参数的网页内容
- c# - LINQ 查询:具有另一个表的 Count 元素的新查询对象
- java - 休眠中ManyToMany关系的无限递归
- sql - 如何从 SQL 加载器加载当前月份记录?
- python - 如何在 NLP 中提取不寻常的生词
- node.js - Socket.io 不会立即在新创建的房间中发送分组事件
- python - 命中 API 端点时如何从 Web 服务调用 python 命令行工具?