encrypting and decrypting function using Csharp

11. February 2010

Author : Aamir Hasan 
Below is the given code how to encrypt and decrypt the data

public class Encryption_Decryption
{
const char CharacterToFill = '@';
static string Key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

public string EnCrypt(string str)
{
int count;
int ctr;
int size = str.Length;
string R = "";

for (count = 0; count < size; ++count)
{
ctr = (str[count] >> 2) & 0x3f;
R += Key[ctr];
ctr = (str[count] << 4) & 0x3f;
if (++count < size)
ctr |= (str[count] >> 4) & 0x0f;

R += Key[ctr];
if (count < size)
{
ctr = (str[count] << 2) & 0x3f;
if (++count < size)
ctr |= (str[count] >> 6) & 0x03;

R += Key[ctr];
}
else
{
++count;
R += CharacterToFill;
}

if (count < size)
{
ctr = str[count] & 0x3f;
R += Key[ctr];
}
else
{
R += CharacterToFill;
}
}

return (R);
}

public string DeCrypt(string str)
{
string R = "";
int count;
char ctr;
char ctrNext;
int size = str.Length;

for (count = 0; count < size; ++count)
{
ctr = (char)Key.IndexOf(str[count]);
++count;
ctrNext = (char)Key.IndexOf(str[count]);
ctr = ((char)((ctr << 2) | ((ctrNext >> 4) & 0x3)));
R += ctr;
if (++count < size)
{
ctr = str[count];
if (CharacterToFill == ctr)
break;

ctr = (char)Key.IndexOf(ctr);
ctrNext = (char)(((ctrNext << 4) & 0xf0) | ((ctr >> 2) & 0xf));
R += ctrNext;
}

if (++count < size)
{
ctrNext = str[count];
if (CharacterToFill == ctrNext)
break;

ctrNext = (char)Key.IndexOf(ctrNext);
ctr = (char)(((ctr << 6) & 0xc0) | ctrNext);
R += ctr;
}
}
return (R);
}
}

 

.


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, CSharp , ,

Capturing Web Camera and Microphone using Silver

4. February 2010

Author : Aamir Hasan

This is my first tutorial on silverlight.In this article i am going tell you how to utilize a user's web camera and microphone attached to their computer via the browser, to capturing audio and video.


First you have to add namespace in your cs file as given below

using System.Windows.Media;
using System.Windows.Media.Imaging;


ObservableCollection images adding images to it:

CaptureSource _captureSource;
ObservableCollection<WriteableBitmap> _images = new ObservableCollection<WriteableBitmap>();

Caturing the video


  

if (_captureSource != null)
{
_captureSource.Stop(); // stop whatever device may be capturing

// set the devices for the capture source
_captureSource.VideoCaptureDevice = (VideoCaptureDevice)VideoSources.SelectedItem;
_captureSource.AudioCaptureDevice = (AudioCaptureDevice)AudioSources.SelectedItem;

// create the brush
VideoBrush vidBrush = new VideoBrush();
vidBrush.SetSource(_captureSource);
WebcamCapture.Fill = vidBrush; // paint the brush on the rectangle

// request user permission and display the capture
if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
{
_captureSource.Start();
}
}

 

Create a Snapshot from the Video


When the capture process has started and a user clicks the Take Snapshot button, the appropriate command is invoked in the VM and the capture source’s AsyncCaptureImage method is invoked. I used the Dispatcher to make sure I operated on the UI thread because I wanted to not only grab the bitmap.


 

private void StopCapture_Click(object sender, RoutedEventArgs e)
{
if (_captureSource != null)
{
_captureSource.Stop();
}
}

private void TakeSnapshot_Click(object sender, RoutedEventArgs e)
{
if (_captureSource != null)
{
// capture the current frame and add it to our observable collection
_captureSource.AsyncCaptureImage((snapImage) =>
{
_images.Add(snapImage);
});
}
}


 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, CSharp ,

Find Out Specific File Extensions In a Root Directory

4. January 2010

String[] files = Directory.GetFiles("", "*.xls");
int totalCount = files.Length;
int count;
foreach (String fileName in files)
{
if (GetSheetName(fileName).Equals("your value"))
count++;
}

private String GetSheetName(String fileName)
{
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;"
+ @"Data Source=" + fileName + @";Extended Properties=""Excel 12.0;HDR=YES;""";

OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
String sheetName = this.GetSheetName(connection.GetSchema("columns"));
return sheetName;
}



To Find out the files in a root here i have given example of Excel files
Regards
Aamir Hasan


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, CSharp , ,

Bulk Copy From Datatable To SQL Server Table

1. January 2010

 public bool BulkEnterData(DataTable dt, string tblName)
    {
        SqlBulkCopy bulk = new SqlBulkCopy(con);
        bulk.DestinationTableName = tblName;
       
        con.Open();
        bulk.WriteToServer(dt);
        con.Close();

        return true;
    }

 

 

to run sqlbulkcopy u must define listed below if it give u an error {A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 - The pipe has been ended.)}

Go to Start -> Programs -> Microsoft SQL Server 2005 -> Configuration Tools -> SQL Server Surface Area Configuration.

 

Then go to "Surface Area Configuration for Services and Connections"

 

Then select "Remote Connections" under "Database Engine" option.

 

Then select "Using both TCP/IP and named pipes" radio button under Local and remote connections.


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, CSharp, SQL 2005 & 2008 , , ,

Serialization and Deserialization

29. December 2009

Serialization and Deserialization

Serialization definition
serialization is the process of converting object into a sequence of bits so
that it can be stored in a file, a memory buffer, or transmitted across a network connection

Deserialization definition
Deserialization is the process of using the serialized state information
to reconstruct the object from the serialized state to its original state.


using System;
using System.Xml.Serialization;

namespace CodeGuru.Serialization
{
[XmlRoot("DataXml")]
public class XMLData
{
private int _Identity = 0;

private string _Name = "";
[XmlElement("Name")]

public string Name
{
get { return this._Name; }
set { this._Name = value; }
}

private string _Address = "";
[XmlAddress]

public string Address
{
get { return this._Address; }
set { this._Address = value; }
}

public XMLData()
{
}
}
}


Serialization


protected void XmlSerializerData()
{
XMLData obj = new XMLData();
obj.Name = "Aamir";
obj.IgnoreMe = "office";
XmlSerializer serializer = new XmlSerializer(obj.GetType());
Stream stream = new FileStream("c:\\MyFileName.xml", FileMode.Create,
FileAccess.Write, FileShare.None);
serializer.Serialize(stream, obj);
stream.Close();
}


Deserialization

public void XmlDeserialize(string XMLPath)
{
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(XMLData));
FileStream fs = new FileStream(XMLPath, FileMode.Open);
object obj = xmlSer.Deserialize(fs);
fs.Close();
XMLData dataObj = (XMLData)obj

}




http://msdn.microsoft.com/en-us/library/ms731073.aspx



Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, CSharp , ,

User Name: Guest

Your Ip: 38.107.191.113
Time: