String encryption met wachtwoord
Een groepje functies die samen een encryptie functie hebben. Encryptie vereist geen wachtwoord. Bij het invullen van geen wachtwoord wordt de string niet ge-encrypt. Wachtwoord wordt niet opgeslagen in de string. String alleen te lezen met wachtwoord, ontcijferen wordt erg lastig. Functies niet gecheckt op notice-errors. Voorbeeld is snel in elkaar gezet. Kleine omschrijving hoe encryptie in zn werk gaat: -converteer input naar binair. -maak MD5 van wachtwoord en zet de output om in binair. (128 bits) -vergelijk wachtwoord-binair met input-binair. schematisch voorbeeld van de werking: Wat er gebeurt: als een bitje in wachtwoord gelijk is aan '1', dan veranderd ie het bitje bij INPUT op dezelfde positie. Als bitje 1 is bij input, wordt deze 0. Als bitje 0 is wordt deze 1. Als het bitje in het wachtwoord '0' is, doet het niets. Indien de input langer is dan het wachtwoord (langer dan 16 tekens) begint ie weer bij bit 0 van het wachtwoord en gaat zo weer opnieuw alle 128 bits bij langs. Omdat ie simpelweg van een 1 een 0 maakt en van een 0 een 1, werkt de functie ook omgekeert. Daarom kan je decrypten EN encrypten met dezelfde functie en dezelfde manier. -zet 'output' terug naar STRING. -return output.
[code]<?php
//functie die een string omzet in binair.
function str2bin($str) {
$array = str_split($str,1);
$bin = '';
foreach($array as $chr) {
$bin.=decbin_fix(ord($chr));
}
return $bin;
}
//functie die een binaire string omzet in een string.
function bin2str($bin) {
$array = str_split($bin,8);
$str = '';
foreach($array as $chr) {
$str.=chr(bindec($chr));
}
return $str;
}
//functie die een getal omzet in een binair getal en deze links aanvult met het opgegeven aantal 0-en.
function decbin_fix($int,$length = 8) {
return str_pad(decbin($int),$length,'0',STR_PAD_LEFT);
}
function livc_hex2bin($hex) {
$hex = str_split($hex,1);
$bin = '';
foreach($hex as $h) {
$bin.=str_pad(base_convert($h,16,2),4,'0',STR_PAD_LEFT);
}
return $bin;
}
function bin_encrypt($bin,$password) {
if($password == '') {
return $bin;
}
$password = livc_hex2bin(md5($password));
$bin = str_split($bin,1);
$password = str_split($password,1);
$pcp = 0;//password cursor position
$bcp = 0;//binairy cursor position
while($bcp<count($bin)) {
if($pcp<count($password)) {
$pcp ++;
} else {
$pcp = 0;
}
if($password[$pcp]=='1' && $bin[$bcp]=='0') {
$bin[$bcp] = '1';
} elseif($password[$pcp]=='1' && $bin[$bcp]=='1') {
$bin[$bcp] = '0';
}
$bcp ++;
}
$bin = array_reduce($bin,'merge');
return $bin;
}
function merge($a,$b) {
return $a.$b;
}
function str_encrypt($str,$password) {
return bin2str(bin_encrypt(str2bin($str),$password));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Zelf Extracting Installer</title>
<style type="text/css">
textarea {
width:400px;
height:200px;
font-size:16px;
}
input {
width:200px;
}
* {
margin:0px;
}
</style>
</head>
<body>
<center>
<form method="post">
<h2>Input:</h2>
<textarea name="input"><?php echo stripslashes($_POST['input']);?></textarea>
<h2>Password:</h2>
<input type="text" name="password" value="<?php echo stripslashes($_POST['password']);?>"/>
<br/>
<input type="submit" style="width:400px;height:40px;font-size:20px;" value="APPLY ENCRYPTION OR DECRYPTION"/>
</form>
<?php if(isSet($_POST['input'])) { ?>
<h2>Output:</h2>
<h3 style="color:red;">Warning: output may contain characters that can NOT be copy-pasted.</h3>
<h3 style="color:red;">"Output reversion" is used to check if the output is correct.</h3>
<h3>If you want copy-paste-able outputs, you should convert the output to HEX or BIN.</h3>
<h3>Use livc_bin2hex(str2bin($string)) to convert a string to HEX.</h3>
<h3>Use bin2str(livc_hex2bin($string)); to convert it back to a STRING.</h3>
<textarea><?php echo str_encrypt(stripslashes($_POST['input']),stripslashes($_POST['password']));?></textarea>
<h2>Output reversed:</h2>
<h3>to check if encryption is succes</h3>
<textarea><?php echo str_encrypt(str_encrypt(stripslashes($_POST['input']),stripslashes($_POST['password'])),stripslashes($_POST['password']));?></textarea>
<h2>Decryption of Encrypted string: <?php if(str_encrypt(str_encrypt(stripslashes($_POST['input']),stripslashes($_POST['password'])),stripslashes($_POST['password']))===stripslashes($_POST['input'])){echo '<span style="color:green;">SUCCES</span>';}else {echo '<span style="color:red;">FAIL</span>';}?></h2>
<?php } ?>
</center>
</body>
</html>[/code]
Reacties
0