usingSystem;usingSystem.Text; namespacecommon{/// <summary> ///implement BASE64 encryption and decryption///Zhou Gong///Time:/// </summary> Public Sealed classBase64 {/// <summary> ///Base64 Encryption/// </summary> /// <param name= "codename" >encoding used for encryption</param> /// <param name= "source" >plaintext to encrypt</param> /// <returns></returns> Public Static stringEncodeBase64 (Encoding encode,stringsource) { byte[] bytes =encode. GetBytes (source); Try{encode=convert.tobase64string (bytes); } Catch{encode=source; } returnencode; } /// <summary> ///Base64 encryption, encrypted by UTF8 encoding/// </summary> /// <param name= "source" >plaintext to encrypt</param> /// <returns>the encrypted string</returns> Public Static stringEncodeBase64 (stringsource) { returnEncodeBase64 (Encoding.UTF8, source); } /// <summary> ///Base64 Decryption/// </summary> /// <param name= "codename" >Decryption is encoded in a way that is consistent with the way encryption is used</param> /// <param name= "Result" >ciphertext to be decrypted</param> /// <returns>the decrypted string</returns> Public Static stringDecodeBase64 (Encoding encode,stringresult) { stringdecode =""; byte[] bytes =convert.frombase64string (Result); Try{decode=encode. GetString (bytes); } Catch{decode=result; } returndecode; } /// <summary> ///Base64 decryption, using UTF8 encoding method to decrypt/// </summary> /// <param name= "Result" >ciphertext to be decrypted</param> /// <returns>the decrypted string</returns> Public Static stringDecodeBase64 (stringresult) { returnDecodeBase64 (Encoding.UTF8, result); } }}
C # Implementation of BASE64 processing, encryption and decryption, encoding and decoding