![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
(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.
__________________
|
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|