Last active
June 12, 2023 04:48
-
-
Save noorxbyte/27f989fc5d1a0ee10b7f to your computer and use it in GitHub Desktop.
PHP Vigenere Cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// initialize variables | |
$pswd = ""; | |
$code = ""; | |
$error = ""; | |
$valid = true; | |
$color = "#FF0000"; | |
// if form was submit | |
if ($_SERVER['REQUEST_METHOD'] == "POST") | |
{ | |
// declare encrypt and decrypt funtions | |
require_once('vigenere.php'); | |
// set the variables | |
$pswd = $_POST['pswd']; | |
$code = $_POST['code']; | |
// check if password is provided | |
if (empty($_POST['pswd'])) | |
{ | |
$error = "Please enter a password!"; | |
$valid = false; | |
} | |
// check if text is provided | |
else if (empty($_POST['code'])) | |
{ | |
$error = "Please enter some text or code to encrypt or decrypt!"; | |
$valid = false; | |
} | |
// check if password is alphanumeric | |
else if (isset($_POST['pswd'])) | |
{ | |
if (!ctype_alpha($_POST['pswd'])) | |
{ | |
$error = "Password should contain only alphabetical characters!"; | |
$valid = false; | |
} | |
} | |
// inputs valid | |
if ($valid) | |
{ | |
// if encrypt button was clicked | |
if (isset($_POST['encrypt'])) | |
{ | |
$code = encrypt($pswd, $code); | |
$error = "Text encrypted successfully!"; | |
$color = "#526F35"; | |
} | |
// if decrypt button was clicked | |
if (isset($_POST['decrypt'])) | |
{ | |
$code = decrypt($pswd, $code); | |
$error = "Code decrypted successfully!"; | |
$color = "#526F35"; | |
} | |
} | |
} | |
?> | |
<html> | |
<head> | |
<title>XBCrypt - Vigenere Cipher</title> | |
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" /> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
<script type="text/javascript" src="Script.js"></script> | |
</head> | |
<body> | |
<br><br><br> | |
<form action="index.php" method="post"> | |
<table cellpadding="5" align="center" cellpadding="2" border="7"> | |
<caption><hr><b>XBCrypt - Text Cryption</b><hr></caption> | |
<tr> | |
<td align="center">Password: <input type="text" name="pswd" id="pass" value="<?php echo htmlspecialchars($pswd); ?>" /></td> | |
</tr> | |
<tr> | |
<td align="center"><textarea id="box" name="code"><?php echo htmlspecialchars($code); ?></textarea></td> | |
</tr> | |
<tr> | |
<td><input type="submit" name="encrypt" class="button" value="Encode" onclick="validate(1)" /></td> | |
</tr> | |
<tr> | |
<td><input type="submit" name="decrypt" class="button" value="Decode" onclick="validate(2)" /></td> | |
</tr> | |
<tr> | |
<td align="center">XBCrypt | By #XByte | Copyright © 2015 | <span style="cursor:pointer;color:#0000FF" onclick="help()">help</span></td> | |
</tr> | |
<tr> | |
<td><center><div style="color: <?php echo htmlspecialchars($color) ?>"><?php echo htmlspecialchars($error) ?></div></center></td> | |
</tr> | |
</table> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
textarea { | |
width: 500; | |
height: 200; | |
background-color: E0E0E0; | |
color: #0000FF; | |
} | |
input { | |
font-family: Courier New; | |
} | |
.button { | |
width: 500; | |
height: 40; | |
} | |
b { | |
font-size: 20pt; | |
color: #303030; | |
} | |
body { | |
background-color: #808080; | |
font-family: Courier New; | |
} | |
table { | |
border-color: #A0A0A0; | |
background-color: #C0C0C0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// function to encrypt the text given | |
function encrypt($pswd, $text) | |
{ | |
// change key to lowercase for simplicity | |
$pswd = strtolower($pswd); | |
// intialize variables | |
$code = ""; | |
$ki = 0; | |
$kl = strlen($pswd); | |
$length = strlen($text); | |
// iterate over each line in text | |
for ($i = 0; $i < $length; $i++) | |
{ | |
// if the letter is alpha, encrypt it | |
if (ctype_alpha($text[$i])) | |
{ | |
// uppercase | |
if (ctype_upper($text[$i])) | |
{ | |
$text[$i] = chr(((ord($pswd[$ki]) - ord("a") + ord($text[$i]) - ord("A")) % 26) + ord("A")); | |
} | |
// lowercase | |
else | |
{ | |
$text[$i] = chr(((ord($pswd[$ki]) - ord("a") + ord($text[$i]) - ord("a")) % 26) + ord("a")); | |
} | |
// update the index of key | |
$ki++; | |
if ($ki >= $kl) | |
{ | |
$ki = 0; | |
} | |
} | |
} | |
// return the encrypted code | |
return $text; | |
} | |
// function to decrypt the text given | |
function decrypt($pswd, $text) | |
{ | |
// change key to lowercase for simplicity | |
$pswd = strtolower($pswd); | |
// intialize variables | |
$code = ""; | |
$ki = 0; | |
$kl = strlen($pswd); | |
$length = strlen($text); | |
// iterate over each line in text | |
for ($i = 0; $i < $length; $i++) | |
{ | |
// if the letter is alpha, decrypt it | |
if (ctype_alpha($text[$i])) | |
{ | |
// uppercase | |
if (ctype_upper($text[$i])) | |
{ | |
$x = (ord($text[$i]) - ord("A")) - (ord($pswd[$ki]) - ord("a")); | |
if ($x < 0) | |
{ | |
$x += 26; | |
} | |
$x = $x + ord("A"); | |
$text[$i] = chr($x); | |
} | |
// lowercase | |
else | |
{ | |
$x = (ord($text[$i]) - ord("a")) - (ord($pswd[$ki]) - ord("a")); | |
if ($x < 0) | |
{ | |
$x += 26; | |
} | |
$x = $x + ord("a"); | |
$text[$i] = chr($x); | |
} | |
// update the index of key | |
$ki++; | |
if ($ki >= $kl) | |
{ | |
$ki = 0; | |
} | |
} | |
} | |
// return the decrypted text | |
return $text; | |
} | |
?> |
Do you have Playfair cipher.
Thanks, dude 🙏
Nice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks man, was very helpfull