A very practical base64 class

Source: Internet
Author: User
Tags file url

Using system;

Namespace sclasslibrary. sencoding
{
/// <Summary>
/// Operations related to the base64 encoding algorithm
/// By Yin Shuguang
/// </Summary>
Public class sbase64
{
Public sbase64 ()
{
//
// Todo: add the constructor logic here
//
}
//--------------------------------------------------------------------------------
/// <Summary>
/// Use base64 to encrypt the string
/// </Summary>
/// <Param name = "sourcestring"> string to be encrypted </param>
/// <Param name = "ens"> system. Text. Encoding object, for example, create a Chinese sequence set object: system. Text. encoding. getencoding (54936) </param>
/// <Returns> encoded text string </returns>
Public static string encodingforstring (string sourcestring, system. Text. Encoding ENS)
{
Return convert. tobase64string (ENS. getbytes (sourcestring ));
}

/// <Summary>
/// Use base64 to encrypt the string
/// </Summary>
/// <Param name = "sourcestring"> string to be encrypted </param>
/// <Returns> encoded text string </returns>
Public static string encodingforstring (string sourcestring)
{
Return encodingforstring (sourcestring, system. Text. encoding. getencoding (54936 ));
}

/// <Summary>
/// Restore the string from a base64 encoded string. Chinese characters are supported.
/// </Summary>
/// <Param name = "base64string"> base64-encrypted string </param>
/// <Param name = "ens"> system. Text. Encoding object, for example, create a Chinese sequence set object: system. Text. encoding. getencoding (54936) </param>
/// <Returns> restored text string </returns>
Public static string decodingforstring (string base64string, system. Text. Encoding ENS)
{
/**
**************************************** ********************
*
* The Bytes obtained from base64string are characters in the internal machine code (ANSI character encoding)
* Generally, the formula for converting an inner code to a Chinese character is:
* (Char) (first byte binary value * 256 + second byte value)
* Because the char or string in C # adopts unicode encoding, it cannot be calculated according to the above formula.
* ANSI Byte encoding is incompatible with Unicode encoding.
* Therefore, use the encoding class provided by the. NET class library to convert ANSI code to Unicode code.
*
* The getencoding method relies on the basic platform to support most code pages. However, system support is provided for: default encoding, that is, the encoding specified in the locale settings of the computer on which this method is executed; little-Endian Unicode (UTF-16LE ); big-Endian Unicode (UTF-16BE); Windows operating system (Windows-1252); UTF-7; UTF-8; ASCII and gb18030 (Simplified Chinese ).
*
* Specify a name listed in the following table to obtain the encoding supported by the system with the corresponding code page.
*
* Code Page name
* 1200 UTF-16LE, UTF-16, ucs-2, Unicode, or ISO-10646-UCS-2"
* 1201 "UTF-16BE" or "unicodefffe"
* 1252 "Windows-1252"
* 65000 utf-7, csunicode11utf7, unicode-1-1-utf-7, unicode-2-0-utf-7, x-unicode-1-1-utf-7 or x-unicode-2-0-utf-7"
* 65001 "UTF-8", "unicode-1-1-utf-8", "unicode-2-0-utf-8", "x-unicode-1-1-utf-8" or "x-unicode-2-0-utf-8"
* 20127 "US-ASCII", "us", "ASCII", "ANSI_X3.4-1968", "ANSI_X3.4-1986", "cp367", "csascii", "ibm367", "iso-ir-6 ", "ISO646-US" or "iso_646.irv: 1991"
* 54936 "gb18030"
*
* Some platforms may not support specific code pages. For example, the Japanese shift-JIS code page (code page 932) may not be supported in Windows 98 in the US ). In this case, the getencoding method will trigger notsupportedexception when executing the following C # code:
*
* Encoding ENC = encoding. getencoding ("shift-JIS ");
*
**************************************** ***********************/
// Obtain the original character from base64string
Return ens. getstring (convert. frombase64string (base64string ));
}

/// <Summary>
/// Restore the string from a base64 encoded string. Chinese characters are supported.
/// </Summary>
/// <Param name = "base64string"> base64-encrypted string </param>
/// <Returns> restored text string </returns>
Public static string decodingforstring (string base64string)
{
Return decodingforstring (base64string, system. Text. encoding. getencoding (54936 ));
}

// Configure //--------------------------------------------------------------------------------------

/// <Summary>
/// Add base64 to any type of file
/// </Summary>
/// <Param name = "FILENAME"> file path and file name </param>
/// <Returns> base64-encoded string of the object </returns>
Public static string encodingforfile (string filename)
{
System. Io. filestream FS = system. Io. file. openread (filename );
System. Io. binaryreader BR = new system. Io. binaryreader (FS );

/* System. byte [] B = new system. byte [fs. Length];
FS. Read (B, 0, convert. toint32 (FS. Length ));*/

String base64string = convert. tobase64string (Br. readbytes (INT) fs. Length ));

BR. Close ();
FS. Close ();
Return base64string;
}

/// <Summary>
/// Save base64 encoded strings as files
/// </Summary>
/// <Param name = "base64string"> base64-encoded string </param>
/// <Param name = "FILENAME"> Save the file path and file name </param>
/// <Returns> whether the file is successfully saved </returns>
Public static bool savedecodingtofile (string base64string, string filename)
{
System. Io. filestream FS = new system. Io. filestream (filename, system. Io. filemode. Create );
System. Io. binarywriter BW = new system. Io. binarywriter (FS );
Bw. Write (convert. frombase64string (base64string ));
Bw. Close ();
FS. Close ();
Return true;
}

//-------------------------------------------------------------------------------

/// <Summary>
/// Obtain the file from network address 1 and convert it to base64 encoding
/// </Summary>
/// <Param name = "url"> File URL, an absolute URL </param>
/// <Param name = "objwebclient"> system. net. WebClient object </param>
/// <Returns> </returns>
Public static string encodingfilefromurl (string URL, system. net. WebClient objwebclient)
{
Return convert. tobase64string (objwebclient. downloaddata (URL ));
}

/// <Summary>
/// Obtain the file from network address 1 and convert it to base64 encoding
/// </Summary>
/// <Param name = "url"> File URL, an absolute URL </param>
/// <Returns> base64 string after the file is converted </returns>
Public static string encodingfilefromurl (string URL)
{
// System. net. WebClient mywebclient = new system. net. WebClient ();
Return encodingfilefromurl (URL, new system. net. WebClient ());
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.