blowfish ¾Ë°í¸®ÁòÀ» ÀÌ¿ëÇÑ µ¥ÀÌÅÍ ¾Ïȣȭ
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®
ÇöÀçÀ§Ä¡ : Code>C>blowcrypt



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

  1. blowcrypt
  2. blowfish ¾Ë°í¸®ÁòÀ» ÀÌ¿ëÇÑ µ¥ÀÌÅÍ ¾Ïȣȭ
  3. yundream
  4. Version 0.2
  5. 2004/03/30


¼³¸í

openssh¿¡¼­ Á¦°øÇÏ´Â blowfish ¾Ë°í¸®ÁòÀ» ÀÌ¿ëÇØ¼­ µ¥ÀÌÅ͸¦ ¾Ïȣȭ ÇÕ´Ï´Ù. blowfish´Â µ¥ÀÌÅÍÀÇ ¾Ïȣȭ¿Í º¹È£È­¸¦ À§Çؼ­ µ¿ÀÏÇÑ Å°¸¦ »ç¿ëÇÏ´Â ´ëĪ ¾Ë°í¸®ÁòÀ» »ç¿ëÇϰí ÀÖ½À´Ï´Ù.

¾Ïȣȭ¿Í º¹È£È­¸¦ À§ÇØ ´Ù¸¥Å°¸¦ »ç¿ëÇÏ´Â RSA¾Ë°í¸®Áò¿¡ ºñÇØ¼­ »ç¿ëÇϱⰡ °£´ÜÇϰí, ´õ ºü¸¥ ¼öÇà´É·ÂÀ» º¸¿©Áشٴ ÀåÁ¡À» °¡Áý´Ï´Ù.

»ç¿ë¹æ¹ý

int keygen(struct mykey *key); 
int cryptinit(); 
int encrypt(unsigned char *data, size_t n,unsigned char *buf, struct mykey *lkey) 
int decrypt(char *cryptmessage, unsigned char *outbuf, struct mykey *lkey, size_t n); 
 
  • keygenÀº ¾Ï/º¹È£È­¿¡ »ç¿ëµÉ ۸¦ »ý¼ºÇÑ´Ù. ¸¸µé¾îÁø Ű´Â key¿¡ º¹»çµÈ´Ù. key´Â ´ÙÀ½°ú °°ÀÌ Á¤ÀǵǾîÀÖ´Ù.

    struct mykey 
    { 
        unsigned char key[16]; 
        unsigned char vec[8]; 
    }; 
     
  • cryptinit´Â chiper¸¦ ÃʱâÈ­ ½ÃŲ´Ù. chiper´Â Àü¿ªÀ¸·Î ¼±¾ðµÇ¾î ÀÖ´Ù.
  • encrypt´Â ÁÖ¾îÁø data¿¡¼­ n ¹ÙÀÌÆ®¸¸Å­À» ¾Ïȣȭ ÇØ¼­ buf¿¡ ÀúÀåÇÑ´Ù. ¾Ïȣȭ´Â lkey¸¦ ÅëÇØ¼­ ÀÌ·ç¾îÁø´Ù. ¾ÏȣȭµÈ µ¥ÀÌÅÍ Å©±â°¡ ¸®ÅϵȴÙ.
  • decrypt´Â ¾ÏȣȭµÈ µ¥ÀÌÅÍ cryptmessage¿¡¼­ n ¹ÙÀÌÆ®¸¸Å­À» lkey¸¦ ÀÌ¿ëÇØ¼­ º¹È£È­ ÇÑ´Ù.

ÄÚµå

#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <fcntl.h> 
#include <string.h> 
#include <time.h> 
 
#include <openssl/ssl.h> 
#include <openssl/evp.h> 
#include <openssl/crypto.h> 
 
struct mykey 
{ 
    unsigned char key[16]; 
    unsigned char vec[8]; 
}; 
 
// chipteer ÃʱâÈ­  
EVP_CIPHER_CTX ctx; 
 
// ·£´ý°ªÀ» ¾ò´Â´Ù.  
// Çʿ信 µû¶ó¼­ /dev/randomÀ» »ç¿ëÇØµµ µÈ´Ù. 
int getrandom(char *rand, size_t size) 
{ 
    struct timeval tp; 
    int i = 0; 
    int ran; 
 
    gettimeofday(&tp, NULL); 
    srandom(tp.tv_sec | tp.tv_usec); 
 
    while(1) 
    { 
        if (!(size / (4*(i+1)))) break; 
 
        ran = random(); 
        memcpy(rand+(i*4), (void *)&ran, 4); 
        //memcpy(rand, (void *)&ran, 4); 
        i++; 
    } 
} 
 
// Ű »ý¼º±â 
int keygen(struct mykey *lkey) 
{ 
    int i = 0; 
    char keyseed[24]; 
    getrandom(keyseed, 24); 
    memcpy((void *)lkey->key, keyseed,   16); 
    memcpy((void *)lkey->vec, keyseed+16, 8); 
} 
 
// chiper¸¦ ÃʱâÈ­ ÇÑ´Ù. 
int cryptinit() 
{ 
    EVP_CIPHER_CTX_init(&ctx); 
} 
 
// data¸¦ lkey¸¦ ÀÌ¿ëÇØ¼­ ¾Ïȣȭ ÇÑ´Ù. 
int encrypt(unsigned char *data, size_t n, unsigned char *buf, struct mykey *lkey) 
{ 
    int olen = 0, tlen, i; 
 
    char test[256] = {0x00,}; 
    printf("msg size is %d\n", n); 
    EVP_EncryptInit(&ctx, EVP_bf_cbc(), lkey->key, lkey->vec); 
    if (EVP_EncryptUpdate(&ctx, buf, &olen, data, n+4) != 1) 
    { 
        printf("Error in encrypt update\n"); 
        return -1; 
    } 
 
 
    if (EVP_EncryptFinal(&ctx, buf+olen, &tlen) != 1) 
    { 
        printf("Error in encrypt final\n"); 
        return -1; 
    } 
    printf("Encrypt size=%d, %d\n", olen, tlen); 
    EVP_CIPHER_CTX_cleanup(&ctx); 
    return olen+tlen; 
} 
 
// ÁÖ¾îÁø ¾ÏȣȭµÈ µ¥ÀÌÅ͸¦ lkey¸¦ ÀÌ¿ëÇØ¼­ º¹È£È­ ÇÑ´Ù. 
int decrypt(char *cryptmessage, unsigned char *outbuf, struct mykey *lkey, size_t n) 
{ 
    int olen, tlen; 
 
    EVP_DecryptInit(&ctx, EVP_bf_cbc(), lkey->key, lkey->vec); 
    if(EVP_DecryptUpdate(&ctx, outbuf, &olen, (unsigned char *)cryptmessage, n) != 1) 
    { 
        printf("error in decrypt \n"); 
        return -1; 
    } 
 
    if(EVP_DecryptFinal(&ctx, outbuf+olen, &tlen) != 1) 
    { 
        printf("error in decrypt final\n"); 
        return -1; 
    } 
    return olen+tlen; 
} 
 
int main() 
{ 
    int i; 
    struct mykey lkey; 
    unsigned char buf[256] = {0x00,}; 
    unsigned char buf2[256] = {0x00,}; 
    unsigned char hello[256] = {0x00,}; 
    unsigned char hello2[256] = {0x00,}; 
    int n; 
 
    strcpy((char *)hello, "hellow world test test test\n"); 
    strcpy((char *)hello2, "¾È³çÇϼ¼¿ä ¼¼»óµé¾Æ..\n"); 
    keygen(&lkey); 
    cryptinit(); 
 
    n = encrypt(hello, strlen((char *)hello), buf, &lkey); 
    decrypt((char *)buf, buf2, &lkey, n); 
    printf("%s\n",  buf2); 
    printf("===================="); 
 
    n = encrypt(hello2, strlen((char *)hello2), buf, &lkey); 
    decrypt((char *)buf, buf2, &lkey, n); 
    printf("%s\n",  buf2); 
} 
 

´ÙÀ½°ú °°ÀÌ ÄÄÆÄÀÏ ÇÑ´Ù.
# gcc -o blowfish blowfish.c -I/usr/local/ssl/include -ldl -lcrypto 
 

Âü°í¹®Çå

  1. OpensslÀ» ÀÌ¿ëÇÑ ÆÄÀÏ ¾Ïȣȭ
  2. randomÀ» ÀÌ¿ëÇÑ random°ª »ý¼º
  3. /dev/randomÀ» ÀÌ¿ëÇÑ random°ª »ý¼º

º¯°æ»çÇ×


2004/03/30

EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.