[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 03-30-2006, 02:49 PM   #1 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


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.
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets

Last edited by miseria; 03-30-2006 at 02:59 PM.
miseria is offline  
Old 03-30-2006, 05:04 PM   #2 (permalink)
Senior Member
 
nasaboy007's Avatar
 
Join Date: Jul 2005
Location: NJ
Posts: 1,112
1,454.30 NP$ (Donate)

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 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 05:35 PM.
nasaboy007 is offline  
Old 03-30-2006, 06:10 PM   #3 (permalink)
NamePros Regular
 
Tree's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
13.25 NP$ (Donate)

Tree will become famous soon enoughTree will become famous soon enough


Could you post up that source?
Tree is offline  
Old 03-30-2006, 06:21 PM   #4 (permalink)
Senior Member
 
nasaboy007's Avatar
 
Join Date: Jul 2005
Location: NJ
Posts: 1,112
1,454.30 NP$ (Donate)

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 of


php file source:

PHP Code:
<?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.
nasaboy007 is offline  
Old 03-30-2006, 06:29 PM   #5 (permalink)
NamePros Regular
 
Tree's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
13.25 NP$ (Donate)

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, 06:34 PM   #6 (permalink)
Senior Member
 
nasaboy007's Avatar
 
Join Date: Jul 2005
Location: NJ
Posts: 1,112
1,454.30 NP$ (Donate)

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 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, 02:22 AM   #7 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


Thanks for the replies. How do I output only alphanumeric characters. For example base64("1") contains '=' and so is not suitable for urls
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets
miseria is offline  
Old 03-31-2006, 12:26 PM   #8 (permalink)
Senior Member
 
nasaboy007's Avatar
 
Join Date: Jul 2005
Location: NJ
Posts: 1,112
1,454.30 NP$ (Donate)

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 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, 12:41 PM   #9 (permalink)
NamePros Regular
 
Tree's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
13.25 NP$ (Donate)

Tree will become famous soon enoughTree will become famous soon enough


Quote:
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 06:11 AM.
Tree is offline  
Old 05-22-2006, 09:54 AM   #10 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie 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, 11:42 AM   #11 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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
Quote:
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

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, 11:58 AM   #12 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie 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, 12:28 PM   #13 (permalink)
NPQ's PA, Slave, and On Call Coder

Technical Services


 
Eric's Avatar
 
Join Date: Mar 2005
Posts: 4,545
0.71 NP$ (Donate)

Eric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond repute

Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse
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>
</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
__________________
Eric is offline  
Old 05-22-2006, 12:33 PM   #14 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie 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, 02:45 PM   #15 (permalink)
Senior Member
 
nasaboy007's Avatar
 
Join Date: Jul 2005
Location: NJ
Posts: 1,112
1,454.30 NP$ (Donate)

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 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, 03:21 PM   #16 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 05:19 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85