Unstoppable Domains

Encoding and decoding

Spaceship Spaceship
Watch

SiKing

Registered MemberEstablished Member
Impact
6
Hey, Is there an easy way to encode and then decode a string in PHP? Probably base64 but I want to add a custom string to make it un-decodable without the string..if that makes sense.

eg,
$code = 'encode me';
$str = base64_encode("blah".$code);

How would I decode that?

Thanks.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
i know what you can do.

just make the "custom string" that u mentioned to be a number and then just use that number to determine how many times the script will encode the $code (or decode).

heres a working sample:

http://nasaboy007.t35.com/coder64/index.html
 
Last edited:
0
•••
Could you post up that source?
 
0
•••
php file source:

PHP:
<?php
$encode=$_POST['encode'];
$decode=$_POST['decode'];
$key=$_POST['key'];
if($encode==''&&$decode==''){
  die('<font color="red" style="font-weight:bolder;">Error:</font> Please enter a string to encode/decode.<br><br><a href="Javascript:history.go(-1);">Click Here to Go Back</a>');
}
if($key==''){
  die('<font color="red" style="font-weight:bolder;">Error:</font> Please enter a key number.<br><br><a href="Javascript:history.go(-1);">Click Here to Go Back</a>');
}
if(!is_numeric($key)){
  die('<font color="red" style="font-weight:bolder;">Error:</font> The key value is not a valid numerical value.<br><br><a href="Javascript:history.go(-1);">Click Here to Go Back</a>');
}
if($encode!=''&&$decode!=''){
  die('<font color="red" style="font-weight:bolder;">Error:</font> Cannot encode and decode simultaneously. Please encode/decode individually.<br><br><a href="Javascript:history.go(-1);">Click Here to Go Back</a>');
}
if($decode==''){
$i=0;
while($i<=$key) {
$encode = base64_encode($encode);
$i++;
}
echo('<font color="red">Final Encoded Value:</font> <br><br>');
echo($encode);
}

if($encode==''){
$i=0;
while($i<=$key) {
$decode = base64_decode($decode);
$i++;
}
echo('<font color="red">Final Decoded Value:</font> <br><br>');
echo $decode;
}

?>

html should be self explanitory ;)


hope it helps.
 
0
•••
Thanks! :)

It loads a bit slow when using a large number and you have the max execution time in php.ini set to 10 seconds. I'm going to try and optimize it if that's alright with you.
 
0
•••
<-- uses free host that i dont give a crap about



so sry bout the max exec time etc lol.

plz do optimize it cuz if u can make it work faster, post the code too, id like to see it.
 
0
•••
Thanks for the replies. How do I output only alphanumeric characters. For example base64("1") contains '=' and so is not suitable for urls
 
0
•••
hmm... dont know if it will work, but i read somewhere that replacing the = with a . (period).

also, from numerous tests i found that the decode option still works even if you take out the equal signs. try it yourself: do sumtin with a key of 1, copy paste it, and take out the ='s. it shud still decode properly (it did for me).

how bout sum rep for my hard work? ;)

any more questions feel free to ask.
 
1
•••
nasaboy007 said:
<-- uses free host that i dont give a crap about

I have a "programmer's" server, and I support PHP coders such as myself. If you'd like hosting for a small amount of NP$, PM me.
 
Last edited:
0
•••
this is amazing! :) This may be just the thing I need...
 
0
•••
miseria said:
Thanks for the replies. How do I output only alphanumeric characters. For example base64("1") contains '=' and so is not suitable for urls


urlencode() will change it to a way to transmit over a url.

EDIT

oops just noticed PoorDoggiewas replying to an old thread.
 
0
•••
yea sorry. It was posted into a new thread, and I replyed in the wrong one! :) anyway - that is a good idea you know, about the urlencode - that will fix the "=" problem.
 
0
•••
http://www.scriptgod.com/encode.php

Since we're bringing up an old thread ;) I've edited it a tad:
PHP:
<html>
<head>
<title>Encode/Decode</title>
</head>

<body>

<h1>Super Encoder/Decoder</h1>

<h3>Instructions</h3>

<p>For Encoding Text:<br /><br />Insert a cleartext string into the text box below marked "Encode". Leave "Decode" blank. Input a whole number of 1 or more in the box marked "Key". The higher the Key value, the string will be more secure as well as long.</p>

<p>For Decoding a code:<br /><br />Insert the encoded string into the text box below marked "Decode". Leave "Encode" blank. Input the Key Value that was used to encode this string into the text box marked "Key". Without this value, the text will not decode properly.</p>

<form method="post" action="encode.php">
<table cellpadding="2" cellspacing="2">
<tr>
  <td valign="top">Encode</td>
  <td><textarea name="encode" rows="3" cols="40" wrap="virtual"></textarea></td>
</tr>
<tr>
  <td valign="top">Decode</td>
  <td><textarea name="decode" rows="3" cols="40" wrap="virtual"></textarea></td>
</tr>
<tr>
  <td valign="top">Key</td>
  <td><input type="text" name="key"></td>
</tr>
<tr>
  <td valign="top">Mode</td>
  <td>
  <select name="mode">
    <option value="encode">Encode</option>
    <option value="decode">Decode</option>
  </select>
  </td>
</tr>
<tr>
  <td valign="top"></td>
  <td><input type="submit" name="submit" value="Encode/Decode"></td>
</tr>
</table>
</form>
<?php

if (isset($_POST['submit']) AND $_POST['submit'] != '')
{
    $encode = trim($_POST['encode']);
    $decode = trim($_POST['decode']);
    $mode = trim($_POST['mode']);
    $key = trim($_POST['key']);

    if ($encode == '' AND $decode == '')
    {
        echo '<span style="color: red; font-weight: bold;">Error:</span> Please enter a string to encode/decode.';
    }
    else if ($key == '' OR preg_match("#([^0-9]+)#", $key))
    {
        echo '<span style="color: red; font-weight: bold;">Error:</span> Either the key number was left blank, or you\'ve entered an invalid key.';
    }
    else if ($encode != '' AND $decode != '')
    {
        echo '<span style="color: red; font-weight: bold;">Error:</span> Cannot encode and decode simultaneously. Please encode/decode individually.';
    }
    else
    {
        $data = '';

        switch ($mode)
        {
            case 'encode':
                for ($i = 0; $i <= $key; $i++)
                {
                    $data .= base64_encode($encode);
                }
                $msg = '<font color="red">Final Encoded Value:</font>';
                break;
            case 'decode':
                for ($i = 0; $i <= $key; $i++)
                {
                    $data .= base64_decode($decode);
                }
                $msg = '<font color="red">Final Decoded Value:</font>';
                break;
        }
        echo $msg.'<br /><br />'.$data;
    }
}

?>

</body>
</html>
Side note: if my code looks a little weird, structure wise etc... i've been trying to conform to vBulletin coding standards :guilty:
 
0
•••
0
•••
@poordoggie
urs uses mcrypt, rite? i didnt use that cuz some hosts dont have it installed. the manual said u gotta install it i think X_X

@secondversion
problem with ur updated version: when u try to decrypt something it decrypts it fine, but it doubles (or triples, quadrouples, etc) the cleartext.
 
0
•••
didn't know about mcrypt till now! :) Sorry... if ur lucky I will try and get the code posted tomorrow sometime :)
 
0
•••
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back