Saturday, April 27, 2013

Public Key Encryption and Digital Signature: How do they work?



I recently had a task which involves Encryption and Digital Signature. This paper from CGI.com is one of the things that helped me understand how public key encryption and digital signatures work.

http://www.cgi.com/files/white-papers/cgi_whpr_35_pki_e.pdf

I hope this also will help you.

Monday, April 8, 2013

My solution to chapter 12 exercises of JavaScript Step by Step 2e

1. Create a webpage that sends a cookie to the browser. Set the expiration date ahead
one day. Verify that the JavaScript code sent the cookie to the browser by viewing it as
it gets set or after it’s been stored on the computer. You could accomplish this second
part of the exercise; by using JavaScript or by viewing the cookies on the computer.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookieName = "mycookie-1";
        var cookieValue = "mycookievalue-1";
        var date = new Date();
        date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
        var expireDate = date.toGMTString();

        var myCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate;
        document.cookie = myCookie;

        // the following code is not part of exercise 1
        // another cookie
        var cookieName2 = "mycookie-another";
        var cookieValue2 = "mycookievalue-another";
        var date2 = new Date();
        date2.setTime(date2.getTime() + (24 * 60 * 60 * 1000));
        var expireDate2 = date2.toGMTString();

        var myCookie2 = cookieName2 + "=" + cookieValue2 + ";expires=" + expireDate2 + ";path=/;domain=localhost;";
        document.cookie = myCookie2;
       
        alert(document.cookie);
    </script>
</body>
</html>


2. Create a webpage that sends a cookie with the cookie’s expiration date set ahead one
week, and set the secure flag. This page can be the same one you created for Exercise
1, but be sure to give the cookie a different name so that you’ve created two separate
cookies, one for each exercise. Also, be sure to enable the secure flag for the cookie in
this exercise, not for the cookie in Exercise 1.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookieName = "mycookie-2";
        var cookieValue = "mycookievalue-2";
        var date = new Date();
        date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
        var expireDate = date.toGMTString();

        var myCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate + ";secure";
        document.cookie = myCookie;
    </script>
</body>
</html>

3. Create a webpage that attempts to read the cookie with the secure flag set. Did you
receive the cookie? If not, what would you need to do to receive it?

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookies = document.cookie.split(";");
        var cookLength = cookies.length;
        for (var c = 0; c < cookLength; c++) {
            alert(cookies[c]);
        }
    </script>
</body>
</html>

4. Create a webpage that reads the cookie you created in Exercise 1. Use a for loop and an
if conditional to display an alert() dialog box when the cookie with the correct name is
found within the loop. Don’t display an alert() dialog box for any other cookies.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookies = document.cookie.split(";");
        var cookLength = cookies.length;
        for (var c = 0; c < cookLength; c++) {
            var pairs = cookies[c].split("=");
            var cookieName = pairs[0];
            var cookieValue = pairs[1];

            // display only the cookie with name of "mycookie-1"
            if (pairs[0] === "mycookie-1") {
                alert("Name: " + cookieName + " -> " + "Value: " + cookieValue);
            }
        }
    </script>
</body>
</html>

My solution to chapter 11 exercises of JavaScript Step by Step 2e

1. Create a webpage that contains an onclick event handler connected to a link using a
DOM 0 inline event. The event handler should display an alert stating “You Clicked
Here”.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <a href="#" onclick="alert('You Clicked Here');">This is a link</a>
</body>
</html>


2. Change the webpage created in Exercise 1 to use the newer style of event handling
shown in ehandler.js (in the companion content) and connect the same click/onclick
event to display the alert created in Exercise 1.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../../Scripts/ehandler.js" type="text/javascript"></script>
</head>
<body>
    <a id="link1" href="#">This is a link</a>

    <script type="text/javascript">
        function showAlert() {
            alert("You Clicked Here");
        };
        var link1 = document.getElementById("link1");
        EHandler.add(link1, "click", showAlert);       
    </script>
</body>
</html>


3. Create a webpage with a link to http://www.microsoft.com. Make that link open in a
new tab.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <a href="http://www.microsoft.com." target="_blank">http://www.microsoft.com</a>
</body>
</html>