using System;
using System.Security.Cryptography;
/// <summary>
///
/// </summary>
///
/// the cipher key is a pair of values thet determined the encription strength
/// by set the encryption key lengt and block size.
/// thare are 3 possible pair according to the desired encryption strength.
/// eache pair have index-0 for weak,1 medium strength, encryption and 2 for the strong encryption
struct CipherKey
{
// the const for the pair inedex;
private const int LowLevelEnc=0;
private const int MedLevelEnc=1;
private const int HigLevelEnc=2;
//const for key length
private const int LowKeyLen=16;
private const int MedKeyLen=24;
private const int HigKeyLen=32;
//const for block size
private const int LowKeySiz=128;
private const int MedKeySiz=192;
private const int HigKeySiz=256;
public int Len;
public int Size;
//function gets the cipher pair index and return the pair -blocj size ,key length
public static CipherKey getCipherKey(int EncLevel)
{
CipherKey Key;
switch (EncLevel)
{
case LowLevelEnc:
{
Key.Len=LowKeyLen;
Key.Size=LowKeySiz;
return Key;
}
case MedLevelEnc:
{
Key.Len=MedKeyLen;
Key.Size=MedKeySiz;
return Key;
}
case HigLevelEnc:
{
Key.Len=HigKeyLen;
Key.Size=HigKeySiz;
return Key;
}
}
throw new Exception("Invalid cipher Level- allow 0..2 -get: "+EncLevel.ToString());
}
}
public class EncDec
{
//the encription cipher mode
private static System.Security.Cryptography.CipherMode sCipherMode=CipherMode.CBC;
//the padding mode-
//available values:
//PaddingMode.None,PaddingMode.PKCS7,PaddingMode.Zeros ;
private static System.Security.Cryptography.PaddingMode sPaddingMode=PaddingMode.PKCS7;
public static string encrypt(string text, //the text to be encrypt
string password,// the encryption key
int cipherindex//The strength of the encryption
)
{
try
{
//get the cipher strength--from cipherindex
CipherKey Key=CipherKey.getCipherKey(cipherindex);
//build and init the Encryptor
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = sCipherMode;
rijndaelCipher.Padding = sPaddingMode;
rijndaelCipher.KeySize = Key.Size;
rijndaelCipher.BlockSize =Key.Size;
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] keyBytes = new byte[Key.Len];
int len= pwdBytes.Length;
if (len > keyBytes.Length) len= keyBytes.Length;
System.Array.Copy(pwdBytes,keyBytes,len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte [] plainText = System.Text.Encoding.UTF8.GetBytes(text);
byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherBytes);
}
catch (Exception)
{
throw ;
}
}
public static string decrypt(string text, //the text to be decrypt
string password,//the original encription key
int cipherindex// the strength of the original encryption
)
{
try
{ //get the cipher strength--from cipherindex
CipherKey Key=CipherKey.getCipherKey(cipherindex);
//build and init the Decryptor
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = sCipherMode;
rijndaelCipher.Padding = sPaddingMode;
rijndaelCipher.KeySize = Key.Size;
rijndaelCipher.BlockSize =Key.Size;
byte[] encryptedData = Convert.FromBase64String(text);
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] keyBytes = new byte[Key.Len];
int len= pwdBytes.Length;
if (len > keyBytes.Length) len= keyBytes.Length;
System.Array.Copy(pwdBytes,keyBytes,len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return System.Text.Encoding.UTF8.GetString(plainText);
}
catch (Exception)
{
throw ;
}
}
}