Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 22nd, 2005, 2:54 PM   #1
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
(example) PHP -- Basic Encryption using OOP via PHP4

[php]
<?php
class encryption {
var $key;

function encryption($key) {
$this->key = $key;
}

function encrypt($str) {
$str = $this->split($str);
$key = $this->split($this->key);

while(current($str)) {
$str[key($str)] = chr(ord(current($str)) + ord(current($key)));
next($str);

if(!next($key))
reset($key);
}

return implode("", $str);
}

function decrypt($str) {
$str = $this->split($str);
$key = $this->split($this->key);

while(current($str)) {
$str[key($str)] = chr(ord(current($str)) - ord(current($key)));
next($str);

if(!next($key))
reset($key);
}

return implode("", $str);
}

function split($str) {
$str = array($str);

while(strlen($str[count($str)-1]) != 1) {
$str[count($str)] = substr($str[count($str)-1], 1, strlen($str[count($str)-1])-1);
$str[count($str)-2] = substr($str[count($str)-2], 0, 1);
}

return $str;
}
}

$blah = new encryption("i like cheese");
$test = array();

$test[0] = $blah->encrypt("This is a test message...");
$test[1] = $blah->decrypt($test[0]);

printf("Encrypted: %s\n" .
"Decrypted: %s\n",
$test[0], $test[1]);
?>
[/php]

I spent a few minutes writing an encryption example in PHP presumably for web programmers who want an easier to use way to be able to store data in a database securely and retrieve it later.

I don't have time to comment the code, but if anybody has any questions i'll answer them in this thread. I tried posting in tutorials, but for some reason it seems that my account doesn't have the priveledges.
__________________

tempest is offline   Reply With Quote
Old Sep 22nd, 2005, 7:56 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I've heard the mods have to move it there, Tempest. I don't know that for a fact.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 23rd, 2005, 8:17 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Yep, I think thats a mod thing. Thanks for the code
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC