using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace GBChipherLib
{
/// <summary>
/// Summary description for Chipher2.
/// </summary>
public class Chipher2
{
private ICryptoTransform encRij;
private ICryptoTransform enc3des;
private ICryptoTransform decRij;
private ICryptoTransform dec3des;
private byte[] keyRij, key3des;
private byte[] ivRij, iv3des;
private string encryptionCode = "[!@#$%^&*()~`'<>?|:;{}_-.,]";
public Chipher2()
{
try
{
byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(encryptionCode);
SHA1 hash = new SHA1Managed();
for (int i = 0; i < 200; ++i)
{
salt = hash.ComputeHash(salt, 0, salt.Length);
hash.Initialize();
}
PasswordDeriveBytes pdb = new PasswordDeriveBytes(encryptionCode, salt, "SHA512", 1000);
Array.Clear(salt, 0, salt.Length);
hash.Clear();
keyRij = pdb.GetBytes(32);
key3des = pdb.GetBytes(24);
ivRij = pdb.GetBytes(16);
iv3des = pdb.GetBytes(8);
////////////////////////////////////////////////////////
RijndaelManaged aes = new RijndaelManaged();
TripleDESCryptoServiceProvider des3 = new TripleDESCryptoServiceProvider();
encRij = aes.CreateEncryptor(keyRij, ivRij);
enc3des = des3.CreateEncryptor(key3des, iv3des);
decRij = aes.CreateDecryptor(keyRij, ivRij);
dec3des = des3.CreateDecryptor(key3des, iv3des);
des3.Clear();
aes.Clear();
}
catch(Exception ex)
{
throw new Exception("GBChipherLib.Chipher2(): " + ex.GetType() + ex.Message);
}
}
public void SetKey(string pwd)
{
encryptionCode = pwd;
}
public bool Encrypt(string s, ref string sOut)
{
bool bReturn = false;
try
{
sOut = EncryptAndBase64(s, enc3des);
bReturn = true;
}
catch (Exception ex)
{
throw new Exception("GBChipherLib.Chipher2(): Encrypt(): " + ex.GetType() + ex.Message);
}
return bReturn;
}
public bool Decrypt(string s, ref string sOut)
{
bool bReturn = false;
try
{
SymmetricAlgorithm alg = new RijndaelManaged();
alg = new TripleDESCryptoServiceProvider();
ICryptoTransform des3 = alg.CreateDecryptor(key3des, iv3des);
alg.Clear();
sOut = DecryptFromBase64(s, des3);
bReturn = true;
}
catch (Exception ex)
{
throw new Exception("GBChipherLib.Chipher2(): Decrypt(): " + ex.GetType() + ex.Message);
}
return bReturn;
}
private string EncryptAndBase64(string val, ICryptoTransform trans)
{
string ret = null;
try
{
byte[] buf = UnicodeEncoding.Unicode.GetBytes(val);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(memStream, trans, CryptoStreamMode.Write);
cryptStream.Write(buf, 0, buf.Length);
cryptStream.FlushFinalBlock();
cryptStream.Clear();
Array.Clear(buf, 0, buf.Length);
buf = memStream.GetBuffer();
ret = Convert.ToBase64String(buf, 0, (int)memStream.Length);
Array.Clear(buf, 0, (int)memStream.Length);
}
catch (Exception ex)
{
throw new Exception("GBChipherLib.Chipher2(): EncryptAndBase64(): " + ex.GetType() + ex.Message);
}
return ret;
}
private string DecryptFromBase64(string base64str, ICryptoTransform trans)
{
string ret = null;
try
{
byte[] buf = Convert.FromBase64String(base64str);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(memStream, trans, CryptoStreamMode.Write);
cryptStream.Write(buf, 0, buf.Length);
cryptStream.FlushFinalBlock();
cryptStream.Clear();
Array.Clear(buf, 0, buf.Length);
buf = memStream.GetBuffer();
ret = UnicodeEncoding.Unicode.GetString(buf, 0, (int)memStream.Length);
Array.Clear(buf, 0, (int)memStream.Length);
memStream.Close();
}
catch (Exception ex)
{
throw new Exception("GBChipherLib.Chipher2(): DecryptFromBase64(): " + ex.GetType() + ex.Message);
}
return ret;
}
}
}
How to use it:
ChipherComp cc= new ChipherComp();
cc.SetKey("blabla..."); // if you want to pass another key
string sOut="";
string sOut2="";
cc.Encrypt("I would like to make a program to run on a Pocket PC ",ref sOut);
Console.WriteLine(sOut); // encrypted string
cc.Decrypt(sOut, ref sOut2);
Console.WriteLine(sOut2); // "I would like to make a program to run on a Pocket PC "
-obislavu-