This article mainly describes the C # implementation of BASE64 processing of encryption and decryption, encoding and decoding, combined with the example of the implementation of C # Base64 encoding and decoding operation-related skills, the need for friends can refer to the following
The example of this paper describes the C # implementation of BASE64 processing of encryption and decryption, encoding and decoding. Share to everyone for your reference, as follows:
using System;
using System.Text;
namespace Common
{
/// <summary>
/// Implement Base64 encryption and decryption
/// Author: Duke
/// </ summary>
public sealed class Base64
{
/// <summary>
/// Base64 encryption
/// </ summary>
/// <param name = "codeName"> Encoding used for encryption </ param>
/// <param name = "source"> plaintext to be encrypted </ param>
/// <returns> </ returns>
public static string EncodeBase64 (Encoding encode, string source)
{
byte [] bytes = encode.GetBytes (source);
try
{
encode = Convert.ToBase64String (bytes);
}
catch
{
encode = source;
}
return encode;
}
/// <summary>
/// Base64 encryption, using utf8 encoding
/// </ summary>
/// <param name = "source"> plaintext to be encrypted </ param>
/// <returns> encrypted string </ returns>
public static string EncodeBase64 (string source)
{
return EncodeBase64 (Encoding.UTF8, source);
}
/// <summary>
/// Base64 decryption
/// </ summary>
/// <param name = "codeName"> The encoding method used for decryption, pay attention to the same method used for encryption </ param>
/// <param name = "result"> the ciphertext to be decrypted </ param>
/// <returns> the decrypted string </ returns>
public static string DecodeBase64 (Encoding encode, string result)
{
string decode = "";
byte [] bytes = Convert.FromBase64String (result);
try
{
decode = encode.GetString (bytes);
}
catch
{
decode = result;
}
return decode;
}
/// <summary>
/// Base64 decryption, using utf8 encoding
/// </ summary>
/// <param name = "result"> the ciphertext to be decrypted </ param>
/// <returns> the decrypted string </ returns>
public static string DecodeBase64 (string result)
{
return DecodeBase64 (Encoding.UTF8, result);
}
}
}