usingSystem;usingSystem.Text;namespacecommon{/// <summary>///implement BASE64 encryption and decryption/// </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); }}}
Base64 encryption decryption in C #