367 lines
9.6 KiB
C#
367 lines
9.6 KiB
C#
|
/*
|
||
|
|-------------------------------------------------------------------------------|
|
||
|
| Copyright © Computer Ease Limited |
|
||
|
| Address: 1/9 Bloack-A Lalmatia, Dhaka-1207, Bangladesh |
|
||
|
| Email: info@celimited.com, cease@bol-online.com, web: www.celimited.com |
|
||
|
| Unauthorized copy or distribution is strictly prohibited |
|
||
|
| Author: S. M. Russel, Last modified date: 23/07/2012 |
|
||
|
|-------------------------------------------------------------------------------|
|
||
|
*/
|
||
|
|
||
|
using System;
|
||
|
using System.Text;
|
||
|
using System.Security.Cryptography;
|
||
|
|
||
|
namespace Ease.Core.Utility
|
||
|
{
|
||
|
internal class Encryption
|
||
|
{
|
||
|
#region Declaration & Constructor
|
||
|
|
||
|
internal const string DefaultPublicKey = "CoMEaLtD";
|
||
|
internal const string DefaultPrivateKey = "CeL.DhK.LaL1@3$5";
|
||
|
|
||
|
internal Encryption()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Helper functions
|
||
|
|
||
|
protected string ByteToHex(byte[] value)
|
||
|
{
|
||
|
string s = string.Empty;
|
||
|
for (int i = 0; i < value.Length; i++)
|
||
|
s += ByteToHex(value[i]);
|
||
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
protected string ByteToHex(byte value)
|
||
|
{
|
||
|
return Convert.ToString(value, 16).PadLeft(2, '0').ToUpper();
|
||
|
}
|
||
|
|
||
|
protected string IntToHex(int intData)
|
||
|
{
|
||
|
return Convert.ToString(intData, 16).PadLeft(2, '0');
|
||
|
}
|
||
|
|
||
|
protected byte[] HexToBytes(string value)
|
||
|
{
|
||
|
byte[] bytes = new byte[value.Length / 2];
|
||
|
for (int i = 0; i < value.Length; i += 2)
|
||
|
bytes[i / 2] = HexToByte(value.Substring(i, 2));
|
||
|
|
||
|
return bytes;
|
||
|
}
|
||
|
|
||
|
protected byte HexToByte(string value)
|
||
|
{
|
||
|
return Convert.ToByte(value, 16);
|
||
|
}
|
||
|
|
||
|
protected int HexToInt(string hexData)
|
||
|
{
|
||
|
return Convert.ToInt32(hexData, 16);
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Encrypt & Decrypt
|
||
|
|
||
|
internal string Encrypt(string key, string data)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
char[] dataChrs, keyChrs;
|
||
|
string encryptedData = string.Empty;
|
||
|
|
||
|
//Save Length of Pass
|
||
|
data = (char)(data.Length) + data;
|
||
|
|
||
|
//Pad Password with space upto 10 Characters
|
||
|
if (data.Length < 10)
|
||
|
data = data.PadRight(10);
|
||
|
|
||
|
dataChrs = data.ToCharArray();
|
||
|
|
||
|
//Make the key big enough
|
||
|
while (key.Length < data.Length)
|
||
|
{
|
||
|
key += key;
|
||
|
}
|
||
|
|
||
|
key = key.Substring(0, data.Length);
|
||
|
keyChrs = key.ToCharArray();
|
||
|
|
||
|
//Encrypting Data
|
||
|
for (int i = 0; i < data.Length; i++)
|
||
|
{
|
||
|
char textChr = dataChrs[i];
|
||
|
char keyChr = keyChrs[i];
|
||
|
|
||
|
encryptedData += IntToHex((int)(textChr) ^ (int)(keyChr));
|
||
|
}
|
||
|
|
||
|
return encryptedData.ToUpper();
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
throw new Exception(e.Message, e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
internal string Decrypt(string key, string data)
|
||
|
{
|
||
|
string decryptedData = string.Empty;
|
||
|
|
||
|
//Taking Lenght, half of Encrypting data
|
||
|
int len = data.Length / 2;
|
||
|
|
||
|
//Making key is big Enough
|
||
|
while (key.Length < len)
|
||
|
{
|
||
|
key += key;
|
||
|
}
|
||
|
|
||
|
key = key.Substring(0, len);
|
||
|
|
||
|
char[] keyChrs = key.ToCharArray();
|
||
|
char[] textChrs = data.ToCharArray();
|
||
|
|
||
|
//Decripting data
|
||
|
for (int i = 0; i < len; i++)
|
||
|
{
|
||
|
string textByte = string.Empty;
|
||
|
for (int j = i * 2; j < (i * 2 + 2); j++)
|
||
|
{
|
||
|
textByte += string.Format("{0}", textChrs.GetValue(j));
|
||
|
}
|
||
|
|
||
|
char textChr = (char)HexToInt(textByte);
|
||
|
char keyChr = keyChrs[i];
|
||
|
|
||
|
decryptedData += (char)((int)(keyChr) ^ (int)(textChr));
|
||
|
}
|
||
|
|
||
|
len = (int)decryptedData.ToCharArray()[0];
|
||
|
decryptedData = decryptedData.Substring(1, len);
|
||
|
|
||
|
return decryptedData;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
|
||
|
internal class TDSEncryption : Encryption
|
||
|
{
|
||
|
#region Declaration & Constructor
|
||
|
|
||
|
private TripleDESCryptoServiceProvider _tdesCSP;
|
||
|
|
||
|
internal TDSEncryption()
|
||
|
{
|
||
|
_tdesCSP = new TripleDESCryptoServiceProvider();
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Helper function
|
||
|
|
||
|
void initialize(string privateKey, string publicKey)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
#region Make the key 128 bit
|
||
|
|
||
|
if (privateKey.Length < 16)
|
||
|
privateKey = privateKey.PadLeft(16);
|
||
|
|
||
|
if (privateKey.Length > 16)
|
||
|
privateKey = privateKey.Substring(0, 16);
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Make IV 64 bit
|
||
|
|
||
|
if (publicKey.Length < 8)
|
||
|
publicKey = publicKey.PadLeft(8);
|
||
|
|
||
|
if (publicKey.Length > 8)
|
||
|
publicKey = publicKey.Substring(0, 8);
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
_tdesCSP.Key = ASCIIEncoding.ASCII.GetBytes(privateKey);
|
||
|
_tdesCSP.IV = ASCIIEncoding.ASCII.GetBytes(publicKey);
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
throw new Exception("Invaid Key");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void destroy()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (_tdesCSP != null)
|
||
|
_tdesCSP.Clear();
|
||
|
_tdesCSP = null;
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Encrypt & Decrypt
|
||
|
|
||
|
internal string Encrypt(string privateKey, string publicKey, string data)
|
||
|
{
|
||
|
string value = string.Empty;
|
||
|
try
|
||
|
{
|
||
|
this.initialize(privateKey, publicKey);
|
||
|
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(data);
|
||
|
value = this.ByteToHex(_tdesCSP.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
|
||
|
this.destroy();
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
throw new Exception("Invalid Message");
|
||
|
}
|
||
|
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
internal string Decrypt(string privateKey, string publicKey, string data)
|
||
|
{
|
||
|
string value = string.Empty;
|
||
|
try
|
||
|
{
|
||
|
this.initialize(privateKey, publicKey);
|
||
|
byte[] buffer = this.HexToBytes(data);
|
||
|
value = ASCIIEncoding.ASCII.GetString(_tdesCSP.CreateDecryptor()
|
||
|
.TransformFinalBlock(buffer, 0, buffer.Length));
|
||
|
this.destroy();
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
throw new Exception("Invalid Message");
|
||
|
}
|
||
|
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
|
||
|
internal class AESEncryption : Encryption
|
||
|
{
|
||
|
#region Declaration & Constructor
|
||
|
|
||
|
private AesCryptoServiceProvider _aesCSP;
|
||
|
|
||
|
internal AESEncryption()
|
||
|
{
|
||
|
_aesCSP = new AesCryptoServiceProvider();
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Helper functions
|
||
|
|
||
|
void initialize(string privateKey, string publicKey)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
#region Make the key 256 bit
|
||
|
|
||
|
if (privateKey.Length < 32)
|
||
|
privateKey = privateKey.PadLeft(32);
|
||
|
|
||
|
if (privateKey.Length > 32)
|
||
|
privateKey = privateKey.Substring(0, 32);
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Make IV 128 bit
|
||
|
|
||
|
if (publicKey.Length < 16)
|
||
|
publicKey = publicKey.PadLeft(16);
|
||
|
|
||
|
if (publicKey.Length > 16)
|
||
|
publicKey = publicKey.Substring(0, 16);
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
_aesCSP.Key = ASCIIEncoding.ASCII.GetBytes(privateKey);
|
||
|
_aesCSP.IV = ASCIIEncoding.ASCII.GetBytes(publicKey);
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
throw new Exception("Invaid Key");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void destroy()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (_aesCSP != null)
|
||
|
_aesCSP.Clear();
|
||
|
_aesCSP = null;
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Encrypt & Decrypt
|
||
|
|
||
|
public string Encrypt(string privatekey, string publickey, string data)
|
||
|
{
|
||
|
string value = string.Empty;
|
||
|
try
|
||
|
{
|
||
|
this.initialize(privatekey, publickey);
|
||
|
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(data);
|
||
|
value = this.ByteToHex(_aesCSP.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
|
||
|
this.destroy();
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
throw new Exception("Invalid Message");
|
||
|
}
|
||
|
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
public string Decrypt(string privatekey, string publickey, string data)
|
||
|
{
|
||
|
string value = string.Empty;
|
||
|
try
|
||
|
{
|
||
|
this.initialize(privatekey, publickey);
|
||
|
byte[] buffer = this.HexToBytes(data);
|
||
|
value = ASCIIEncoding.ASCII.GetString(_aesCSP.CreateDecryptor()
|
||
|
.TransformFinalBlock(buffer, 0, buffer.Length));
|
||
|
this.destroy();
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
throw new Exception("Invalid Message");
|
||
|
}
|
||
|
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
}
|