The Caesar cipher, named after Julius Caesar who is believed to have used it for secure communication, is a simple and very old encryption technique. It is a type of substitution cipher where each letter in the plaintext is shifted a certain number of places down or up the alphabet. The shift value is known as the cipher key.
Here's how the Caesar cipher works:
- Key Selection: The sender and receiver agree on a key value, which is an integer between 1 and 25. The key determines the number of positions each letter in the plaintext is shifted.
- Encryption: Each letter in the plaintext is shifted by the key value. If the key is, for example, 3, then 'A' becomes 'D,' 'B' becomes 'E,' 'C' becomes 'F,' and so on. When reaching the end of the alphabet, the shift wraps around to the beginning.
- Decryption: To decrypt the ciphertext and recover the original plaintext, the receiver uses the same key but applies the reverse shift. If the key is 3, then 'D' becomes 'A,' 'E' becomes 'B,' 'F' becomes 'C,' and so on.
The Caesar cipher is relatively easy to understand and implement, but it is also very insecure. Since there are only 25 possible keys (excluding the key value of 0, which results in no encryption), it can be easily broken through brute-force attacks. Modern cryptographic techniques are much more secure and complex, making the Caesar Cipher more of a historical curiosity rather than a practical encryption method for sensitive data.
References
Demo
To encrypt a message, enter the message in the Plaintext textbox, specify the shift, and click Encrypt. To decrypt a message, enter the ciphertext in the Ciphertext textbox, specify the shift, and click Decrypt. Note that, in this implementation, strings are converted to upper case before encryption/decryption, and spaces and punctuation marks are not encrypted/decrypted.
Using this tool try to decrypt the following message: SDQMF VAN, KAG NDAWQ FTQ OAPQ!
PHP Implementation of This Demo
To implement the simple demo shown on this page on your own website, follow these simple steps. First, paste the following PHP script at the beginning of your page, after the<body>
tag.
<?php
// Check if a request was submitted
if ( isset( $_POST[ 'generate' ] ) ) {
$text = $_POST[ 'text' ];
$algorithm = $_POST[ 'algorithm' ];
// Validate the selected algorithm
$allowed_algorithms = [ 'MD5' => 'md5', 'SHA-1' => 'sha1', 'SHA-256' => 'sha256', 'SHA-512' => 'sha512' ];
if ( !in_array( $algorithm, array_keys( $allowed_algorithms ) ) ) {
$html = 'Invalid algorithm selected!
';
} else {
// Generate the hash
$hash = hash( $allowed_algorithms[ $algorithm ], $text );
$html = '' . $algorithm . ' Hash
: ' . $hash . '';
}
}
?>
Next, place the following code where you would like the form to be shown.
<form action="" method="post" name="caesar" id="caesar">
<strong>Plaintext</strong>
<input name="plaintext" type="text" id="plaintext" size="30" value="<?php echo $plaintext; ?>">
<input name="Command" type="submit" id="Command" value="Encrypt">
<strong>Shift</strong>
<select id="shift" name="shift">
<!-- Loop to generate options from 0 to 25 -->
<?php
for ($i = 0; $i <= 25; $i++) {
$selected = ($i == $shift) ? " selected" : "";
echo "<option value='$i'$selected>$i</option>";
}
?>
</select>
<strong>Ciphertext</strong>
<input name="ciphertext" type="text" id="ciphertext" size="30" value="<?php echo $ciphertext; ?>">
<input name="Command" type="submit" id="Command" value="Decrypt">
</form>