본문 바로가기
IT-개발,DB

[ASP.NET] Base64 인코드 디코드(C#)

by SB리치퍼슨 2010. 9. 16.

출처 : 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);
    }
}

 
반응형

댓글