Also mein Programm soll mit Hilfe zweier Primzahlen den öffentlichen und privaten Schlüssel der RSA Verschlüsselungsmethode erzeugen... Zusätzlich werden noch andere Werte eingeblendet die nötig sind um die Schlüssel zu berechnen... Screenshot ist im Anhang
Key_Gen_open.php
PHP-Code:
<?php
// Der Bezug zur Klassendatei wird hergestellt
require ('./Key_Gen_class.php');
if(isset($_POST['create'])){
if ($_POST['prime1'] != $_POST['prime2']){
// Die Eigenschaften der Klasse RSAKeyGenerator werden aufgerufen
$RSAKeyGenerator = new RSAKeyGenerator();
$RSAKeyGenerator->fill_primes($_POST['prime1'],$_POST['prime2']);
$m=$RSAKeyGenerator->create_m();
$phi=$RSAKeyGenerator->create_phi();
$e=$RSAKeyGenerator->create_e ();
$d=$RSAKeyGenerator->create_d ();
}
// Werden 2 gleiche Primzahlen eingegeben erhält der Benutzer eine Fehlermeldung
else {
echo "<font color=\"#FF0000\">Please choose two different prime numbers!</font>";
}
}
// Die Programmoberfläche wird erstellt
echo"<html>";
echo"<head>";
echo"<title>RSA Keys</title>";
echo"</head>";
echo"<body> ";
echo"<h2>RSA Keys</h2>";
echo"<form action='" . $SELF_PHP . "' method='POST'>";
echo"<table border=1>";
echo"<tr><td><b>P =
<select name='prime1' size='1'>;
<option>5</option>;
<option>7</option>;
<option>11</option>;
<option>13</option>;
<option>17</option>;
<option>19</option>;
<option>23</option>;
<option>29</option>;
<option>31</option>;
<option>37</option>;
<option>41</option>;
<option>43</option>;
<option>47</option>;
<option>53</option>;
<option>59</option>;
<option>61</option>;
<option>67</option>;
<option>71</option>;
<option>73</option>;
<option>79</option>;
<option>83</option>;
<option>89</option>;
<option>97</option>;
</select></td>";
echo"<td><b>Q =;
<select name='prime2' size='1'>;
<option>5</option>;
<option>7</option>;
<option>11</option>;
<option>13</option>;
<option>17</option>;
<option>19</option>;
<option>23</option>;
<option>29</option>;
<option>31</option>;
<option>37</option>;
<option>41</option>;
<option>43</option>;
<option>47</option>;
<option>53</option>;
<option>59</option>;
<option>61</option>;
<option>67</option>;
<option>71</option>;
<option>73</option>;
<option>79</option>;
<option>83</option>;
<option>89</option>;
<option>97</option>;
</select></td>";
echo"<tr><td align='right'><b>m</td>";
echo"<td><input type='text' size='8' value='" . $m . "' readonly></input></td></tr>";
echo"<tr><td align='right'><b>(p-1)*(q-1)</td>";
echo"<td><input type='text' size='8' value='" . $phi . "' readonly></input></td></tr>";
echo"<tr><td align='right'><b>e</td>";
echo"<td><input type='text' size='8' value='" . $e . "' readonly></input></td></tr>";
echo"<tr><td align='right'><b>d</td>";
echo"<td><input type='text' size='8' value='" . $d . "' readonly></input></td></tr>";
echo"<tr><td colspan='2' align='center'><input type='submit' name='create' value='Create Key'></td></tr>";
echo"</table>";
echo"</form>";
echo"</body>";
echo"</html>";
?>
_________________________________________________________________
Key_Gen_class.php
<?php
// Die Klasse RSAKeyGenerator wird definiert
class RSAKeyGenerator {
// ----------------------->Attribute<-----------------------------------
private $m;
private $phi;
private $p;
private $q;
private $e;
// ----------------------->Eigenschaften<------------------------------
// Die vom Benutzer gewählten Primzahlen werden den Attributen zugeschrieben
function fill_primes ($p, $q){
$this->p = $p;
$this->q = $q;
}
// m wird berechnet
function create_m (){
$m = ($this->p)*($this->q);
$this->m = $m;
return $m;
}
// phi wird berechnet
function create_phi () {
$phi = ($this->p-1)*($this->q-1);
$this->phi = $phi;
return $phi;
}
// Der öffentliche Schlüssel e wird berechnet
function create_e () {
for($e=2; $e<$this->m; $e++){
//KGV von e und phi wird gebildet
if ($e > $this->phi){
for ($k=$e; ($k) % ($this->phi) != 0; $k += $e){
// In $k steht das KGV
}
}
else{
for ($k=$this->phi; ($k) % ($e) != 0; $k += $this->phi){
// In $k steht das KGV
}
}
/*
GGT ist (e->öffentlicher Schlüssel mal phi) durch das KGV beider
Sobald $finish den Wert 1 hat sind beide zueinander teilerfremd
-> Die Bedingung für e ist erfüllt, die Schleife wird abgebrochen (break;)
*/
$finish = ($e * $this->phi)/$k;
if ($finish == 1){
break;
}
}
$this->e = $e;
return $e;
}
// Der private Schlüssel d wird berechnet
function create_d () {
for ($d=1; (($this->e*$d) % ($this->phi)) != 1 ; $d++){
}
return $d;
}
}
?>
Ich hoffe das hilft ein wenig mehr um für Durchblick zu sorgen. Wenn schonmal mein ganzes Script drin steht wäre ich für Optimierungs und Verbesserungsvorschläge sehr dankbar.