본문으로 바로가기

MinGW에서.. crypt 라이브러리를 뭐로.. 쓸까.. 이리저리 기웃거리다..
LibTomCrypt를 사용하기로 결정하였음. 다른 라이브러리를 사용하여..
암호화된 파일을 LibTomCrypt를 사용하여 복호화를 해봄.
코드를 보면.. base64 디코드를 한 후 복호화를 하는데..
AES로 암호화후 base64로 인코딩을 해놓은 데이터라 그런 것임.

 

참고로 LibTomCrypt는 msys2에서 바로 설치 가능.

pacman -S mingw-w64-x86_64-libtomcrypt


아래의 코드를 만들기 위해 참고한 코드: modes_test.c

#include <tomcrypt.h>
  
int errno;
 
static void *
read_file (const char *fname, size_t *r_length)
{
    FILE *fp;
    struct stat st;
    char *buf;
    size_t buflen;
 
    fp = fopen (fname, "rb");
    if (!fp)
    {
        printf ("can't open `%s': %s\n", fname, strerror (errno));
        return NULL;
    }
 
    if (fstat (fileno(fp), &st))
    {
        printf ("can't stat `%s': %s\n", fname, strerror (errno));
        fclose (fp);
        return NULL;
    }
 
    buflen = st.st_size;
    buf = malloc (buflen+1);
    if (fread (buf, buflen, 1, fp) != 1)
    {
        printf ("error reading `%s': %s\n", fname, strerror (errno));
        fclose (fp);
        free (buf);
        return NULL;
    }
    fclose (fp);
 
    if (r_length)
        *r_length = buflen;
    return buf;
}
 
int main(void)
{
    unsigned char *base64dec;
    unsigned long base64dec_len;
    {
        size_t file_length;
        unsigned char *base64enc = read_file("a.b64", &file_length);
        base64dec = malloc(file_length);
        base64dec_len = file_length;
        base64_decode(base64enc, file_length, base64dec, &base64dec_len);
    }
 
    {
        register_cipher(&aes_desc);
 
        unsigned char key[16], iv[16];
        symmetric_CFB cfb;
        int cipher_idx;
 
        cipher_idx = find_cipher("aes");
        if (cipher_idx == -1) {
            printf("Invalid cipher entered on command line.\n");
            exit(-1);
        }
 
        sprintf(iv, "%s", "1234567812345678");
        sprintf(key, "%s", "1234567890123456");
 
        char *decrypt = malloc(base64dec_len+1);
        cfb_start(cipher_idx, iv, key, 16, 0, &cfb);
        cfb_setiv(iv, 16, &cfb);
        cfb_decrypt(base64dec, decrypt, base64dec_len, &cfb);
 
        fprintf(stdout, "%s\n", decrypt);
        fflush(stdout);
 
        free(decrypt);
    }
 
    return 0;
}