Background Image
Web
Development
Tools
Tool...
49 Projects
Web Development Project
Web
Development
Project
Project: Crypt
Creator:Graphix
Project type:Games
Created on:2010-10-13 15:46:41
Last edit:2010-11-17 16:18:52
Download:


Interface

The interface consists out of the following elements:

  • A textarea that needs to be hashed

  • Hash method selection (md5 and sha1)

  • Button that submits the hash

  • A textarea that needs to be encrypted

  • Encryption method

  • Key input text

  • Button that submits encryption

  • A textarea that needs to be decrypted

  • Decryption method

  • Key input text

  • Button that submits decryption


Programming

We start off with the interface:

Code Snippet (Syntax highlighted) - Toggle Wrap

<form action="Crypt.php" method="post">
<table cellspacing='20'>
<tr>
<td style='vertical-align:top;'>
<h2>Hash</h2>
Text that needs to be hashed:<br />
<textarea name='text_to_be_hashed' cols='20' rows='5'><?php echo $text_to_be_hashed; ?></textarea><br /><br />
<select name='hash_type'>
<option value='md5'>MD5</option>
<option value='sha1'>SHA1</option>
</select>
<input type='submit' name='subm_hash' value='Calculate hash' /><br /><br />
Hash of text:<br />
<input type='text' value='<?php echo $text_hash; ?>' name='text_hash' /><br />
</td>
<td>
<h2>Encryption</h2>
Text to be encrypted:<br />
<textarea name='text_to_be_enc' cols='20' rows='5'><?php echo $text_to_be_enc; ?></textarea><br /><br />
Key: <input type='text' name='enc_key' value='<?php echo $enc_key; ?>' /><br /><br />
<select name='enc_type'>
<option value='rijndael'>Rijndael</option>
</select>
<input type='submit' name='subm_enc' value='Encrypt' /><br /><br />
Encrypted text: <br />
<textarea name='text_enc' cols='20' rows='5'><?php echo $text_enc; ?></textarea><br />
<br />
</td>
<td>
<h2>Decryption</h2>
Text to be decrypted:<br />
<textarea name='text_to_be_dec' cols='20' rows='5'><?php echo $text_to_be_dec; ?></textarea><br /><br />
Key: <input type='text' name='dec_key' value='<?php echo $dec_key; ?>' /><br /><br />
<select name='dec_type'>
<option value='rijndael'>Rijndael</option>
</select>
<input type='submit' name='subm_dec' value='Decrypt' /><br /><br />
Decrypted text: <br />
<textarea name='text_dec' cols='20' rows='5'><?php echo $text_dec; ?></textarea><br />
<br />
</td>
</tr>
</table>
</form>


Now we start with the PHP, you might notice the echo of variables in the interface, so we will first create the code that creates the form values:

Code Snippet (Syntax highlighted) - Toggle Wrap

/* Setting form values, if they are not send as form values: */

/* Hash form values: */
$text_to_be_hashed = (isset($_POST['text_to_be_hashed'])) ? $_POST['text_to_be_hashed'] : "";
$text_hash = (isset($_POST['text_hash'])) ? $_POST['text_hash'] : "";
$hash_type = (isset($_POST['hash_type'])) ? $_POST['hash_type'] : "";

/* Encryption form values: */
$text_to_be_enc = (isset($_POST['text_to_be_enc'])) ? $_POST['text_to_be_enc'] : "";
$text_enc = (isset($_POST['text_enc'])) ? $_POST['text_enc'] : "";
$enc_key = (isset($_POST['enc_key'])) ? $_POST['enc_key'] : "";
$enc_type = (isset($_POST['enc_type'])) ? $_POST['enc_type'] : "";

/* Decryption form values: */
$text_to_be_dec = (isset($_POST['text_to_be_dec'])) ? $_POST['text_to_be_dec'] : "";
$text_dec = (isset($_POST['text_dec'])) ? $_POST['text_dec'] : "";
$dec_key = (isset($_POST['dec_key'])) ? $_POST['dec_key'] : "";
$dec_type = (isset($_POST['dec_type'])) ? $_POST['dec_type'] : "";


Now we start with the simple hash calculating code, using the build-in hash function md5() and sha1() :

Code Snippet (Syntax highlighted) - Toggle Wrap

if (isset($_POST['subm_hash'])) {

/* Calculating hash depending on hash type: */

if ($hash_type == 'md5') {
$text_hash = md5($text_to_be_hashed);
} else if ($hash_type == 'sha1') {
$text_hash = sha1($text_to_be_hashed);
}

} /* Here the encryption code is added (see next code) */


Now the encryption code, every step is commented and is self-explainatory:

Code Snippet (Syntax highlighted) - Toggle Wrap

else if (isset($_POST['subm_enc'])) {

/* Calculating encryption text depending on type: */

if ($enc_type == 'rijndael') {

// Create the key:
$key = $enc_key;

// Set the data:
$data = $text_to_be_enc;

// Open the cipher:
// Using Rijndael 256 in CBC mode.
$m = mcrypt_module_open('rijndael-256', '', 'cbc', '');

// Initialize the encryption:
mcrypt_generic_init($m, $key, "00000000000000000000000000000000");

// Encrypt the data:
if ($data != "") {
$data = mcrypt_generic($m, $data);
}

// Close the encryption handler:
mcrypt_generic_deinit($m);

// Close the cipher:
mcrypt_module_close($m);

// Returning the encrypted data:
$text_enc = base64_encode($data);

}

}


The decryption code is almost the same, but only mdecrypt_generic is used instead of mcrypt_generic:

Code Snippet (Syntax highlighted) - Toggle Wrap

else if (isset($_POST['subm_dec'])) {

if ($enc_type == 'rijndael') {

// Create the key:
$key = $dec_key;

// Set the data:
$data = $text_to_be_dec;

// Open the cipher:
// Using Rijndael 256 in CBC mode.
$m = mcrypt_module_open('rijndael-256', '', 'cbc', '');

// Initialize the encryption:
mcrypt_generic_init($m, $key, "00000000000000000000000000000000");

// Decrypt the data:
if ($data != "") {
$data = mdecrypt_generic($m, base64_decode($data));
}

// Close the encryption handler:
mcrypt_generic_deinit($m);

// Close the cipher:
mcrypt_module_close($m);

$text_dec = trim($data);

}

}


And that concludes the Crypt program. You can freely use the above code to implement it into your own encryption and decryption functions.

This code in action

You can download the sourcecode using the link below:
http://www.webdevproject.com/zips/Crypt.zip
Comments
Latest news
New project: Math Quiz
2010-11-09 18:07:48
Read more...

New project: Custom Messagebox
2010-11-06 19:10:50
Read more...

New project: Visitor Counter
2010-11-01 21:39:04
Read more...

New project: Image Editor
2010-11-01 21:37:27
Read more...

New project: Image Generator
2010-11-01 21:33:05
Read more...

Projects Contact © 2010 WDP - All rights reserved Terms and Conditions Log in