Encrypt/Decrypt String in C#

using System.Security.Cryptography;
using System.IO;

static byte[] keys = ASCIIEncoding.ASCII.GetBytes("friction");

public static string StringEncrypt(string originalString)
        {

            if (String.IsNullOrEmpty(originalString))
            {
                throw new ArgumentNullException
                       ("The string which needs to be encrypted can not be null.");
            }
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                cryptoProvider.CreateEncryptor(keys, keys), CryptoStreamMode.Write);
            StreamWriter writer = new StreamWriter(cryptoStream);
            writer.Write(originalString);
            writer.Flush();
            cryptoStream.FlushFinalBlock();
            writer.Flush();
            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }


public static string StringDecrypt(string cryptedString)
        {
            if (String.IsNullOrEmpty(cryptedString))
            {
                throw new ArgumentNullException
                   ("The string which needs to be decrypted can not be null.");
            }
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream
                    (Convert.FromBase64String(cryptedString));
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                cryptoProvider.CreateDecryptor(keys, keys), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream);
            return reader.ReadToEnd();
        }

using Statement in C#

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object


Example:

using (Font font1 = new Font("Arial", 10.0f))
{
byte charset = font1.GdiCharSet;
}

You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler.

{
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
((IDisposable)font1).Dispose();
}
}

Multiple instances of a type can be declared in a using statement.

using (Font font3 = new Font("Arial", 10.0f),
font4 = new Font("Arial", 10.0f))
{
// Use font3 and font4.
}

Excel Workbook to XML or Database

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace ReadExcelFile
{
class Program
{
static void Main(string[] args)
{
OleDbConnection connExcelFile = new OleDbConnection();
DataTable dt = null;
DataTable tblFileTables;
string currentTbl = string.Empty;
int tblCount = 0;
connExcelFile.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=fileName.xls;Extended Properties=\"EXCEL 8.0;HDR=NO\";";
try
{
connExcelFile.Open();
dt = connExcelFile.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
tblFileTables = connExcelFile.GetSchema("Tables");
tblCount = tblFileTables.Rows.Count;
if (!(dt == null))
{
foreach (DataRow dr in dt.Rows)
{
currentTbl = dr["TABLE_NAME"].ToString();
if (string.IsNullOrEmpty(currentTbl))
{
continue;
}
currentTbl.Replace("'", "");
if (currentTbl.EndsWith("$'") || currentTbl.EndsWith("$"))
{
string testString = "Select * FROM [" + currentTbl + "]";
OleDbCommand cmd = new OleDbCommand(testString, connExcelFile);
try
{
//// If the query can not execute due to sheet error igore the sheet.
//// Else add to sheet list.
OleDbDataAdapter tblAdpt = new OleDbDataAdapter(cmd);
tblAdpt.Fill(tblFileTables);
tblFileTables.WriteXml(".\\" + currentTbl + ".xml");
break;
}
catch (InvalidOperationException e)
{
throw e;
}
catch (OleDbException e)
{
throw e;
// If sheet invalid ignore sheet.
}
}

}
}


}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}