Jump to content

PHP Cipher Thing Trouble


cjburkey01

Recommended Posts

So I made this code:

<?php

 

if(isset($_POST['box'], $_POST['choose'])) {

 

if($_POST['choose'] == "to") {

 

$eng = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ,'l' ,'m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');

$udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v');

 

$convertedLanguage = str_replace($eng, $udc, $_POST['box']);

 

} else if($_POST['choose'] == "from") {

 

//Convert text from UDC

 

}

 

}

 

?>

 

<html>

<head>

<title>UDC Converting To/From</title>

</head>

 

<body>

<center>

<h1>UDC To/From</h1>

<form action="" method="post">

<?php echo $convertedLanguage; ?>

<textarea name="box" rows="10" cols="50"></textarea><br />

<label>To UDC</label>

<input type="radio" name="choose" value="to" checked="checked" /><br />

<label>From UDC</label>

<input type="radio" name="choose" value="from" /><br />

<input type="submit" value="Change" />

</form>

</center>

</body>

</html>

And it works for most letters, but some don't like the 'e', turns to 'o', then back to an 'e'

If I put in 'Test'

I get 'peqp'. Like it should be, but the 'e' should be an 'o'

 

To me, it seems as if it running through the e twice. I don't know why? is there a better method?

Link to comment
https://forums.phpfreaks.com/topic/283411-php-cipher-thing-trouble/
Share on other sites

So I could do

 

$eng = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ,'l' ,'m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');

$udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v');

 

$convertedLanguage = preg_replace($eng, $udc, $_POST['box']);

 

Or do I have to put the slashes in from and behind each part in the array?

You may have the same issue with preg_replace:

 

http://us1.php.net/manual/en/function.preg-replace.php

 


Note:

When using arrays with pattern and replacement, the keys are processed in the order they appear in the array. This is not necessarily the same as the numerical index order. If you use indexes to identify which pattern should be replaced by which replacement, you should perform a ksort() on each array prior to calling preg_replace().

 

You may have to resort to doing a loop to iterate over each character.

If (when) you go the route of iterating through the string you could use modulus (sometimes called modulo, but is % in PHP and many other languages)

 

This commonly known as a shift cipher or Caesar cipher: http://en.wikipedia.org/wiki/Caesar_cipher

 

Chars are generally stored as numbers, you can use the following to convert back and forth:

http://us2.php.net/manual/en/function.chr.php

http://us2.php.net/manual/en/function.ord.php

 

but one issue here is that a isn't 0 it's 97, whereas A is 65, so it'd may need offsetting after the mod (if < 97 then + 97)

You need strtr which does not have this problem since it does single-byte translations.

 

$eng = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ,'l' ,'m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
$udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v');

echo strtr('test', array_combine($eng, $udc));
$eng = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ,'l' ,'m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
$udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v');

$convertedLanguage = strtr($_POST['box'], array_combine($eng, $udc));

Should work?

$eng = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ,'l' ,'m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
$udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v');

$convertedLanguage = strtr($_POST['box'], array_combine($eng, $udc));

Should work?

 

IT WORKS!!! THANK YOU ALL FOR THE HELP  :happy-04:  :happy-04:  :happy-04:  :happy-04:  :happy-04:  :happy-04:  :happy-04:  :happy-04:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.