AES암호화에 대해 질문 드릴께요..

hypnosis의 이미지

#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 암호화 하는데 지금 리눅스에서 암호화 한 파일을 윈도우에서 복호화 할려고 하는데요,,
윈도우에서 복호화 되지 않습니다..ㅠㅠ
리눅스와 윈도우에서 똑같은 파일을 위 코드를 써서 암호화 해봤는데 결과값이 다르더군여..
왜 그런지 아시는분 도와주세요ㅠ ㅠ
kippler의 이미지

혹시 64비트에서 컴파일 하나요?

aes 소스에서 long 을 쓴 부분이 있어서 64비트 리눅스에서 컴파일할 경우

관련 부분을 수정해 줘야 했던걸로 기억합니다.

======== 서명 =======
주거지는 www.indidev.net 입니다.

hypnosis의 이미지

VMware에서 리눅스 서버 를 돌리고 있는데요, 리눅스도 32비트 인걸 확인했구요,

윈도우도 xp service pack3 32비트 인걸 확인했는데 암/복호화가 안되네요 ㅠㅠ

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.