无法从'void *'转换为'char'

时间:2014-01-18 19:21:58

标签: c function

我是C的新手并且毫无疑问犯了一些错误,但我同意尝试学习它以帮助一个朋友,但似乎已经碰到了一堵砖墙。我已经构建了一些代码来移位字节,以加密工作正常的数据。但是当我尝试使用用户生成的密钥实现XOR时,我在标题中得到了错误(下面的代码)。

非常感谢任何帮助。

#define ENCRYPTION_FORMULA(int) Byte + 25
#define DECRYPTION_FORMULA(int) Byte - 25
#define MAX_LENGTH 40#define MIN_LENGTH 10

//Prototypes (the different sections of the program are used to encrypt and decrypt data):
void displayMenu();
char * string_toupper(char * s);
int Encrypt (char* FILENAME, char *NEW_FILENAME, char *USER_KEY);
int Decrypt(char * FILENAME, char * NEW_FILENAME);



void main() {
    char select[21];
    char FILENAME[150];
    char *USER_KEY=malloc(MAX_LENGTH);
    char NEW_FILENAME[150];
    char Byte;
    char newByte;

    displayMenu();
    scanf("%s", select);
    string_toupper(select);






    while (strcmp(select, "EXIT") != 0) //While the user does not exit the program. Option 4: Exit the program
    {
        if (strcmp(select, "ENCRYPT") == 0) //If "Encrypt" is chosen, this option will start the encryption process of a file.
        {
            printf("-------------------------------------------------------------\n");
            printf("Please Enter an encryption password. Between 10-14 characters\n");
            scanf("%s", USER_KEY);
            if (strlen(USER_KEY) < MIN_LENGTH){
                printf("---------------------------------------------------------------\n");
                printf("Error, the you entered password is too short. Please try again.\n");
                printf("---------------------------------------------------------------\n");
            } else if (strlen(USER_KEY) > MAX_LENGTH) {
                printf("--------------------------------------------------------------\n");
                printf("Error, the you entered password is too long. Please try again.\n");
                printf("--------------------------------------------------------------\n");
            } else {
                printf("-------------------------------------------------------------\n");
                printf("Enter the source file name for to begin the encrypt process.\n");
                printf("-------------------------------------------------------------\n");
                scanf("%s", FILENAME);
                printf("-------------------------------------------------------------\n");
                printf("Enter the desired destination for the file.\n");
                printf("-------------------------------------------------------------\n");
                scanf("%s", NEW_FILENAME);
                printf("-------------------------------------------------------------\n");
                Encrypt(FILENAME, NEW_FILENAME, USER_KEY);
            }
        } else if (strcmp(select, "DECRYPT") == 0) 

      //If "Decrypt" is chosen, this option will start the decryption process of a file.
        {

           printf("---------------------------------------------------------------\n");
            printf("Enter the source file name for to begin the decryption process.\n");
           printf("---------------------------------------------------------------\n");
            scanf("%s", FILENAME);
           printf("---------------------------------------------------------------\n");
            printf("Enter the desired destination for the file.\n");
           printf("---------------------------------------------------------------\n");
            scanf("%s", NEW_FILENAME);
           printf("---------------------------------------------------------------\n");
            Decrypt(FILENAME, NEW_FILENAME);

        } else {
            printf("-------------------------------------------------------------\n");
            printf("INVALID SELECTION - PLEASE TRY AGAIN!\n");
            printf("-------------------------------------------------------------\n");

        }

        displayMenu();
        scanf("%s", select);
        string_toupper(select);
    }
}

void displayMenu() {
    printf("  ********************************************************\n");
    printf("  *          XX - Encryption Algorithm             *\n");
    printf("  *       (Weclome to XX's Encryption Algorithm!)       *\n");
    printf("  *                                                      *\n");
    printf("  *  ENCRYPT  = Select a file to encrypt              {1}*\n");
    printf("  *  DECRYPT  = Select a file to decrypt              {2}*\n");
    printf("  *  EXIT     = Exit Program                          {3}*\n");
    printf("  ********************************************************\n");
}

Encryption Function:

int Encrypt(char * FILENAME, char * NEW_FILENAME, char *USER_KEY) {
    FILE * inFile; //Declare inFile
    FILE * outFile; //Declare outFile

    char Byte = 0;
    char newByte;
    int n;


    int i = 0;

    inFile = fopen(FILENAME, "rb");
    outFile = fopen(NEW_FILENAME, "w");

    if (inFile == NULL) {
        printf("-------------------------------------------------------------\n");
        printf("Error: Can't open inFile.\n");
        printf("-------------------------------------------------------------\n");
    }

    if (outFile == NULL) {
        printf("-------------------------------------------------------------\n");
        printf("Error: Can't open outFile.\n");
        printf("-------------------------------------------------------------\n");
        return 1;
    } else {
        printf("File Opened, Encrypting.\n");
        printf("-------------------------------------------------------------\n");

        while (1) {
            printf(".");
            if (Byte != EOF) {
                Byte = fgetc(inFile);
                newByte = Byte + 25;
                fputc(newByte ^ USER_KEY, outFile);

            } else {
             printf("\n-------------------------------------------------------------");
                printf("\nEnd of File.\n");
             printf("-------------------------------------------------------------\n");
                break;
            }
        }
        fclose(inFile);
        fclose(outFile);

        printf("-------------------------------------------------------------\n");
        printf("Your encrypted data is available at the designated path.\n");
        printf("-------------------------------------------------------------\n");
    }
}

2 个答案:

答案 0 :(得分:1)

第一个文件中的

char USER_KEY = malloc(MAX_LENGTH);错误。它必须是char *USER_KEY = (char *)malloc(MAX_LENGTH);,然后使用*USER_KEY代替USER_KEY

答案 1 :(得分:0)

您的USER_KEY应该是char字符串而不是单个char。使用char *声明它使用和引用。 您的下一个错误是检查字符串的长度,使用strlen(USER_KEY)。