You are on page 1of 18

PROJECT

Design Website For Shopping Using Asp.Net (C#) And Encrypt Personal Information Using RSA Algorithm

Completed by Firas M. Kadhum


Iraq - Baghdad

Description:
This project illustrates a very simple website for sell different types of mobile devices by request. So in short, after the customer fills all information to complete purchasing procedure, he/she will receive confirmation message which contains the most of information that have been entered, moreover, the encrypted information using RSA Algorithm to his /her e-mail that has already been entered in buy page. Figures and the script code show the project is fully.

The Figures:

Fig 1: Master Page (with Default Page)

Fig 2: About Page

Fig 3: Buy Page


3

Fig 4: Buy Page during the filling

Fig 5: Buy Page after confirm purchase


4

Fig 6: Confirmation Message

Fig 6: Contact Us Page


5

Fig7: Contact Us Page during the filling

The Code Asp.Net (C#):

Default Pages Code: using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ButtonBuy1_Click(object sender, EventArgs e) { Session.Add("Phone1", LabelPhone1.Text); Session.Add("Price1", LabelPrice1.Text); // Redirect to Buy.aspx page. Server.Transfer("Buy.aspx");

} protected void ButtonBuy2_Click(object sender, EventArgs e) { Session.Add("Phone2", LabelPhone2.Text); Session.Add("Price2", LabelPrice2.Text); // Redirect to Buy.aspx page. Server.Transfer("Buy.aspx");

Contact Us Pages Code:


using using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Net; System.Net.Mail;

public partial class ContactUs : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { MailMessage MailMsg = new MailMessage(); MailMsg.To.Add("YourID@gmail.com"); MailMsg.From = new MailAddress( txtYourEmail.Text); MailMsg.Subject = txtSubject.Text; // To send a message in multiple lines string message_c = "This is a message from the our contact page:"; MailMsg.Body = "<div style=\"font: 11px verdana, arial\">"; MailMsg.Body += message_c.Replace("\n", "<br/>")+"<br/><br/>"; MailMsg.Body += "<h3>The Message:</h3>"; MailMsg.Body += "<div style=\"font-size:11px;line-height:16px\">"; MailMsg.Body += txtMessage.Text; MailMsg.Body += "<hr /><br />"; MailMsg.Body += "<h3>Sender information:</h3>"; MailMsg.Body += "<div style=\"font-size:11px;line-height:16px\">"; MailMsg.Body += "<strong>Name :</strong> " + txtYourName.Text + "<br />"; MailMsg.Body += "<strong>E-mail :</strong> " + txtYourEmail.Text + "<br />"; MailMsg.Body += "<hr /><br />"; MailMsg.Body += "<h3>Attachment:</h3>"; MailMsg.Body += "<div style=\"font-size:11px;line-height:16px\">"; MailMsg.IsBodyHtml = true; // To upload file if ( FileUpload1.HasFile) { MailMsg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName)); } SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential("YourID@gmail.com","YourGmailPassword"); smtp.EnableSsl = true;

LabelR.Visible= true; try {

smtp.Send(MailMsg); LabelR.Text = " Dear " + txtYourName.Text + " , your message has been sent successfully"; txtYourName.Text = ""; txtYourEmail.Text = ""; txtSubject.Text = ""; txtMessage.Text = ""; } catch (Exception ex) { LabelR.Text = ex.Message; } } }

Design page:
<%@ Page Title="Contact Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ContactUs.aspx.cs" Inherits="ContactUs" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"> <style type="text/css"> .style46 { color: #FF0000; } .style57 { text-decoration: underline; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> <h2> Contact Us: </h2> <p><strong style="color: #0000FF; font-weight: bold"> Please,provide us your info. and message: <span class="style46"><strong style="font-weight: bold"> <span class="style57"> Note that</span>: * means required field.</strong></span></strong></p> <p> Your name:<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtYourName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> <br /> <asp:TextBox ID="txtYourName" runat="server"></asp:TextBox><br /> Your email address:<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtYourEmail" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> <br /> <asp:TextBox ID="txtYourEmail" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"

ControlToValidate="txtYourEmail" ErrorMessage="It must be: example@domain.com" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([.]\w+)*"></asp:RegularExpressionValidator> <br /> Subject:<br /> <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox><br /> Message:<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtMessage" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> <br /> <asp:TextBox ID="txtMessage" runat="server" Height="60px" TextMode="MultiLine" Width="300px"></asp:TextBox><br /> Attachment:<br /> <asp:FileUpload ID="FileUpload1" runat="server" /><br /> <br /> <asp:Button ID="Button1" runat="server" Text="Send" ToolTip="Send Message" onclick="Button1_Click" /><br /> <asp:Label ID="LabelR" runat="server" ForeColor="#3333FF" style="font-weight: 700"></asp:Label> </p> </asp:Content>

10

Buy Pages Code:


using using using using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Text; System.Security.Cryptography; System.Net; System.Net.Mail;

public partial class Buy : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Reads info into variables on the Buy.aspx page. //For first phone if (Session["Phone1"] != null) { LabelSitem.Text = (string)(Session["Phone1"]); LabelPrice.Text = (string)(Session["Price1"]); Session.Clear(); } //For second phone if (Session["Phone2"] != null) { LabelSitem.Text = (string)(Session["Phone2"]); LabelPrice.Text = (string)(Session["Price2"]); Session.Clear(); }

protected void ButtonCancel_Click(object sender, EventArgs e) { Response.Redirect("Default.aspx"); } protected void ButtonConfirmPurchase_Click(object sender, EventArgs e) { //============================================================================== // Code to Encrypt Credit Card Number using RSA Algorithm: //============================================================================== UnicodeEncoding bytConvertor = new UnicodeEncoding(); String plaintext = txtCCNumber.Text; // TextBox which has Credit Card Number byte[] plainData = bytConvertor.GetBytes(plaintext); RSACryptoServiceProvider RSAServiceProvider = new RSACryptoServiceProvider(); byte[] enData = Encrypt(plainData, RSAServiceProvider.ExportParameters(false)); String Encrypted_CreditCard_Number = bytConvertor.GetString(enData); //============================================================================== // Code to send Confirmation Message to customer's e-mail: //============================================================================== MailMessage MailMsg = new MailMessage(); MailMsg.To.Add(txtEmail.Text); MailMsg.From = new MailAddress("YourID@gmail.com"); MailMsg.Subject = "Confirmation Message";

11

string message_c = "Thanks for your purchase."; MailMsg.Body = "<div style=\"font: 11px verdana, arial\">"; MailMsg.Body += message_c.Replace("\n", "<br/>") + "<br/><br/>"; MailMsg.Body += "<h3>Purchasing information:</h3>"; MailMsg.Body += "<div style=\"font-size:11px;line-height:16px\">"; MailMsg.Body += "<strong>Item purchased :</strong> " + LabelSitem.Text + "<br />"; MailMsg.Body += "<strong>Price :</strong> " + LabelPrice.Text + "<br />"; MailMsg.Body += "<strong>Name :</strong> " + RBListStatus.SelectedItem.Text +" "+ txtLastName.Text +" "+ txtFirstName.Text + "<br />"; MailMsg.Body += "<strong>E-mail :</strong> " + txtEmail.Text + "<br />"; MailMsg.Body += "<strong>Country :</strong> " + DDListCountry.SelectedItem.Text + "<br />"; MailMsg.Body += "<strong>Address :</strong> " + txtAddress.Text + "," + txtCity.Text + " " + txtState.Text + "," + txtZPCode.Text + "<br />"; MailMsg.Body += "<strong>Mobile :</strong> " + txtMobile.Text + "<br />"; MailMsg.Body += "<hr /><br />"; MailMsg.Body += "<h3>Card information:</h3>"; MailMsg.Body += "<div style=\"font-size:11px;line-height:16px\">"; MailMsg.Body += "<strong>Credit Card Types:</strong> " + DDListCardTypes.SelectedItem.Text + "<br />"; MailMsg.Body += "<strong>Card holder name printed on card:</strong> " + txtCardHolderName.Text + "<br />"; MailMsg.Body += "<strong>Credit Card Number:</strong> " + Encrypted_CreditCard_Number + "<br />"; MailMsg.Body += "<strong>Card Expiration:</strong> " + txtCardEx.Text + "<br />"; MailMsg.Body += "<strong>Card Verification Code(3 digits):</strong> " + txtthreedigit.Text + "<br />"; MailMsg.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential("YourID@gmail.com", "YourGmailPassword"); smtp.EnableSsl = true; LabelR2.Visible = true; LabelR3.Visible = true; try { smtp.Send(MailMsg); LabelR2.Text = " Thank you!" + RBListStatus.SelectedItem.Text + " " + txtLastName.Text + " " + txtFirstName.Text + " ,your purchase requested has been submitted successfully"; LabelR3.Text = " Please,check your email for order confirmation.You may exit this page."; } catch (Exception ex) { LabelR2.Text = ex.Message; }

//============================================================================== // Encrypt Method //============================================================================== static private byte[] Encrypt(byte[] DataToEncrypt, RSAParameters keyInfo) { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.ImportParameters(keyInfo); return RSA.Encrypt(DataToEncrypt, false); } }

12

Design page:
<%@ Page Title="Buy" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Buy.aspx.cs" Inherits="Buy" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> <h2> Buy: </h2> <p><strong style="color: #0000FF; font-weight: bold"> Please , provide us your information: </strong> <span class="style46"><strong style="font-weight: bold"> <span class="style57"> Note that</span>: * means required field</strong></span></p> <table class="style2"> <tr> <td class="style59"> Selected item :</td> <td class="style60"> <asp:Label ID="LabelSitem" runat="server" style="color: #0000FF; font-weight: 700"></asp:Label> </td> <td class="style61"> Price :</td> <td> <asp:Label ID="LabelPrice" runat="server" style="color: #0000FF; font-weight: 700"></asp:Label> </td> <td> &nbsp;</td> </tr> </table> <p> <asp:RadioButtonList ID="RBListStatus" runat="server" RepeatDirection="Horizontal"> <asp:ListItem>Mr.</asp:ListItem> <asp:ListItem>Mrs.</asp:ListItem> <asp:ListItem>Miss.</asp:ListItem> <asp:ListItem>Ms.</asp:ListItem> </asp:RadioButtonList>

<table class="style2"> <tr> <td class="style43"> First Name:</td> <td class="style20"> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtFirstName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style47"> <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> &nbsp;</td> <td class="style32"> Last Name:</td> <td class="style44"> <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtLastName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>

13

</td> <td class="style22" colspan="3"> &nbsp;<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> </td> <td class="style19" colspan="2"> &nbsp;</td> <td> &nbsp;</td> </tr> <tr> <td class="style43"> E-mail Address:</td> <td class="style42"> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style47"> <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage=" ex@dom.com" ForeColor="Red" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([.]\w+)*"></asp:RegularExpressionValidator> </td> <td class="style32"> Country:</td> <td class="style44"> <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="DDListCountry" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style22" colspan="3"> &nbsp;<asp:DropDownList ID="DDListCountry" runat="server" Height="19px" Width="123px"> <asp:ListItem Selected="True"></asp:ListItem> <asp:ListItem>Afghanistan</asp:ListItem> <asp:ListItem>Albania</asp:ListItem> <asp:ListItem>Algeria</asp:ListItem> <asp:ListItem>Argentina</asp:ListItem> <asp:ListItem>Australia</asp:ListItem> <asp:ListItem>Bahrain</asp:ListItem> <asp:ListItem>Belgium</asp:ListItem> <asp:ListItem>Bolivia</asp:ListItem> <asp:ListItem>Brazil</asp:ListItem> <asp:ListItem>Cameroon</asp:ListItem> <asp:ListItem>Canada</asp:ListItem> <asp:ListItem>Chile</asp:ListItem> <asp:ListItem>China</asp:ListItem> <asp:ListItem>Egypt</asp:ListItem> <asp:ListItem>Estonia</asp:ListItem> <asp:ListItem>Finland</asp:ListItem> <asp:ListItem>France</asp:ListItem> <asp:ListItem>Georgia</asp:ListItem> <asp:ListItem>Germany</asp:ListItem> <asp:ListItem>Greece</asp:ListItem> <asp:ListItem>Haiti</asp:ListItem> <asp:ListItem>Honduras</asp:ListItem> <asp:ListItem>Hungary</asp:ListItem> <asp:ListItem>Iceland</asp:ListItem> <asp:ListItem>India</asp:ListItem> <asp:ListItem>Iraq</asp:ListItem> <asp:ListItem>Italy</asp:ListItem>

14

<asp:ListItem>Jamaica</asp:ListItem> <asp:ListItem>Japan</asp:ListItem> <asp:ListItem>Ukraine</asp:ListItem> <asp:ListItem>United Arab Emirates</asp:ListItem> <asp:ListItem>United Kingdom</asp:ListItem> <asp:ListItem>United States</asp:ListItem> <asp:ListItem>Uruguay</asp:ListItem> <asp:ListItem>Uzbekistan</asp:ListItem> <asp:ListItem>Venezuela</asp:ListItem> <asp:ListItem>Vietnam</asp:ListItem> <asp:ListItem>Yemen</asp:ListItem> <asp:ListItem>Zambia</asp:ListItem> <asp:ListItem>Zimbabwe</asp:ListItem> </asp:DropDownList> </td> <td class="style19" colspan="2"> &nbsp;</td> <td> &nbsp;</td> </tr> <tr> <td class="style43"> Address:</td> <td class="style42"> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAddress" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style47"> <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> </td> <td class="style32"> City:</td> <td class="style44"> <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txtCity" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style29"> &nbsp;<asp:TextBox ID="txtCity" runat="server" style="text-align: left"></asp:TextBox> </td> <td class="style30"> State:</td> <td class="style45"> <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtState" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style27"> <asp:TextBox ID="txtState" runat="server"></asp:TextBox> </td> <td class="style26"> &nbsp;</td> <td> &nbsp;</td> </tr> <tr> <td class="style43"> Zip/Post Code:</td> <td class="style42"> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"

15

ControlToValidate="txtZPCode" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style47"> <asp:TextBox ID="txtZPCode" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtZPCode" ErrorMessage="5d or 5d-4d " ForeColor="Red" ValidationExpression="^\d{5}(-\d{4})?$"></asp:RegularExpressionValidator> </td> <td class="style32"> Mobile:</td> <td class="style18"> &nbsp;</td> <td class="style29"> &nbsp;<asp:TextBox ID="txtMobile" runat="server"></asp:TextBox> </td> <td class="style30"> &nbsp;</td> <td class="style31"> &nbsp;</td> <td class="style27"> &nbsp;</td> <td class="style26"> &nbsp;</td> <td> &nbsp;</td> </tr> </table> <p> <strong>Card information:</strong></p> <table class="style2"> <tr> <td class="style58"> Credit Card Types:</td> <td class="style42" colspan="2"> <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="DDListCardTypes" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style53"> <asp:DropDownList ID="DDListCardTypes" runat="server"> <asp:ListItem Selected="True"></asp:ListItem> <asp:ListItem>MasterCard</asp:ListItem> <asp:ListItem>Visa</asp:ListItem> <asp:ListItem>Visa Electron</asp:ListItem> <asp:ListItem>China UnionPay</asp:ListItem> <asp:ListItem>Other</asp:ListItem> </asp:DropDownList> </td> <td class="style55"> Card Expiration:</td> <td class="style44"> <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="txtCardEx" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style48"> <asp:TextBox ID="txtCardEx" runat="server"></asp:TextBox> </td> <td class="style56"> <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"

16

ControlToValidate="txtCardEx" ErrorMessage="MM/YY" ForeColor="Red" ValidationExpression="^((0[1-9])|(1[02]))\/(\d{2})$"></asp:RegularExpressionValidator> </td> <td class="style31"> &nbsp;</td> <td class="style27"> &nbsp;</td> <td class="style26"> &nbsp;</td> <td> &nbsp;</td> </tr> <tr> <td colspan="2"> Card holder name printed on card:</td> <td class="style46"> <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="txtCardHolderName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style53"> <asp:TextBox ID="txtCardHolderName" runat="server"></asp:TextBox> </td> <td class="style55"> &nbsp;</td> <td class="style18"> &nbsp;</td> <td class="style48"> &nbsp;</td> <td class="style56"> &nbsp;</td> <td class="style31"> &nbsp;</td> <td class="style27"> &nbsp;</td> <td class="style26"> &nbsp;</td> <td> &nbsp;</td> </tr> <tr> <td colspan="2"> Credit Card Number:</td> <td class="style46"> <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="txtCCNumber" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> <td class="style53"> <asp:TextBox ID="txtCCNumber" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txtCCNumber" ErrorMessage="16d or Less 16d" ForeColor="Red" ValidationExpression="^[0-9]{1,16}$"></asp:RegularExpressionValidator> </td> <td class="style55"> Card Verification Code (3digit):</td> <td class="style44"> <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" ControlToValidate="txtthreedigit" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>

17

</td> <td class="style48"> <asp:TextBox ID="txtthreedigit" runat="server"></asp:TextBox> </td> <td class="style56"> <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ControlToValidate="txtthreedigit" ErrorMessage="3d" ForeColor="Red" ValidationExpression="\d{3}"></asp:RegularExpressionValidator> </td> <td class="style31"> &nbsp;</td> <td class="style27"> &nbsp;</td> <td class="style26"> &nbsp;</td> <td> &nbsp;</td> </tr> </table> <br /> <table class="style2"> <tr> <td class="style40"> &nbsp;</td> <td class="style39"> <asp:Button ID="ButtonConfirmPurchase" runat="server" style="font-weight: 700" Text="Confirm Purchase" ToolTip="Click To Confirm Purchase" onclick="ButtonConfirmPurchase_Click" /> </td> <td class="style41"> <asp:Button ID="ButtonCancel" runat="server" Height="26px" style="font-weight: 700" Text="Cancel" ToolTip="Click To Cancel" Width="157px" onclick="ButtonCancel_Click" CausesValidation="False" /> </td> <td> &nbsp;</td> </tr> <tr> <td class="style38" colspan="3"> <asp:Label ID="LabelR2" runat="server" ForeColor="#3333FF" style="font-weight: 700"></asp:Label> </td> <td> &nbsp;</td> </tr> <tr> <td class="style38" colspan="3"> <asp:Label ID="LabelR3" runat="server" ForeColor="Red" style="font-weight: 700"></asp:Label> </td> <td> &nbsp;</td> </tr> </table> </p> </asp:Content>

18