A very practical Base64 class

Source: Internet
Author: User
Tags base64 file url save file
Using System;

Namespace Sclasslibrary.sencoding
{
<summary>
Related operations on Base64 coding algorithms
Author: Ying Guang
</summary>
public class SBase64
{
Public SBase64 ()
{
//
TODO: Add constructor logic here
//
}
//--------------------------------------------------------------------------------
<summary>
Encrypt a string using the Base64 algorithm
</summary>
<param name= "sourcestring" > Strings to be encrypted </param>
<param name= "Ens" >system.text.encoding object, such as creating a Chinese encoding Set object: System.Text.Encoding.GetEncoding (54936) </param >
<returns> text string after the overweight </returns>
public static string Encodingforstring (String sourcestring,system.text.encoding ens)
{
Return convert.tobase64string (Ens. GetBytes (sourcestring));
}

<summary>
Encrypt a string using the Base64 algorithm
</summary>
<param name= "sourcestring" > Strings to be encrypted </param>
<returns> text string after the overweight </returns>
public static string Encodingforstring (String sourcestring)
{
Return encodingforstring (sourcestring,system.text.encoding.getencoding (54936));
}

<summary>
To restore a string from a base64 encoded string, support Chinese
</summary>
<param name= "base64string" >base64 encrypted string </param>
<param name= "Ens" >system.text.encoding object, such as creating a Chinese encoding Set object: System.Text.Encoding.GetEncoding (54936) </param >
<returns> text string after restore </returns>
public static string Decodingforstring (String base64string,system.text.encoding ens)
{
/**
* ***********************************************************
*
* Internal code (ANSI character encoding) of bytes obtained from base64string
* Generally, the conversion of the machine code to Chinese characters is the formula:
* (char) (the first byte of the binary value *256+ the second byte value)
* and char or string in C # because of Unicode encoding, it is not possible to follow the formula above
* ANSI Byte and Unicode encoding incompatible
* Using the encoding class provided by the. NET class library to convert from ANSI to Unicode code
*
* The GetEncoding method relies on most of the code pages supported by the underlying platform. However, system support is provided for the following conditions: The default encoding, the encoding specified in the locale of the computer executing this method, Little-endian Unicode (utf-16le), Big-endian Unicode (UTF-16BE), and Windows Operating system (windows-1252); Utf-7;utf-8;ascii and GB18030 (Simplified Chinese).
*
* Specify one of the names listed in the following table to obtain a system-supported encoding 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 American version of Windows 98 may not support the Japanese Shift-jis code page (code page 932). In this case, the GetEncoding method will raise the NotSupportedException when executing the following C # code:
*
* Encoding enc = encoding.getencoding ("Shift-jis");
*
* **************************************************************/
Get the original character from the Base64string
Return ENS. GetString (convert.frombase64string (base64string));
}

<summary>
To restore a string from a base64 encoded string, support Chinese
</summary>
<param name= "base64string" >base64 encrypted string </param>
<returns> text string after restore </returns>
public static string Decodingforstring (String base64string)
{
Return decodingforstring (base64string,system.text.encoding.getencoding (54936));
}

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

<summary>
Base64 any type of file
</summary>
<param name= "filename" > File path and filename </param>
<returns> string after base64 encoding of files </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 a base64 encoded string as a file
</summary>
<param name= "base64string" > Base64 after the string </param>
<param name= "filename" > Save file path and filename </param>
<returns> Save File Success </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 a file from the network address and convert it to base64 encoding
</summary>
<param name= "url" > file URL address, an absolute URL address </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 a file from the network address and convert it to base64 encoding
</summary>
<param name= "url" > file URL address, an absolute URL address </param>
<returns> convert file to Base64 string </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.