NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Encoding and decoding

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 03-30-2006, 03:49 PM THREAD STARTER               #1 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Encoding and decoding


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.
__________________
Encenta - Amazon Associates CMS
Last edited by Encenta.com; 03-30-2006 at 03:59 PM.
Encenta.com is offline  
Old 03-30-2006, 06:04 PM   #2 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



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 by nasaboy007; 03-30-2006 at 06:35 PM.
nasaboy007 is offline  
Old 03-30-2006, 07:10 PM   #3 (permalink)
NamePros Regular
 
Tree's Avatar
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
Tree will become famous soon enoughTree will become famous soon enough
 



Could you post up that source?
Tree is offline  
Old 03-30-2006, 07:21 PM   #4 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



php file source:

PHP Code:
<?php
$encode
=$_POST['encode'];
$decode=$_POST['decode'];
????: NamePros.com http://www.namepros.com/programming/182172-encoding-and-decoding.html
$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) {
????: NamePros.com http://www.namepros.com/showthread.php?t=182172
$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.
nasaboy007 is offline  
Old 03-30-2006, 07:29 PM   #5 (permalink)
NamePros Regular
 
Tree's Avatar
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
Tree will become famous soon enoughTree will become famous soon enough
 



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.
Tree is offline  
Old 03-30-2006, 07:34 PM   #6 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



<-- 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.
nasaboy007 is offline  
Old 03-31-2006, 03:22 AM THREAD STARTER               #7 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Thanks for the replies. How do I output only alphanumeric characters. For example base64("1") contains '=' and so is not suitable for urls
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 03-31-2006, 01:26 PM   #8 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



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.
nasaboy007 is offline  
Old 03-31-2006, 01:41 PM   #9 (permalink)
NamePros Regular
 
Tree's Avatar
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
Tree will become famous soon enoughTree will become famous soon enough
 



Originally Posted by nasaboy007
<-- 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 by Tree; 04-01-2006 at 07:11 AM.
Tree is offline  
Old 05-22-2006, 10:54 AM   #10 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



this is amazing! This may be just the thing I need...
PoorDoggie is offline  
Old 05-22-2006, 12:42 PM   #11 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Originally Posted by miseria
Thanks for the replies. How do I output only alphanumeric characters. For example base64("1") contains '=' and so is not suitable for urls

????: NamePros.com http://www.namepros.com/showthread.php?t=182172
urlencode() will change it to a way to transmit over a url.

EDIT

oops just noticed PoorDoggiewas replying to an old thread.
Peter is offline  
Old 05-22-2006, 12:58 PM   #12 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



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.
PoorDoggie is offline  
Old 05-22-2006, 01:28 PM   #13 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
http://www.scriptgod.com/encode.php

Since we're bringing up an old thread I've edited it a tad:
PHP Code:
<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>
????: NamePros.com http://www.namepros.com/showthread.php?t=182172
</tr>
</table>
</form>
<?php

if (isset($_POST['submit']) AND $_POST['submit'] != '')
{
    
$encode trim($_POST['encode']);
    
$decode trim($_POST['decode']);
    
$mode trim($_POST['mode']);
????: NamePros.com http://www.namepros.com/showthread.php?t=182172
    
$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
Eric is offline  
Old 05-22-2006, 01:33 PM   #14 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



see if you can crack mine (runs on same basis... but is more intricate!) - http://6yd.net/encryption
PoorDoggie is offline  
Old 05-22-2006, 03:45 PM   #15 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



@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.
nasaboy007 is offline  
Old 05-22-2006, 04:21 PM   #16 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



didn't know about mcrypt till now! Sorry... if ur lucky I will try and get the code posted tomorrow sometime
PoorDoggie is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 10:22 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger