Sunday, October 9, 2011

Data Encription and Decription in C#.Net

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Text;

///
/// Summary description for crypto
///

public class Crypto
{
private static byte[] _salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");

///
/// Encrypt the given string using AES. The string can be decrypted using
/// DecryptStringAES(). The sharedSecret parameters must match.
///

/// The text to encrypt.
/// A password used to generate a key for encryption.
public static string EncryptStringAES(string plainText, string sharedSecret)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");

string outStr = null; // Encrypted string to return
RijndaelManaged aesAlg = null; // RijndaelManaged object used to encrypt the data.

try
{
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);

// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{

//Write all data to the stream.
swEncrypt.Write(plainText);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}

// Return the encrypted bytes from the memory stream.
return outStr;
}

///
/// Decrypt the given string. Assumes the string was encrypted using
/// EncryptStringAES(), using an identical sharedSecret.
///

/// The text to decrypt.
/// A password used to generate a key for decryption.
public static string DecryptStringAES(string cipherText, string sharedSecret)
{
if (string.IsNullOrEmpty(cipherText))
throw new ArgumentNullException("cipherText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");

// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;

// Declare the string used to hold
// the decrypted text.
string plaintext = null;

try
{
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);

// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
byte[] bytes = Convert.FromBase64String(cipherText);
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))

// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}

return plaintext;
}
}

Thursday, September 8, 2011

Multiple Item Drag and Drop Between Tow ListBox

Private Sub lbAll_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbAll.MouseDown
'If lbAll.Items.Count = 0 Then
' Return
'End If
'Dim s As String = lbAll.Items(lbAll.IndexFromPoint(e.X, e.Y)).ToString()
'Dim dde1 As DragDropEffects = DoDragDrop(s, DragDropEffects.All)

'If dde1 = DragDropEffects.All Then
' 'lbAll.Items.RemoveAt(lbAll.IndexFromPoint(e.X, e.Y))
'End If
lbAll.DoDragDrop(lbAll.SelectedItems, DragDropEffects.Move)
End Sub

Private Sub lbSelected_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lbSelected.DragDrop
'If e.Data.GetDataPresent(DataFormats.StringFormat) Then
' Dim str As String = DirectCast(e.Data.GetData(DataFormats.StringFormat), String)
' lbSelected.Items.Add(str)
'End If

For Each item As Object In (e.Data.GetData(GetType(ListBox.SelectedObjectCollection)))
lbSelected.Items.Add(item)
Next
End Sub

Private Sub lbSelected_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lbSelected.DragOver
' e.Effect = DragDropEffects.All
If (e.Data.GetDataPresent(GetType(ListBox.SelectedObjectCollection))) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub

Monday, September 5, 2011

Connection String for .xls and .xlsx

MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & G_FilePath & "'; Extended Properties=Excel 8.0;")
Else
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & G_FilePath & "';Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2""")
End If

Tuesday, August 30, 2011

Mathemetical calculation in Asp.Net GridView inner text Box






onclick="javascript:calc();" onkeyup="javascript:calc();" onBlur="javascript:calc();"

Tuesday, August 23, 2011

Send mail in vb.net ,desktop applicaton

Dim Mail As New MailMessage()
Mail.To.Add(New MailAddress("tomail@gmail.com"))
Mail.From = (New MailAddress("frommail@gmail.com"))
Mail.Subject = "Mail subject"
Mail.Body = "Mail body"
Mail.IsBodyHtml = True
Dim smtpClient As New SmtpClient("smtp.gmail.com")
smtpClient.Host = "smtp.gmail.com"
smtpClient.Port = 587
smtpClient.EnableSsl = True
Dim userpass As New Net.NetworkCredential("frommail@gmail.com", "frommail_password")
smtpClient.Credentials = userpass
smtpClient.Send(Mail)



MessageBox.Show("Mail Send")

Friday, August 19, 2011

Get current rowindes from row command

CommandArgument='<%# Container.DataItemIndex %>'

Thursday, August 18, 2011

get gridview rowindex commandargument



delete a perticular row by rowindex

protected void grdRoomNumber_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
grdRoomNumber.DeleteRow(Convert.ToInt32(e.CommandArgument));
}
}