1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284:
| <?php $key = $_POST['key']; ?>
<form action="" method="post" target=""> Key: <br> <input type="Text" name="key" value="<?php echo $key; ?>" size="" maxlength="12"><br><br>
Text: <br> <textarea name="text" cols="50" rows="6"></textarea><br>
<input type="Submit" name="crypt" value="Verschlüsseln"> <input type="Submit" name="decrypt" value="Entschlüsseln"> </form>
<?php include("blowfish.box.php"); //P-Box and S-Box
$text = trim($_POST['text']); if(isset($_POST['key'])) keys($_POST['key']);
//Verschlüsselungsmodus / Encryptionmode: //EBC: 0 //CBC: 1 define("CBC",1);
//Einen Text in Longzahlen umwandeln //Covert a string into longinteger function _str2long($data) { $n = strlen($data); $tmp = unpack('N*', $data); $data_long = array(); $j = 0;
foreach ($tmp as $value) $data_long[$j++] = $value; return $data_long; }
//Longzahlen in Text umwandeln //Convert a longinteger into a string function _long2str($l) { return pack('N', $l); }
//Key-Algorithmen function keys($key) { global $pbox,$sbox0,$sbox1,$sbox2,$sbox3; $key_md5 = md5($key);
//Füllt den $key auf 16 Stellen auf //Convert the $key into a 16Byte key $key = _str2long(substr(str_pad($key, 16, $key_md5),0,16));
# XOR Pbox1 with the first 32 bits of the key, XOR P2 with the second 32-bits of the key, for($i=0;$i<count($pbox);$i++) { $pbox[$i] ^= $key[$i%4]; }
$v[0] = 0x00000000; $v[1] = 0x00000000;
//P-Box durch verschlüsselte Nullbit Blöcke ersetzen. In der nächsten Runde das Resultat erneut verschlüsseln //Encrypt Nullbit Blocks and replace the Pbox with the Chiffre. Next round, encrypt the result for($i=0;$i<count($pbox);$i+=2) { $v = block_encrypt(array($v[0],$v[1])); $pbox[$i] = $v[0]; $pbox[$i+1] = $v[1]; }
//S-Box [0 bis 3] durch verschlüsselte Blöcke ersetzen //Replace S-Box [0 to 3] entries with encrypted blocks for($i=0;$i<count($sbox0);$i+=2) { $v = block_encrypt(array($v[0],$v[1])); $sbox0[$i] = $v[0]; $sbox0[$i+1] = $v[1]; }
//S-Box1 for($i=0;$i<count($sbox1);$i+=2) { $v = block_encrypt(array($v[0],$v[1])); $sbox1[$i] = $v[0]; $sbox1[$i+1] = $v[1]; }
//S-Box2 for($i=0;$i<count($sbox2);$i+=2) { $v = block_encrypt(array($v[0],$v[1])); $sbox2[$i] = $v[0]; $sbox2[$i+1] = $v[1]; }
//S-Box3 for($i=0;$i<count($sbox3);$i+=2) { $v = block_encrypt(array($v[0],$v[1])); $sbox3[$i] = $v[0]; $sbox3[$i+1] = $v[1]; }
return $key; }
//Verschlüsselung ($text = text/string) //Encrytion function blowfish_crypt($text) { $n = strlen($text); if($n%8 != 0) $lng = ($n+(8-($n%8))); else $lng = 0;
$text = str_pad($text, $lng, ' '); $text = _str2long($text);
//Initialization vector: IV if(CBC == 1) { $cipher[0][0] = time(); $cipher[0][1] = (double)microtime()*1000000; }
$a = 1; for($i = 0; $i<count($text); $i+=2) { if(CBC == 1) { //$text mit letztem Geheimtext XOR Verknüpfen //$text is XORed with the previous ciphertext $text[$i] ^= $cipher[$a-1][0]; $text[$i+1] ^= $cipher[$a-1][1]; }
$cipher[] = block_encrypt(array($text[$i],$text[$i+1])); $a++; }
$output = ""; for($i = 0; $i<count($cipher); $i++) { $output .= _long2str($cipher[$i][0]); $output .= _long2str($cipher[$i][1]); }
return base64_encode($output); }
//Entschlüsseln //Decryption function blowfish_decrypt($text) { $plain = array(); $cipher = _str2long(base64_decode($text));
if(CBC == 1) $i = 2; //Message start at second block else $i = 0; //Message start at first block
for($i; $i<count($cipher); $i+=2) { $return = block_decrypt(array($cipher[$i],$cipher[$i+1]));
if(CBC == 1) { //Xor Verknüpfung von $return und Geheimtext aus von den letzten beiden Blöcken //XORed $return with the previous ciphertext $plain[] = array($return[0]^$cipher[$i-2],$return[1]^$cipher[$i-1]); } else //EBC Mode { $plain[] = $return; } }
for($i = 0; $i<count($plain); $i++) { $output .= _long2str($plain[$i][0]); $output .= _long2str($plain[$i][1]); } return $output; }
if(isset($_POST["crypt"])) { $output = blowfish_crypt($text); echo "<br><b>Verschlüsselt</b> sieht der Text so aus:<br> <textarea name=\"ausgabe\" cols=\"45\" rows=\"9\">". $output."</textarea>"; }
else if(isset($_POST["decrypt"])) { $output = blowfish_decrypt($text); echo "<b>Entschlüsselt</b> sieht der Text so aus:<br>". nl2br(htmlentities(stripslashes($output))); }
function block_encrypt($text) { global $pbox; $vl = $text[0]; $vr = $text[1];
for($i=0;$i<16;$i++) { $vl ^= $pbox[$i]; $vr ^= sbox_round($vl);
$v_tmp = $vl; $vl = $vr; $vr = $v_tmp; }
$v_tmp = $vl; $vl = $vr; $vr = $v_tmp;
$vr ^= $pbox[16]; $vl ^= $pbox[17];
return array($vl,$vr); }
function block_decrypt($text) { global $pbox;
$vl = $text[0]; $vr = $text[1];
for($i=17;$i>1;$i--) { $vl ^= $pbox[$i]; $vr ^= sbox_round($vl);
$v_tmp = $vl; $vl = $vr; $vr = $v_tmp; }
$v_tmp = $vl; $vl = $vr; $vr = $v_tmp;
$vr ^= $pbox[1]; $vl ^= $pbox[0];
return array($vl,$vr); }
function sbox_round($integer) { global $sbox0,$sbox1,$sbox2,$sbox3;
//$integer in vier 8 Bit Blöcke unterteilen //Split $integer into four 8 Bit blocks $b0 = $integer<<24 & 0xFF; $b1 = $integer<<16 & 0xFF; $b2 = $integer<<8 & 0xFF; $b3 = $integer & 0xFF;
$return = ($sbox0[$b0] + $sbox1[$b1] % pow(2,32)) ; $return = ($return ^ $sbox2[$b2]) + $sbox3[$b3] % pow(2,32);
return $return; } ?> |