openssl blowfish 관련 질문

kurome의 이미지

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());
ymir의 이미지

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 』

kurome의 이미지

네 모드를 확인했구요, 함수 떼어내서 보여드리느니라구 저리 IV 가 들어간 것이구요 실제로 IV 가들어갔더라도 ECB 이므로 영향을 받지 않습니다.

key, mode 모두 일치 합니다.

동일한 값으로 openssl/blowfish.h 에 있는 함수를 이용했을 땐 제대로 값이 출력되는데 evp 로는 다른 값이 나와서 그 이유가 무엇인지 궁금하네요

ymir의 이미지

다시 보니 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 』

kurome의 이미지

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;
        }
ymir의 이미지

그렇군요.. 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 』

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.