Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 19th, 2002, 11:44 PM
MercuryBoard MercuryBoard is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Posts: 11 MercuryBoard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Simple 2-way encryption

I'm looking for a 2-way algorithm that can convert a string into an alphanumeric string that doesn't resemble the original. Can this be done?

(This is for a PHP script)

Basically what I want to do is take a few pieces of information such as the below, related to an error message, and put them together so it can be transmitted easily through a URL. It would be an added bonus to keep this data fairly secure.

Quote:
4.2.1
3.23.49
Apache
The database could not connect.

Reply With Quote
  #2  
Old July 20th, 2002, 03:47 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
Maybe you can use some prewritten modules for PHP, such as the Mcrypt module. http://www.onlamp.com/pub/a/php/200...ypt.html?page=3

If you want the algorithm descriptions for the sake of learning, I'd recommend buying a copy of Bruce Schneier's "Applied Cryptography" where he discusses the relative strengths and weaknesses of many cryptographic algorithms.

Hope this helps.

Reply With Quote
  #3  
Old July 26th, 2002, 11:35 AM
mrTed's Avatar
mrTed mrTed is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: a northern town
Posts: 74 mrTed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 49 sec
Reputation Power: 12
Hi,

I did an 2 way encryption algorith class for php and a vbscript sub a while back. If it is of use to you let me know and I will dig it out?

Regards, Ed.
__________________
/* measure twice, cut once */

Reply With Quote
  #4  
Old July 26th, 2002, 02:23 PM
MercuryBoard MercuryBoard is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Posts: 11 MercuryBoard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Basically I need something to obscure data and make it transmittable by URL, where it can then be unobscured. Encryption would be a plus but basically I need something simpler.

Reply With Quote
  #5  
Old July 30th, 2002, 11:13 AM
mrTed's Avatar
mrTed mrTed is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: a northern town
Posts: 74 mrTed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 49 sec
Reputation Power: 12
Hi MercuryBoard,

No problem. If you don't get sorted and need the code let me know.

Regards, Ed.

Reply With Quote
  #6  
Old July 30th, 2002, 11:27 AM
MercuryBoard MercuryBoard is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Posts: 11 MercuryBoard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm interested... Send me the code

Reply With Quote
  #7  
Old August 2nd, 2002, 11:40 AM
mrTed's Avatar
mrTed mrTed is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: a northern town
Posts: 74 mrTed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 49 sec
Reputation Power: 12
Hi,

Sorry for the delay, I couldn't remember where I had put it?
I think it will be secure enough if the data is not too critical

The Class...

PHP Code:
<?
//Encryption class
Class cls_encrypt{

var 
$s_array = array();
var 
$K_array = array();
var 
$return_val;
var 
$num_count;
var 
$alp_count;
var 
$lwr_count;

    
//Function to return the decimal value of an ascii character...
    
function get_ascii_val($passed_val){
        
//This assigns all the ascii vals to the $asc_chars() array...
        
for($x_val 0$x_val <= 255$x_val $x_val 1){
            
$asc_chars[chr($x_val)] = $x_val
        }
        return 
$asc_chars[$passed_val];
    } 

    
//Basic function to encrypt/decrypt. Input_val gets crypted/decrypted using key_val... 
    
Function encrypt_value($input_val$key_val){
        
//Create an array of 0 - 255 and initialise 
        
for ($i 0$i <= 255$i $i 1){
            
$s_array[$i] = $i;
        }  

        
//
        
$j 1;
        
//Create an array of 0 - 255 and initialise with a calculated chr value...
        
for ($i 0$i <= 255$i $i 1){
            if (
$j strlen($key_val)){
                
$j 1;
            }
            
$K_array[$i] = $this -> get_ascii_val(substr($key_val$j1));
            
$j $j 1;
        }
        
        
//Shuffle the array values around to give a 'random' value...
        
$j 0;
        for (
$i 0$i <= 255$i $i 1){
            
$j = ($j $s_array[$i] + $K_array[$i]) % 256;
            
$temp_var $s_array[$i];
            
$s_array[$i] = $s_array[$j];
            
$s_array[$j] = $temp_var;
        }
        
        
//Create the 'encrypted' string from the 2 initialised arrays...
        
$i 0;
        
$j 0;
        for (
$x 0$x strlen($input_val); $x++){
            
$i = ($i 1) % 256;
            
$j = ($j $s_array[$i]) % 256;
            
$temp_var $s_array[$i];
            
$s_array[$i] = $s_array[$j];
            
$s_array[$j] = $temp_var;
            
$t = ($s_array[$i] + ($s_array[$j] % 256)) % 256;
            
$y $s_array[$t];
            
$comp_val $this -> get_ascii_val(substr($input_val$x1)) ^ $y;
            
$calc_val Chr($this -> get_ascii_val(substr($input_val$x1)) ^ $y);
            
$return_val $return_val.$calc_val;
        }
        return 
$return_val;
    }
}
?>


... And something to test it with (gives an idea of what it does...

PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<
html>
<
head>
    <
title>Encrypt...</title>
</
head>

<
body>
<? 
if (isset(
$_POST[submit])){
    include(
"addEncryptClasses.inc.php");
    
$my_obj = new cls_encrypt();
    
$obscure_text $my_obj -> encrypt_value($_POST[enctext], $_POST[enckey]);
    echo 
"Text is : ".$_POST[enctext]."<br>";
    echo 
"Key is : ".$_POST[enckey]."<br>";
    echo 
"Obscured Text is : ".$obscure_text."<br><br>";
    echo 
"Re-enter the key to un-obscure the text!<br><br>";
}
?>

<table cellspacing="2" cellpadding="2" border="0">
<form action="" method="post">
<tr>
    <td>
        Enter the text to be obscured or unobscured 
    </td>
</tr>
<tr>
    <td><input type="text" name="enctext" value="<? echo $secure_text?>" size="100"></td>
</tr>
<tr>
    <td>Enter the key</td>
</tr>
<tr>
    <td><input type="text" name="enckey"></td>
</tr>
<tr>
    <td><input type="submit" name="submit"></td>
</tr>
</form>
</table>

</body>
</html> 


I used it originally for storing values in a db as encrypted strings, then used it to store stuff in sessions etc.

It is probably a little hungry for day to day use if there is a lot of data but I think you could easily reduce the number of chars in the encryption routine from 256 to say 40 or 50. If I get a bit of time this week I will sort it out and let you know.

The other reason to reduce the amount of chars used it that this currently uses *ALL* the ascii char set, as opposed to just alphanumeric chars which would be a load better for passing in the URL.

Hope it helps you out anyway.

Regards, Ed.

Reply With Quote
  #8  
Old August 2nd, 2002, 12:06 PM
Dingle Dingle is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 452 Dingle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 12 sec
Reputation Power: 13
heres one that ive used before, i'm not sure where i got it but it seems to work pretty well

PHP Code:
class rc4crypt {
    function 
endecrypt ($pwd$data$case='') {
        if (
$case == 'de') {
            
$data urldecode($data);
        }

        
$key[] = "";
        
$box[] = "";
        
$temp_swap "";
        
$pwd_length 0;

        
$pwd_length strlen($pwd);

        for (
$i 0$i <= 255$i++) {
            
$key[$i] = ord(substr($pwd, ($i $pwd_length), 1));
            
$box[$i] = $i;
        }

        
$x 0;

        for (
$i 0$i <= 255$i++) {
            
$x = ($x $box[$i] + $key[$i]) % 256;
            
$temp_swap $box[$i];

            
$box[$i] = $box[$x];
            
$box[$x] = $temp_swap;
        }

        
$temp "";
        
$k "";

        
$cipherby "";
        
$cipher "";

        
$a 0;
        
$j 0;

        for (
$i 0$i strlen($data); $i++) {
            
$a = ($a 1) % 256;
            
$j = ($j $box[$a]) % 256;

            
$temp $box[$a];
            
$box[$a] = $box[$j];

            
$box[$j] = $temp;

            
$k $box[(($box[$a] + $box[$j]) % 256)];
            
$cipherby ord(substr($data$i1)) ^ $k;

            
$cipher .= chr($cipherby);
        }

        if (
$case == 'de') {
            
$cipher urldecode(urlencode($cipher));

        } else {
            
$cipher urlencode($cipher);
        }

        return 
$cipher;
    }
}

####### USAGE ###########

$thestring "the quick brown fox";
$thepasswd "mypassword";

$rc4 = new rc4crypt;

$thestring $rc4->endecrypt($thepasswd,$thestring);
## $thestring is now encrypted

$thestring $rc4->endecrypt($thepasswd,$thestring,'de');
## $thestring is now decrypted 

Reply With Quote
  #9  
Old August 31st, 2011, 03:28 PM
nilesh4life nilesh4life is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 1 nilesh4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 48 sec
Reputation Power: 0
Thank You

Dingle,

I registered on forum JUST to say to Thank you! The code is pretty old and working pretty good.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Simple 2-way encryption

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap