Jump to content

PHP Cipher Thing Trouble


Go to solution Solved by ignace,

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)

  • Solution

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?

Edited by cjburkey01
$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:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.