천객만래 [千客萬來] (It has an interminable succession of visitors)


/////////////////////////////////////////////////////////////
// base64 인코딩/디코딩 및 제거
// base64.h
K4LIB_BASIC_API unsigned char* base64_encode(const unsigned char *string, int length, int *ret_length);
K4LIB_BASIC_API unsigned char* base64_decode(const unsigned char *string, int length, int *ret_length);
K4LIB_BASIC_API void base64_free(unsigned char* data);

 


/////////////////////////////////////////////////////////////
// base64 인코딩/디코딩 및 제거
// base64.cpp
static char base64_table[] =
{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
};
static char base64_pad = '=';

unsigned char *base64_encode(const unsigned char *string, int length, int *ret_length)
{
    const unsigned char *current = string;
    int i    = 0;
    int    wi    =    0;
    int        nAllocSize    =    ((length + 3 - length
  % 3) * 4 / 3 + 1) * sizeof(char);
    nAllocSize    +=    (nAllocSize/80+1)*2;
    unsigned char *result = new unsigned char[nAllocSize];
 
    while (length > 2)
    { /* keep going until we have less than 24 bits */
        result[i++] = base64_table[current[0] >> 2];
        result[i++] = base64_table[((current[0] & 0x03) <<  4)
   + (current[1] >> 4)];
        result[i++] = base64_table[((current[1] & 0x0f) <<  2)
   + (current[2] >> 6)];
        result[i++] = base64_table[current[2] & 0x3f];
 
        current += 3;
        length -= 3; /* we just handle 3 octets of data */
        wi+=4;
 
/*        if(wi%80 == 0)
        {
            result[i++] = '\r';
            result[i++] = '\n';
        }
*/    }
 
    /* now deal with the tail end of things */
    if (length != 0)
    {
        result[i++] = base64_table[current[0] >> 2];
        if (length > 1)
        {
            result[i++] = base64_table[((current[0] &
    0x03) <<  4) + (current[1] >> 4)];
            result[i++] = base64_table[(current[1] &
    0x0f) <<  2];
            result[i++] = base64_pad;
        }else
        {
            result[i++] = base64_table[(current[0] &
    0x03) <<  4];
            result[i++] = base64_pad;
            result[i++] = base64_pad;
        }
    }
    if(ret_length)
    {
        *ret_length = i;
    }
    result[i] = '\0';
    return result;
}

/* as above, but backwards. :) */
unsigned char *base64_decode(const unsigned char *string, int length, int *ret_length)
{
    const unsigned char *current = string;
    int ch, i = 0, j = 0, k;
    char *chp;
 
    unsigned char *result = new unsigned char[(length / 4 *3 + 1) * sizeof(char)];
    if (result == NULL) {
        return NULL;
    }
 
    /* run through the whole string, converting as we go */
    while ((ch = *current++) != '\0') {
        if (ch == base64_pad) break;
        chp = strchr(base64_table, ch);
        if (chp == NULL) continue;
        ch = chp - base64_table;
 
        switch(i % 4) {
        case 0:
            result[j] = (unsigned char)(ch <<  2);
            break;
        case 1:
            result[j++] |= ch >> 4;
            result[j] = (unsigned char)((ch & 0x0f) <<  4);
            break;
        case 2:
            result[j++] |= ch >>2;
            result[j] = (unsigned char)((ch & 0x03) <<  6);
            break;
        case 3:
            result[j++] |= ch;
            break;
        }
        i++;
    }
 
    k = j;
    /* mop things up if we ended on a boundary */
    if (ch == base64_pad) {
        switch(i % 4) {
        case 0:
        case 1:
            delete [] result;
            return NULL;
        case 2:
            k++;
        case 3:
            result[k++] = 0;
        }
    }
    if(ret_length) {
        *ret_length = j;
    }
    result[k] = '\0';
    return result;
}

void base64_free(unsigned char* data)
{
 delete[] data;
}

Posted by SB패밀리

protected string strOrig = "test your coding";
  protected string strResult = "";
  protected string reOrig = "";
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   //암호화

   byte[] byteOrig = new byte[strOrig.Length];
   for (int i = 0; i < strOrig.Length; i++)
   {
    byteOrig[i]= Convert.ToByte(Convert.ToChar(strOrig.Substring(i,1)));
   }
   this.strResult = Convert.ToBase64String(byteOrig);//결과

  //해독

   this.reOrig = System.Text.Encoding.Default.GetString(System.Convert.FromBase64String(strResult));

  }

Posted by SB패밀리

[자바스크립트]Base64 Endode, Decode

  

function encode_base64( what )
{
      var base64_encodetable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      var result = "";
      var len = what.length;
      var x, y;
      var ptr = 0;

      while( len-- > 0 )
      {
            x = what.charCodeAt( ptr++ );
            result += base64_encodetable.charAt( ( x >> 2 ) & 63 );

            if( len-- <= 0 )
            {
                  result += base64_encodetable.charAt( ( x << 4 ) & 63 );
                  result += "==";
                  break;
            }

            y = what.charCodeAt( ptr++ );
            result += base64_encodetable.charAt( ( ( x << 4 ) | ( ( y >> 4 ) & 15 ) ) & 63 );

            if ( len-- <= 0 )
            {
                  result += base64_encodetable.charAt( ( y << 2 ) & 63 );
                  result += "=";
                  break;
            }

            x = what.charCodeAt( ptr++ );
            result += base64_encodetable.charAt( ( ( y << 2 ) | ( ( x >> 6 ) & 3 ) ) & 63 );
            result += base64_encodetable.charAt( x & 63 );

      }

      return result;
}


function decode_base64( what )
{
      var base64_decodetable = new Array (
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
            52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255, 255, 255, 255, 255,
            255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,  11,  12,  13,  14,
            15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
            255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
            41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51, 255, 255, 255, 255, 255
      );
      var result = "";
      var len = what.length;
      var x, y;
      var ptr = 0;

      while( !isNaN( x = what.charCodeAt( ptr++ ) ) )
      {
            if( x == 13 || x == 10 )
                  continue;

            if( ( x > 127 ) || (( x = base64_decodetable[x] ) == 255) )
                  return false;
            if( ( isNaN( y = what.charCodeAt( ptr++ ) ) ) || (( y = base64_decodetable[y] ) == 255) )
                  return false;

            result += String.fromCharCode( (x << 2) | (y >> 4) );

            if( (x = what.charCodeAt( ptr++ )) == 61 )
            {
                  if( (what.charCodeAt( ptr++ ) != 61) || (!isNaN(what.charCodeAt( ptr ) ) ) )
                        return false;
            }
            else
            {
                  if( ( x > 127 ) || (( x = base64_decodetable[x] ) == 255) )
                        return false;
                  result += String.fromCharCode( (y << 4) | (x >> 2) );
                  if( (y = what.charCodeAt( ptr++ )) == 61 )
                  {
                        if( !isNaN(what.charCodeAt( ptr ) ) )
                              return false;
                  }
                  else
                  {
                        if( (y > 127) || ((y = base64_decodetable[y]) == 255) )
                              return false;
                        result += String.fromCharCode( (x << 6) | y );
                  }
            }
      }
      return result;
}

Posted by SB패밀리

출처 : http://www.hoonsbara.com/hoonsboard.aspx?table_name=asptip&board_idx=447451&page=4&keyword=&search=&boardmode=2

 protected string Base64Encode(string str)
{
  return Convert.ToBase64String(
    System.Text.Encoding.GetEncoding("euc-kr").GetBytes(str)); 
}

protected string Base64Decode(string str)
{
  return System.Text.Encoding.GetEncoding("euc-kr").GetString(
    System.Convert.FromBase64String(str));
}

void Page_Load(Object src, EventArgs e )
{
  string str="인코딩할 한글 1234567890!@#$%^&*()";
  Response.Write( "인코딩 전 : " + str +"<br>" );
  string encoded = Base64Encode(str);
  Response.Write( "BASE64 인코드 : " + encoded +"<br>");
  string decoded = Base64Decode(encoded);
  Response.Write( "BASE64 디코드 : " + decoded +"<br>");
}

===================================================================================================

 

출처 : MSDN 2003

 

public void EncodeWithString() {
    System.IO.FileStream inFile;    
    byte[]                 binaryData;

    try {
        inFile = new System.IO.FileStream(inputFileName,
                                          System.IO.FileMode.Open,
                                          System.IO.FileAccess.Read);
        binaryData = new Byte[inFile.Length];
        long bytesRead = inFile.Read(binaryData, 0,
                                    (int)inFile.Length);
        inFile.Close();
    }
    catch (System.Exception exp) {
        // Error creating stream or reading from it.
        System.Console.WriteLine("{0}", exp.Message);
        return;
    }

    // Convert the binary input into Base64 UUEncoded output.
    string base64String;
    try {
         base64String =
            System.Convert.ToBase64String(binaryData,
                                          0,
                                          binaryData.Length);
    }
    catch (System.ArgumentNullException) {
        System.Console.WriteLine("Binary data array is null.");
        return;
    }

    // Write the UUEncoded version to the output file.
    System.IO.StreamWriter outFile;
    try {
        outFile = new System.IO.StreamWriter(outputFileName,
                                    false,
                                    System.Text.Encoding.ASCII);            
        outFile.Write(base64String);
        outFile.Close();
    }
    catch (System.Exception exp) {
        // Error creating stream or writing to it.
        System.Console.WriteLine("{0}", exp.Message);
    }
}

 
Posted by SB패밀리