Cijfers naar tekens

Status
Niet open voor verdere reacties.

5blabla5

Gebruiker
Lid geworden
27 apr 2009
Berichten
485
Voor een speciale crypt functie die onder andere gebruik maakt van Extended DES encryptie, moet ik het aantal rondes (irritations) opgeven dat de library uitvoert. Blijkbaar is Extended DES vrij ingewikkeld, want het maakt gebruik van de volgende conversietabel:

. = 0
/ = 1
0 = 2
1 = 3
2 = 4
(t/m 9 = 11)
A = 12
B = 13
(t/m Z = 37)
a = 38
b = 39
(t/m z = 63)

Het aantal ronden wordt uitgedrukt in een string met de bovenstaande karakters (voorbeeld: /.Aa). Om te berekenen welke tekens je moet gebruiken heb ik het volgende schematje:
k1 * 64 ^ 0 = A
k2 * 64 ^ 1 = B
k3 * 64 ^ 2 = C
k4 * 64 ^ 3 = D
Hier staat A+B+C+D voor het aantal ronden, en k1 t/m k4 voor de vier karakters die de string bevat.

Bron die ik heb gebruikt: http://arstechnica.com/civis/viewtopic.php?f=20&t=1125143 (tweede post)

Voor de duidelijkheid: het is de bedoeling dat ik van het aantal rondes naar de string ga. Hiervoor heb ik de volgende code gemaakt:
PHP:
<?php

			$rounds = 5000;

			// Generating conversion table for Extended DES irritation conversion
			$charset  = './';
			$charset .= '0123456789';
			$charset .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$charset .= 'abcdefghijklmnopqrstuvwxyz';
			
			$charextd = array();
			for($i = 0; $i < strlen($charset); $i++) {
				$charextd[] = $charset[$i];
			}
			
			// Generating iritation string with var $round
			if($rounds < 64) {
			
				$irritation  = $charextd[$rounds];
				$irritation .= '.';
				$irritation .= '.';
				$irritation .= '.';
				
			} elseif($rounds < 4096) {
			
				$irritation  = $charextd[$rounds - (floor($rounds / 64) * 64)];
				$irritation .= $charextd[floor($rounds / 64)];
				$irritation .= '.';
				$irritation .= '.';
				
			} elseif($rounds < 262144) {
			
				$irritation  = $charextd[$rounds - (floor($rounds / 64) * 64)];
				$irritation .= $charextd[$rounds - (floor($rounds / 4096) * 4096)];
				$irritation .= $charextd[floor($rounds / 64)];
				$irritation .= '.';
				
			} elseif($rounds < 16777216) {
			
				$irritation  = $charextd[$rounds - (floor($rounds / 64) * 64)];
				$irritation .= $charextd[$rounds - (floor($rounds / 4096) * 64)];
				$irritation .= $charextd[$rounds - (floor($rounds / 262144) * 64)];
				$irritation .= $charextd[floor($rounds / 64)];
				
			} else {
			
				$irritation  = 'z';
				$irritation .= 'z';
				$irritation .= 'z';
				$irritation .= 'z';
				
			}
			
			echo $irritation;
			
?>

Het script zelf geeft echter notices, en de string klopt nog niet. Momenteel lukt het me niet om formules hiervoor te verzinnen, dus ik vraag jullie om hulp. Alvast bedankt!
 
De vraag is al opgelost! Ik was blijkbaar niet helemaal helder. Ik keer het proces om: van teken 4 naar 1 i.p.v. 1 naar 4, en trek telkens het verkregen getal er vanaf.

Script:

PHP:
<?php

	// Generating conversion table for Extended DES irritation conversion
	$charset  = './';
	$charset .= '0123456789';
	$charset .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$charset .= 'abcdefghijklmnopqrstuvwxyz';
		
	$charextd = array();
	for($i = 0; $i < strlen($charset); $i++) {
		$charextd[] = $charset[$i];
	}

	$distributor = 5000;

	$four        = floor($distributor / 262144);
	$distributor = $distributor - ($four * 262114);

	$three       = floor($distributor / 4096);
	$distributor = $distributor - ($three * 4096);

	$two         = floor($distributor / 64);
	$distributor = $distributor - ($two * 64);

	$one         = $distributor;
	$distributor = 0;
	
	$irritation  = $charextd[$one] . $charextd[$two] . $charextd[$three] . $charextd[$four];
	
	echo $irritation;

?>
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan