We rely on encryption on the Internet more than ever before. For shopping, paying bills, banking online, and many other purposes, it's important that our Internet transactions remain as secure as possible. Encryption is an especially popular and effective technique for maintaining Internet security...
I stumble upon this class on some forum. This is not my original work. I'm posting it today just to share.
It has 2 method encrypt and decrypt. Pretty simple. Any questions just post a comment.
using System;
/// <summary>
/// MyEncryption64 will encode and decode data.
/// </summary>
public class MyEncryption64
{
public MyEncryption64()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// This method will encrypt the send string of data.
/// </summary>
/// <param name="data">Data string to be encrypted</param>
/// <returns></returns>
public string Encode(string data)
{
try
{
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
}
catch (Exception e)
{
throw new Exception("Error in MyEncryption64 Encode" + e.Message);
}
}
/// <summary>
/// This method will decrypt the send string of data.
/// </summary>
/// <param name="data">Data string to be decrypted</param>
/// <returns></returns>
public string Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in MyEncryption64 Decode" + e.Message);
}
}
}