AES암호화에 대해 질문 드릴께요..
글쓴이: hypnosis / 작성시간: 월, 2013/01/28 - 4:11오후
#include "openssl/aes.h" #define IVSIZE 8 typedef struct { unsigned char iVec[AES_BLOCK_SIZE]; unsigned int num; unsigned char ecount[AES_BLOCK_SIZE]; }ctr_state; int AES_InitCTR(ctr_state *state, const unsigned char iv[IVSIZE]); int AES_Encrypt(char *_plainFile, char *_cipherFile, char *_key, char *_iv); int AES_Decrypt(char *_cipherFile, char *_plainFile, char *_iv); void writeAESKey(char* _f_hash); int AES_Encrypt(char *_plainFile, char *_cipherFile, char *_hashValue, char *_iv) { AES_KEY encKey; //AES구조체 FILE *in_File = NULL; //암호화 할 파일에 대한 파일포인터 FILE *out_File = NULL; //암호화 되어 출력 될 파일포인터 int readLen = 0; //파일의 read 길이 int writeLen = 0; //파일의 write 길이 unsigned char readBuf[AES_BLOCK_SIZE]; //read 할 버퍼 unsigned char outBuf[AES_BLOCK_SIZE]; //출력 될 버퍼 ctr_state state; writeAESKey(_hashValue); strcpy(_cipherFile,_plainFile); _cipherFile[strlen(_cipherFile)] = 'a'; _cipherFile[strlen(_cipherFile)+1] = '\0'; FILE *AES_FILE; char uKey[BUFSIZE]={0,}; char *ckey; unsigned int nkey; int i = 1; AES_FILE = fopen("AES_KEY.txt", "rb"); //encKey = Read_AesKey(_hashValue); fread(uKey, sizeof(char), BUFSIZE, AES_FILE); ckey = strtok(uKey, "@"); nkey = strtoul(ckey,NULL,10); //nkey = atoi(ckey); encKey.rd_key[0] = nkey; while(ckey = strtok(NULL, "@")){ if(ckey == NULL){ ckey = NULL; } else{ if(strcmp(ckey, "10")){ nkey = strtoul(ckey,NULL,10); encKey.rd_key[i++] = nkey; } else{ nkey = strtoul(ckey,NULL,10); encKey.rounds = nkey; } } } in_File = fopen(_plainFile, "rb"); if(in_File != NULL) { out_File = fopen (_cipherFile, "wb"); while(1) { AES_InitCTR(&state, (unsigned char*)_iv); readLen = fread(readBuf, sizeof(char), AES_BLOCK_SIZE, in_File); AES_ctr128_encrypt(readBuf, outBuf, readLen, &encKey, state.iVec, state.ecount, &state.num); writeLen = fwrite(outBuf, sizeof(char), readLen, out_File); if(writeLen < AES_BLOCK_SIZE) { break; } } } fclose(AES_FILE); fclose(in_File); fclose(out_File); return 0; } int AES_Decrypt(char *_cipherFile, char *_plainFile, char *_iv) { AES_KEY encKey; //AES구조체 FILE *in_File = NULL; //암호화 할 파일에 대한 파일포인터 FILE *out_File = NULL; //암호화 되어 출력 될 파일포인터 int readLen = 0; //파일의 read 길이 int writeLen = 0; //파일의 write 길이 unsigned char readBuf[AES_BLOCK_SIZE]; //read 할 버퍼 unsigned char outBuf[AES_BLOCK_SIZE]; //출력 될 버퍼 ctr_state state; strcpy(_plainFile,_cipherFile); _plainFile[strlen(_plainFile)-1] = '\0'; printf("확인용1"); FILE *AES_FILE; char uKey[1024] = {0,}; char *ckey; unsigned int nkey; int i = 1; AES_FILE = fopen("AES_KEY.txt", "rb"); //if(AES_FILE == NULL) return 0; printf("확인용1"); fread(uKey, sizeof(char), 1024, AES_FILE); printf("확인용2"); ckey = strtok(uKey, "@"); nkey = ntohl(strtoul(ckey, NULL, 10));//nkey = atoi(ckey);//_atoi64(ckey); encKey.rd_key[0] = nkey; printf("확인용3"); while(ckey = strtok(NULL, "@")){ printf("확인용4"); if(ckey == NULL/* || context == NULL*/){ printf("확인용5"); ckey = NULL; //context = NULL; } else{ if(strcmp(ckey, "10")){ nkey = ntohl(strtoul(ckey, NULL, 10));//nkey = atoi(ckey);// _atoi64(ckey); encKey.rd_key[i++] = nkey; } else{ nkey = strtoul(ckey, NULL, 10);//nkey = atoi(ckey);//_atoi64(ckey); encKey.rounds = nkey; } } } printf("확인용6"); in_File = fopen(_cipherFile, "rb"); if(in_File != NULL) { out_File = fopen(_plainFile, "wb"); while(1) { AES_InitCTR(&state, (unsigned char*)_iv); readLen = fread(readBuf, sizeof(char), AES_BLOCK_SIZE, in_File); AES_ctr128_encrypt(readBuf, outBuf, readLen, &encKey, state.iVec, state.ecount, &state.num); writeLen = fwrite(outBuf, sizeof(char), readLen, out_File); if(writeLen < AES_BLOCK_SIZE) { break; } } } printf("확인용7"); fclose(AES_FILE); fclose(in_File); fclose(out_File); return 0; } int AES_InitCTR(ctr_state *state, const unsigned char iv[IVSIZE]) { state->num = 0; memset(state->ecount, 0, AES_BLOCK_SIZE); memset(state->iVec + IVSIZE, 0, IVSIZE); memcpy(state->iVec, iv, IVSIZE); return 0; } void writeAESKey(char* _f_hash){ AES_KEY encKey; FILE *fp; int i; fp = fopen("AES_KEY.txt", "wb"); AES_set_encrypt_key((unsigned char*)_f_hash, 128, &encKey); for(i = 0; i < 60; i++) { fprintf(fp, "%u%s", encKey.rd_key[i], "@"); } fprintf(fp, "%d", encKey.rounds); fclose(fp); }
AES 암호화 하는데 지금 리눅스에서 암호화 한 파일을 윈도우에서 복호화 할려고 하는데요,,
윈도우에서 복호화 되지 않습니다..ㅠㅠ
리눅스와 윈도우에서 똑같은 파일을 위 코드를 써서 암호화 해봤는데 결과값이 다르더군여..
왜 그런지 아시는분 도와주세요ㅠ ㅠ
Forums:
혹시 64비트에서 컴파일 하나요? aes 소스에서
혹시 64비트에서 컴파일 하나요?
aes 소스에서 long 을 쓴 부분이 있어서 64비트 리눅스에서 컴파일할 경우
관련 부분을 수정해 줘야 했던걸로 기억합니다.
======== 서명 =======
주거지는 www.indidev.net 입니다.
아니요
VMware에서 리눅스 서버 를 돌리고 있는데요, 리눅스도 32비트 인걸 확인했구요,
윈도우도 xp service pack3 32비트 인걸 확인했는데 암/복호화가 안되네요 ㅠㅠ
댓글 달기