Posts Tagged .Net

Encrypting and decrypting data

I am seeing a lot of questions people are asking on how to do encryption/decryption. To help those people I have written a simple class encorporating several encryption/decryption functions:

  • byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV) – encrypts a byte array with a key and an IV and returns a byte array;
  • string Encrypt(string clearText, string Password) – encrypts a string with a password and returns a string;
  • byte[] Encrypt(byte[] clearData, string Password) - encrypts a byte array with a password and returns a byte array;
  • void Encrypt(string fileIn, string fileOut, string Password) – encrypts a file with a password and writes the encrypted bytes into another file.

For each of those there is also a corresponding Decrypt function. The Main method is a simple testing method that exercises some of those functions. The 2nd and the 3rd Encrypt functions call into the 1st function, so you will need to carry the 1st one around if you are using the 2nd or the 3rd. The last Encrypt function (the one that works with files) is standalone. I made it operate in a stream-like manner, without reading the whole file into memory, which makes it possible to encrypt/decrypt gigabytes of data without going out of memory space.

I am using Rijndael algorithm in this sample. The reason for this is that it is 100% implemented in managed code in our libraries, so it does not rely on CryptoAPI or any encryption packs and will work everywhere. If you need performance I would suggest replacing it with TripleDES (it is a one line change), and if you do, also do not forget to change the IV size to 8 bytes and the Key size to 16 bytes.

I have tried to document the code well, and I would like to encourage you to read through it and understand how it works, it should be pretty easy. You can also grab the whole thing, stick it into a .cs file and it should compile. If you run it you will see it make some test encryption/decryption roundtrip; you can also provide a file name as a parameter, and it will encrypt the file into a <name>.encrypted file and then decrypt it back into a <name>.decrypted.

Enjoy!

(more…)

Add comment April 16, 2009

Encrypt / Decrypt Section in Configuration File

Lets understand Encrypt and Decrypt particular section in web.config file with example of connectionstring encrypt in web.config file.
(more…)

Add comment January 18, 2008

URL Mapping in asp.net 2.0

URL Mapping is a mechanism by which you can change the displayed url in address bar.

Example:

Your asp.net application is developed from years, with convention frm as prefix to webform.

(more…)

Add comment January 18, 2008

Sending Emails with Dynamic Content

Introduction
This article explains how to send email from asp.net with dynamic contents.
Description
There are lots and lots of requirements come in our day to day life of web application development. One thing which we do most is sending notification emails to users of our website. In case, if your website is an e-commerce system, then you have to send emails such as customer registration confirmation emails, invoice emails and even payment confirmation emails. So I took my time to explain how to send emails with dynamic contents through asp.net 2.0.
System.Web.Mail Namespace

The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server. The classes in this namespace can be used from ASP.NET or from any managed application.
System.IO Namespace
The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.
These two namespaces plays a major role in sending emails with dynamic content. I want the message of the email to like
Hi ,

Thank you for sending this email. See you soon.

Good Bye,

So open a web project in Microsoft Visual Studio, obviously you can see a Default.aspx page. Add one html file named MyContent.html. Copy and paste the above message to the html file. Then come to Default.aspx file, add two textboxes and rename it as txtMyName and txtFriendName. Add a button and rename it as cmdSendEmail and change the Button’s Text to Send Email.

Now in Default.aspx.cs, include those two namespaces as follows.
using System.Web.Mail;
using System.IO;
Create an object for StreamReader class by assigning the html file path. Then open the file with File.OpenText method. So the content of the html file will be read and stored in the result variable. Now you can find and replace the content by the values in the Textboxes using the asp.net ‘Replace’ method and store the resultant content to another variable called MessageBody. So the email is ready with dynamic contents.

Now we create an object to the MailMessage as mail. Set the BodyFormat property to MailFormat.Html. This tells the mail object, you are going to send a Html based content message. Then set the From, To, Body and the Subject properties of the mail object. Point the SmtpMail.SmtpServer to your mail server. In most cases, it will be ‘localhost’. The last line of the code, triggers the email from your application to the target mail server.
try
{
StreamReader sr=new StreamReader(“MyContent.htm”);
sr = File.OpenText(“MyContent.htm”);
string result = sr.ReadToEnd();
sr.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
return;
}

string MessageBody=result.Replace(“”, txtFriendName.Text);
MessageBody=MessageBody.Replace(“”, txtMyName.Text);
MessageBody= MessageBody.Replace(“”, DateTime.Today.ToString());
MailMessage mail = new MailMessage();
mail.Body = MessageBody;
mail.BodyFormat = MailFormat.Html;
mail.From = “YOUR EMAIL ID”;
mail.To = “YOUR FRIEND’S EMAIL ID”;
mail.Subject = “Dynamic Content Email From “+ txtMyName.Text;
SmtpMail.SmtpServer = “your email server”;
SmtpMail.Send(mail);
So you can use the above block of code to send emails from your web applications. You can design any type of html content and replace the necessary dynamic values from the code-behind. Hope this small article will help you lot.

http://www.aspdotnetcodes.com

1 comment December 20, 2007


Categories

Category Cloud

.Net Business HTML & CSS & DOM Javascript PHP Tool

Tags

.Net ADO.NET ajax Business Javascript Oracle ADO.NET Subversion .Net Subversion SVN Source VB.Net

Recent Posts

Archives

Blogroll

Recent Comments

Asaduzzaman Arif on Encrypt/Decrypt string VB…
Pranav on Encrypt/Decrypt string VB…
ntcnet on Encrypt/Decrypt string VB…
Elena on Encrypt/Decrypt string VB…
Elena on Encrypt/Decrypt string VB…

Twitter