gpt4 book ai didi

c - C中的加密函数

转载 作者:行者123 更新时间:2023-11-30 20:33:37 25 4
gpt4 key购买 nike

enter image description here所以我需要编写一个程序来扫描单词,然后用给定的 key 对它们进行加密( key 在 main 中)。输出应包含加密的单词(加密使用带有单词中字母的 ASCII 值的 key ,因此假设我们有单词 BOOK,那么第一个字母将切换到第 65 位的 key )。我需要将用户给出的单词扫描到字符串中,并将指向这些字符串的指针保存到数组 char *words 中。然后我加密它们并按如下方式打印它们:

words[0][0-n]\n ('n' is the length of the word=the same length as original but encr)
words[1][0-k]\n ('k' is the length of the word=the same length as original but encr)

。。。我是编程新手,并且在使用这些字符串时遇到问题..帮助

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define KEY_SIZE 256



char encrypt_char(unsigned char key[KEY_SIZE], char ch);
int read_words(char* words[], int size, int max_str_len);
void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE]);




int main()
{
int i=0,j=0;
unsigned char key[KEY_SIZE] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D',
'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'};

char* words[1000]; //the words must bee a 2D array beacause its a string array .
int num_words=read_words(words,1000,100);
printf("number of words =%d",num_words);/////check
encrypt_words(words,num_words,key);
while (words[i][j]!=0)
{
printf("%c",words[i][j]);
i++,j++;
}
return 0;
}






char encrypt_char(unsigned char key[KEY_SIZE], char ch)
{
int temp=ch;
return key[temp];
}





int read_words(char* words[], int size, int max_str_len)
{
printf("in the read words func\n"); ///check
int i=0,words_count=0;
char* temp=malloc((max_str_len+1)*sizeof(char));
if (temp==NULL)
{
printf("Memory allocation failure\n");
free(temp);
return 0;
}
printf("passed the fail test\n"); ////check
while ( scanf("%s",temp)==1&&words_count<size)
{
printf("in the while \n");
int length=strlen(temp);
char* the_word=malloc((length+1)*sizeof(char));
strcpy(the_word, temp);
words[i]=(char*)the_word;
i++;
words_count++;
printf("the i=%d words count=%d words[%d]=%s \n",i,words_count,i,&words[i]);/////////check
}
printf("WORDS COUNT IS=%d\n",words_count);
return words_count;

}


void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE])
{
int i=0,j=0;
while (words[i][j]!=0&&i<num_words)
{
char temp=words[i][j];
words[i][j]=encrypt_char(key,temp);
i++,j++;
}
}

编译器消息:

-------------- Build: Debug in hw4q2 (compiler: GNU GCC Compiler)---------------

mingw32-gcc.exe -Wall -Werror -pedantic -ansi -W -O -g -pedantic-errors -std=c99 -c "C:\Users\Anton\Desktop\C projects\hw4q2\hw4q2.c" -o obj\Debug\hw4q2.o
mingw32-g++.exe -o hw4q2.exe obj\Debug\hw4q2.o
Output file is hw4q2.exe with size 73.25 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

最佳答案

您的问题出在这段代码中:

while (words[i][j]!=0)
{
printf("%c",words[i][j]);
i++,j++;
}

此处同时递增 ij。因此,你们都可以一步跳到下一个字母和下一个字符串。如果您跳出字符串终止符,则会失败。

试试这个:

for (i=0; i<num_words; ++i)
{
printf("%s\n",words[i]);
}

类似的问题在这里:

void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE])
{
int i=0,j=0;
while (words[i][j]!=0&&i<num_words)
{
char temp=words[i][j];
words[i][j]=encrypt_char(key,temp);

i++,j++; // Ups...
}
}

您需要先完成一个字符串(即仅递增 j),然后移动到下一个字符串(即递增 i)。

类似于:

void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE])
{
int i=0,j=0;
for (i=0; i<num_words; ++i)
{
j = 0;
while (words[i][j]!=0)
{
char temp=words[i][j];
words[i][j]=encrypt_char(key,temp);
j++;
}
}
}

关于c - C中的加密函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44683952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com