Metropolitan State University

ICS 325

Building Services on the Internet

 

Lab 19

Web Encryption

(Linux Redhat 7.1)

 

 

1.      Build the Client Side Encryption Method.

§         Encrypt Data send it to a PHP script to decrypt it.

§         Log onto "redhat.ics.metrostate.edu" using your class account, then do the following:

$ cd

$ cd public_html

$ mkdir lab19

$ cd lab19

§         Edit an HTML document called "encrypt.html" using pico with the following lines:

<html>

<head>

<title>JavaScripting Example</title>

  <script type="text/javascript">

      var str2encrypt,str2decrypt;

   var encryptedString, decryptedString;

   var strLen, charCodes;

      var encryptKey=2;

     

      function encryptData(){

        encryptedString='';

        str2encrypt = document.encryptionForm.rawData.value;

         strLen = str2encrypt.length;

        for (i = 0; i < strLen; i++){

            charCodes = str2encrypt.charCodeAt(i);

            charCodes = charCodes * encryptKey;

            encryptedString = encryptedString + String.fromCharCode(charCodes);

         }   

        document.encryptionForm.encData.value = encryptedString;

      } 

 

      function decryptData(){

        decryptedString='';

        str2decrypt = document.encryptionForm.encData.value ;

        strLen = str2decrypt.length;

        for (i = 0; i < strLen; i++){

            charCodes = str2decrypt.charCodeAt(i);

            charCodes = charCodes / encryptKey;

            decryptedString = decryptedString + String.fromCharCode(charCodes);

        }   

        document.encryptionForm.decData.value = decryptedString;

      }

</script>

</head>

 

<body>

<h1>JavaScript Web Encryption</h1>

   <h3><b>NOTE:</b> Please enter lower case letters or numbers!</h3>

   <form name="encryptionForm" action="decrypt.php" method="post">

      <table cols=2>

         <tr>

            <td>Enter Phrase to Encrypt:</td>

            <td><input type="text" name="rawData" size="50"></td>

         </tr>

         <tr>

            <td colspan=2><input type="button" onclick="encryptData();"

value="Encrypt Message"><br></td>

         </tr>

         <tr>

            <td>Encrypted Message:</td>

            <td><input type="text" name="encData" size="50"><br></td>

         </tr>

         <tr>

      <td colspan=2><input type="button" onclick="decryptData();"

value="Decrypt Message"><br></td>

         </tr>

         <tr>

           <td>Orignal Message:</td>

           <td><input type="text" name="decData" size="50"><br></td>

        </tr>

      </table>

<input type="submit" value="submit" name="submit">

</form>

</body>

</html>

 

§         Create the PHP Code to decrypt the message.

$ cd

$ cd public_html

$ cd lab19

§         Edit an PHP document called "decrypt.php" using pico with the following lines:

 

<?php

   if(isset($submit)){

      $stLen = strlen($encData);

      $encryptKey = 2;

      for ($i = 0; $i < $stLen; $i++)  {

         $encChar = ord(substr($encData,$i,1));

         $encChar = $encChar / $encryptKey;

         $decString = $decString . chr($encChar);

      }                       

   echo "Message before Decryption = $encData<br />";

      echo "Message after Decryption = $decString<br />";

   }

?>

 

§         Invoke IE 5 on your machine, and check your html file with the following address:

http://redhat.ics.metrostate.edu/~su04325??/lab19/encrypt.html