JinnyLyn / BUFS_S-BitUnited_Worm

부산외대 S-Bit United 동아리 Worm조 활동
1 stars 1 forks source link

There might be issues when converting the key to binary. #4

Open donggeunlee96 opened 4 days ago

donggeunlee96 commented 4 days ago

projects/crypto/cryptov2.c

Of course, in the case of 'mykey', it is an array, but I have questions about whether the input object string is also in array format.

example code

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

char mykey[14] = "aabbAABB";
char *char_mykey = NULL;
int *int_mykey = NULL;

// char을 이진수로 변환하는 함수 작성
void char2binary(char c, char *binary_char) {
    for (int i = 7; i >= 0; --i) {
        binary_char[7 - i] = (c & (1 << i)) ? '1' : '0';
    }
    binary_char[8] = '\0';  // 문자열 끝에 NULL 추가
}

void string2binary(const char *s, char *binary) {
    int len = strlen(s);
    char binary_char[9];
    for (int i = 0; i < len; ++i) {
        char2binary(s[i], binary_char);
        strcat(binary, binary_char);
        if (i < len - 1) {
            strcat(binary, " ");
        }
    }
}

int main(void) {
    // char_mykey에 메모리 할당 (mykey의 각 문자에 대해 9비트 * 14 + 공백)
    char_mykey = (char *)malloc(9 * strlen(mykey) + strlen(mykey));  
    if (char_mykey == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }
    char_mykey[0] = '\0';  // 초기화를 위해 첫 글자에 NULL 값 설정

    // 이진 변환
    string2binary(mykey, char_mykey);

    // 출력
    printf("Binary representation: %s\n", char_mykey);

    // 메모리 해제
    free(char_mykey);

    return 0;
}

image

So I recommend adding an array to the object string to make it the same as 'mykey' and adding a condition to limit the character count.