Base64 encrypted strings and files

Source: Internet
Author: User
Code

Using System;

Namespace Shapbse64
{
 
///   <Summary>
/// Base64 encodingAlgorithmRelated Operations
/// By free Pentium (wgscd)
///   </Summary>
Public   Class Sbase64
{
Public Sbase64 ()
{
//
// Todo: add the constructor logic here
//
}
// --------------------------------------------------------------------------------
///   <Summary>
/// Use base64 to encrypt a string
///   </Summary>
///   <Param name = "sourcestring"> String to be encrypted </Param>
///   <Param name = "ens"> System. Text. Encoding object. For example, create a Chinese Character Set object: system. Text. encoding. getencoding (54936) </Param>
///   <Returns> Text string after Encryption </Returns>
Public   Static   String Encodingforstring ( String Sourcestring, system. Text. Encoding ENS)
{
Return Convert. tobase64string (ENS. getbytes (sourcestring ));
}

///   <Summary>
/// Use base64 to encrypt a string
///   </Summary>
///   <Param name = "sourcestring"> String to be encrypted </Param>
///   <Returns> Text string after Encryption </Returns>
Public   Static   String Encodingforstring ( String Sourcestring)
{
Return Encodingforstring (sourcestring, system. Text. encoding. getencoding ( 54936 ));
}

///   <Summary>
/// Restores a 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 Character 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, the encoding class provided by the. NET class library is used to implement the conversion from ANSI encoding to unicode encoding. Code Conversion
*
* 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>
/// Restores a 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 ));
}

// Bytes --------------------------------------------------------------------------------------

///   <Summary>
/// Add base64 to any type of Files
///   </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 ));*/

StringBase64string=Convert. tobase64string (Br. readbytes ((Int) Fs. Length ));

BR. Close ();
FS. Close ();
ReturnBase64string;
}

///   <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 saved successfully </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 ;
}

 

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.