openssl blowfish 관련 질문
글쓴이: kurome / 작성시간: 수, 2010/08/18 - 10:48오전
openssl 에 blowfish를 이용하여 암호화를 하고 있습니다. 먼저 evp 에 내용을 이용하여 아래의 내용으로 코딩을 하였을 경우 발생하는 값은 아래와 같이 나오는데요 실제적으로 java 나 php 에서 코드를 출력해보면 결과가 다릅니다.
이러한 결과값에 확연한 차이를 발생하는 이유가 무엇인지요? 뒤에 값만 다르다면 padding 이 원인이겠지만... 먼가 다른 이유가 있는지 알고 싶습니다.
결과값은 bin to hex 한 값 입니다.
openssl 결과 : E8 60 5A 69 4D 83 0E B7 62 C8 B8 38 77 5A 9F 63 java or php 결과 : 05f06befac8c6b8349004d1c2bbe94cd
EEVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
int olen = 0, tlen;
unsigned char buf[512] = {0};
size_t n = strlen(data);
// set padding
//EVP_CIPHER_CTX_set_padding(&ctx, 0);
EVP_EncryptInit(&ctx, EVP_bf_ecb(), (unsigned char*)_keyValue.c_str(), (unsigned char*)_iv.c_str());
if (EVP_EncryptUpdate(&ctx, buf, &olen, (unsigned char*)data, n) != 1)
{
printf("Error in encrypt update\n");
return "";
}
if (EVP_EncryptFinal(&ctx, buf+olen, &tlen) != 1)
{
printf("Error in encrypt final\n");
return "";
}
EVP_CIPHER_CTX_cleanup(&ctx);
CUtility util;
std::string returnValue;
util.Bin2Hex((const char*)buf, returnValue);
LOG2("encrypted %s\n", returnValue.c_str());Forums:


음..
iv 를 argument 로 받았는데.. iv 가 필요없는 ECB mode 를 쓰셨네요..?
key, iv, block cipher mode 가 일치하는지 확인해 보셔야 할 듯..
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
+
네 모드를 확인했구요, 함수 떼어내서 보여드리느니라구 저리 IV 가 들어간 것이구요 실제로 IV 가들어갔더라도 ECB 이므로 영향을 받지 않습니다.
key, mode 모두 일치 합니다.
동일한 값으로 openssl/blowfish.h 에 있는 함수를 이용했을 땐 제대로 값이 출력되는데 evp 로는 다른 값이 나와서 그 이유가 무엇인지 궁금하네요
음..
다시 보니 EVP_EncryptFinal() 의 argument 도 조금 이상한 듯 하네요...
그냥 EVP_EncryptFinal(&ctx, buf, &olen) 로 넘겨야 하지 않나요?
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
+
http://www.openssl.org/docs/crypto/EVP_EncryptInit.html
링크내에 하단을 보시면 다음과 같은 소스를 보실 수 있으실 것 입니다.
int do_crypt(char *outfile) { unsigned char outbuf[1024]; int outlen, tmplen; /* Bogus key and IV: we'd normally set these from * another source. */ unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; unsigned char iv[] = {1,2,3,4,5,6,7,8}; char intext[] = "Some Crypto Text"; EVP_CIPHER_CTX ctx; FILE *out; EVP_CIPHER_CTX_init(&ctx); EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv); if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) { /* Error */ return 0; } /* Buffer passed to EVP_EncryptFinal() must be after data just * encrypted to avoid overwriting it. */ if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) { /* Error */ return 0; } outlen += tmplen; EVP_CIPHER_CTX_cleanup(&ctx); /* Need binary mode for fopen because encrypted data is * binary data. Also cannot use strlen() on it because * it wont be null terminated and may contain embedded * nulls. */ out = fopen(outfile, "wb"); fwrite(outbuf, 1, outlen, out); fclose(out); return 1; }음...
그렇군요.. Final 에서 남은 데이터의 패딩 & 암호화를 처리하니..
출력 버퍼 끝에 붙여줘야 맞죠.. 잠시 착각했네요... ;;
raw bf encrypt 함수는 먹히는데, evp 에서는 결과가 다르다라.. 흠... ;;
data 가 string 이 맞다면, 위 코드에서 딱히 더 이상 드릴 말씀은 없네요..
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
댓글 달기