article

Easy Encryption

Email
Submitted on: 5/7/2016 8:52:28 AM
By: Jon Elliott 
Level: Beginner
User Rating: Unrated
Compatibility: C#, VB.NET, ASP.NET
Views: 2219
author picture
 
     This article is a class you can easily add to any project and start encrypting as

 
				using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Security.Common
{
/// 
/// Encrypts and Decrypts strings
/// 
public sealed class EncryptionHelper
{
private TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
private UTF8Encoding utf8 = new UTF8Encoding();
/// 
/// The embeded encyption Key, default key
/// 
private byte[] _CryptoKey = { 8, 6, 6, 9, 3, 0, 9, 8, 6, 8, 5, 3,1, 9, 7, 8, 6, 4, 4, 4, 4, 1, 69, 69 };
/// 
/// The initialization Vector
/// 
private byte[] _InitVector = { 69, 6, 1, 8, 2, 3, 69, 1 };
/// 
/// Constructor
/// 
public EncryptionHelper()
{
//NOOP, do not forget to set your key in the 
}
public EncryptionHelper(byte[] CryptoKey)
{
_CryptoKey = CryptoKey;
}
		///For use on the web, like a Query string variable
public string UrlEncode(string input)
{
var output = input;
output = output.Replace("=","@"); 
output = output.Replace("+", "-"); 
output = output.Replace("/", "_");
return output;
}
///For use on the web, like a Query string variable
public string UrlDecode(string input)
{
var output = input;
output = output.Replace("@", "=");
output = output.Replace("-", "+");
output = output.Replace("_", "/");
return output;
}
/// 
/// Key to use during encryption and decryption
/// 
/// 
/// 
/// byte[] key Ex. { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
/// 
/// 
public byte[] CryptoKey
{
get { return _CryptoKey; }
set { _CryptoKey = value; }
}
/// 
/// Initialization vector to use during encryption and decryption
/// 
/// 
/// 
/// byte[] iv Example { 8, 7, 6, 5, 4, 3, 2, 1 };
/// 
/// 
public byte[] InitializationVector
{
get { return _InitVector; }
set { _InitVector = value; }
}
/// 
/// Decrypt a string
/// 
/// 
/// Decrypted data as string
public string Decrypt(string text)
{
try
{
byte[] input = Convert.FromBase64String(text);
byte[] output = Transform(input, des.CreateDecryptor(this._CryptoKey, this._InitVector));
return utf8.GetString(output);
}
catch (Exception ex)
{
return text;
}
}
/// 
/// Encrypt a string
/// 
/// 
/// Encrypted data as string
public string Encrypt(string text)
{
byte[] input = utf8.GetBytes(text);
byte[] output = Transform(input, des.CreateEncryptor(this._CryptoKey, this._InitVector));
return Convert.ToBase64String(output);
}
/// 
/// Encrypt or Decrypt bytes.
/// 
/// 
/// This is used by the public methods
/// 
/// Data to be encrypted/decrypted
/// 
/// des.CreateEncryptor(this.keyValue, this.iVValue)
/// 
/// Byte data containing result of operation
private byte[] Transform(byte[] input, ICryptoTransform cryptoTransform)
{
// Create the necessary streams
var memory = new MemoryStream();
var stream = new CryptoStream(memory, cryptoTransform, CryptoStreamMode.Write);
// Transform the bytes as requested
stream.Write(input, 0, input.Length);
stream.FlushFinalBlock();
// Read the memory stream and convert it back into byte array
memory.Position = 0;
var result = new byte[memory.Length];
memory.Read(result, 0, result.Length);
// Clean up
memory.Close();
stream.Close();
// Return result
return result;
}
}
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/19/2018 2:38:29 AMDavid

Can you please give a longer description of how to use it please. I like the idea, but with the description missing, it's not much help. Thanks for the upload.

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.