所以我正在学习计算机科学的edX课程之一(不是为了学分,只是为了帮助自学),高级问题集与解密密码有关。为了更好地理解用于进行加密的函数crypt(),我做了以下代码来弄清楚用于“盐”字符串的内容(终端中的man crypt()命令说:(“salt是一个从字符集[a-zA-Z0-9./]中选择的两个字符的字符串。这个字符串用于以4096种不同的方式之一扰乱算法。“))请告诉我你是否可以理解为什么我是得到这个错误。
以下是我使用的代码。我在尝试编译时收到此错误:
test.c:37:33: error: incompatible integer to pointer conversion passing 'char' to
parameter of type 'char *'; take the address with & [-Werror,-Wint-conversion]
char *salt = strcat(whatever1,whatever2);
代码:
#define _XOPEN_SOURCE //this is needed for the unistd and crypt stuff
#include <stdio.h>
#include <unistd.h> //this lets me do the crypt() fcn
#include <cs50.h> //just lets me use string as a type among w/some input fcns and is made by the course
#include <string.h>
#include <ctype.h> //this lets me use toascii()
char *word = "caesar";
char *password;
char toASCII_New(int i) //attempt to turn input to HEX then corresponding ASCII character
{
for(int num = i; num<= 172; num++)
{
for(int hex_num = 0x41; hex_num <= 0x7a; hex_num++)
{
if(num == hex_num)
{
char letter = (char) toascii(hex_num); //should convert hex to ascii here
return letter;
}
}
}
char backup = 'a';
return backup;
}
string checker(int first, int second) //Checks the inputs to see if the can be the correct SALTS input to crypt
{
for (int i = first; i<=first+25; i++)
{
for(int j = second; j<=second+25; j++)
{
char whatever1 = (char) toASCII_New(i);
char whatever2 = (char) toASCII_New(j);
char *salt = strcat(whatever1,whatever2);
password = crypt(word,salt);
int compare = strcmp(password,"50zPJlUFIYY0o"); // this is what i'm really checking
if (compare == 0)
return salt;
}
}
return "no";
}
int main(void)
{
string AA = checker(65,65);
string Aa = checker(65,97);
string aA = checker(97,65);
string aa = checker(97,97);
printf("AA: %s\nAa: %s\naA: %s\naa: %s\n",AA,Aa,aA,aa);
}
答案 0 :(得分:2)
strcat用于连接字符串而不是2个单独的字符。 &#34; whatever1&#34;将连接的字符串保持在一起。为什么要使用-dangerous-strcat操作,只需要一个2字节的字符串?只需使用:
char salt[3];
salt[0]=toASCII_New(i); /* why the casting to char ? you already returned a char... */
salt[1]=toASCII_New(j);
salt[2]='\0'; /* to terminate the string properly */
答案 1 :(得分:1)
以下是一些一般性建议,因为整个代码需要重构:
函数toASCII_New
应该按如下方式重写:
char toASCII_New(int c)
{
if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'))
return (char)toascii(c);
return 'a';
}
函数checker
应该按如下方式重写:
int checker(int first, int second, char salt[3])
{
for (int i=first; i<=first+25; i++)
{
for (int j=second; j<=second+25; j++)
{
salt[0] = toASCII_New(i);
salt[1] = toASCII_New(j);
password = crypt(word,salt);
if (strcmp(password,"50zPJlUFIYY0o") == 0)
return 1;
}
}
return 0;
}
函数main
应该按如下方式重写:
int main()
{
char salt_AA[3] = {0};
char salt_Aa[3] = {0};
char salt_aA[3] = {0};
char salt_aa[3] = {0};
int salt_AA_OK = checker('A','A');
int salt_Aa_OK = checker('A','a');
int salt_aA_OK = checker('a','A');
int salt_aa_OK = checker('a','a');
printf("AA: %s\n",salt_AA_OK? salt_AA:"failed");
printf("Aa: %s\n",salt_Aa_OK? salt_Aa:"failed");
printf("aA: %s\n",salt_aA_OK? salt_aA:"failed");
printf("aa: %s\n",salt_aa_OK? salt_aa:"failed");
return 0;
}