Encrypt and Decrypt – Basic version

October 24, 2009

This is a basic version of encrypt-decrypt function…we used this from one of the .NET sites, since i am not able to find teh reference link , iam posting it here so that others can use it…
public class MyEnctDect
{
private Byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
private Byte[] iv = { 65, 110, 68, 26, 69, 178, 200, 219 };

public string Encrypt(string plainText)
{
//’ Declare a UTF8Encoding object so we may use the GetByte
//’ method to transform the plainText into a Byte array.
String outString;
UTF8Encoding utf8encoder = new UTF8Encoding();
Byte[] inputInBytes = utf8encoder.GetBytes(plainText);
//’ Create a new TripleDES service provider
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
//’ The ICryptTransform interface uses the TripleDES
//’ crypt provider along with encryption key and init vector
//’ information
ICryptoTransform cryptoTransform = tdesProvider.CreateEncryptor(key, iv);
//’ All cryptographic functions need a stream to output the
//’ encrypted information. Here we declare a memory stream
//’ for this purpose.
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write);
//’ Write the encrypted information to the stream. Flush the information
//’ when done to ensure everything is out of the buffer.
cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
cryptStream.FlushFinalBlock();
encryptedStream.Position = 0;
//’ Read the stream back into a Byte array and return it to the calling
//’ method.
// Dim result(encryptedStream.Length – 1) As Byte
byte[] result = new byte[encryptedStream.Length];
encryptedStream.Read(result, 0, (int)encryptedStream.Length);
cryptStream.Close();
//’test section
//’Convert to / fro the memory stream to a base64 string
//’compare decText to result
outString = System.Text.ASCIIEncoding.Default.GetString(result);

string encText = Convert.ToBase64String(encryptedStream.ToArray());
byte[] decText = Convert.FromBase64String(encText);
string sDec = Decrypt(decText);
return outString;
}

public String Decrypt(Byte[] inputInBytes)
{
//’ UTFEncoding is used to transform the decrypted Byte Array
//’ information back into a string.
UTF8Encoding utf8encoder = new UTF8Encoding();
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
//’ As before we must provide the encryption/decryption key along with
//’ the init vector.
ICryptoTransform cryptoTransform = tdesProvider.CreateDecryptor(key, iv);
//’ Provide a memory stream to decrypt information into
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write);
cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
cryptStream.FlushFinalBlock();
decryptedStream.Position = 0;
//’ Read the memory stream and convert it back into a string
Byte[] result = new byte[decryptedStream.Length];
decryptedStream.Read(result, 0, (int)decryptedStream.Length);
cryptStream.Close();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(result);
}
}
usage
MyEnctDect classDeclartion = new MyEnctDect();
classDeclartion.Encrypt(text);
classDeclartion.Decrypt(bytearray);

Its a good one for beginners, there are other methods available as well


Creating controls with javascript

October 23, 2009

Thought this will be helpful for beginners

Create static combo
Create password field
create textbox
create textarea, all samples are subject to COPY rights…i mean you can use them for freee…

function CreateStaticCombo(sCtrlId, Width, list, value, defaultValue) {

var optGroup = document.createElement(’select’)
var arr = list.split(‘###’);

optGroup.setAttribute(‘className’, ‘ComboBoxStyle’);
optGroup.onblur = function() { };
optGroup.id = sCtrlId;
optGroup.name = sCtrlId;

var objOption = document.createElement(“option”)
objOption.innerText = “”;
objOption.value = “”;
optGroup.appendChild(objOption)

for (var i = 0; i < arr.length; i++) {
if (arr[i] != "") {
var objOption = document.createElement("option")
objOption.innerText = arr[i];
objOption.value = arr[i];

if (arr[i] == value.trim())
objOption.selected = true;

optGroup.appendChild(objOption)
}
}
optGroup.style.border = 0;
optGroup.onkeyup = function() { };
return optGroup;

}

function CreateLookupIcon(sCtrlId, IsLookupConditionAvailable) {
var imageNode = document.createElement("input");
imageNode.type = 'image';
imageNode.id = "imagetext"
imageNode.src = '../images/asmall.gif';

imageNode.onclick = function() { someJavascriptFunction(someParameters); return false; };
return imageNode;
}
function CreateHiddenField(sCtrlId) {
var txtbox = document.createElement('input');
txtbox.type = 'hidden';
txtbox.id = sCtrlId.replace('TD', 'D');
txtbox.name = sCtrlId.replace('TD', 'D');
return txtbox;
}

function CreatePassword(sCtrlId) {
var txtbox = document.createElement('input');
txtbox.onblur = function() { SetHiddenValue(this.id, this.value); EvaluateExpression('ALL', sCtrlId.toString().split('~')[2]) };
txtbox.type = 'password';
txtbox.passwordText = '*';
txtbox.id = sCtrlId;
txtbox.name = sCtrlId;
txtbox.setAttribute('className', 'MVTextBoxStyle');
txtbox.style.border = 1;
txtbox.style.bordercolor = "black";
txtbox.style.borderStyle = "solid"
return txtbox;
}

function CreateTextArea(sCtrlId, Width) {
var txtbox = document.createElement('textarea');
txtbox.rows = "2";
txtbox.style.height = "40px";
txtbox.onblur = function() { SetHiddenValue(this.id.replace('txt', ''), this.value); EvaluateExpression('ALL', sCtrlId.toString().split('~')[2]) };
txtbox.id = "txt" + sCtrlId;
txtbox.name = "txt" + sCtrlId;
txtbox.setAttribute('className', 'MVTextBoxStyle');

return txtbox;
}