I need simple encryption, mycrypt doesnt work
Moderator: General Moderators
I need simple encryption, mycrypt doesnt work
What is some simple encryption I can do? (It needs to be reversible and based off of a key) I'd prefer the method be less than 8 lines, if possible.
Thanks in advance.
Thanks in advance.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
You might try a search for the blowfish algorithm amungst many others. If you look at the algorithms that mcrypt supports you can google for each individually and you might find extension-less PHP implementations of them. You can usually include these things (so that's one line) but obviously the include represents a whole file worth of code.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
I bet this could be shortened to 8 lines.
Code: Select all
<?php
function RC4($keyfile, $data) { //ecncrypt $data with the key in $keyfile with an rc4 algorithm
$pwd = implode('', file($keyfile));
$pwd_length = strlen($pwd);
for ($i = 0; $i < 255; $i++) {
$key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
$counter[$i] = $i;
}
for ($i = 0; $i < 255; $i++) {
$x = ($x + $counter[$i] + $key[$i]) % 256;
$temp_swap = $counter[$i];
$counter[$i] = $counter[$x];
$counter[$x] = $temp_swap;
}
for ($i = 0; $i < strlen($data); $i++) {
$a = ($a + 1) % 256;
$j = ($j + $counter[$a]) % 256;
$temp = $counter[$a];
$counter[$a] = $counter[$j];
$counter[$j] = $temp;
$k = $counter[(($counter[$a] + $counter[$j]) % 256)];
$Zcipher = ord(substr($data, $i, 1)) ^ $k;
$Zcrypt .= chr($Zcipher);
}
return $Zcrypt;
}
?>- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
You could write all your code like this:Ahh if I did that I'd be so cool 
Code: Select all
$a=array(1,4,6,32,4,63,6,3,2);for($i=0,$j=count($a);$i<$j;$i++){if($a[$i]>5){echo $a[$i];}else{foreach(array('pubs','clubs','towns') as $k=>$v){echo 'Data '.$v.' at pos '.$k;}}}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
It is never, ever, a good idea to compact lines of code. Your sanity withstanding, what of others that may have to read or work with the code later? There's also the logic error some people run into:Sure, you could combine those two lines that apparently the author would want to run for that if into a single line using the comma operator, but it's just not worth the hassle of debugging and for other programmers.
I also put my support behind the methodology of always placing braces around code blocks; even one liners.
Now this doesn't really apply when you're specifically trying to obscure what the code is doing or something similar, but that's a different thread and way of programming.
Code: Select all
if ($something)
$foo = $something . ' + 60'; doSomething($foo);I also put my support behind the methodology of always placing braces around code blocks; even one liners.
Now this doesn't really apply when you're specifically trying to obscure what the code is doing or something similar, but that's a different thread and way of programming.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Everybody should write there PHP adhereing to the Zend Framework PHP Coding Standard. I do, its been great 
I do write a majority of my code with pretty formatting for ease of understanding it at first glance, but if I'm doing something really simple, I sometimes might do something like this:
I always put curly braces where necessary too 
And, from what I can see, I follow what ole said too.
Edit: I see sometimes people just doing echo "Hi";, is that bad, or is my way bad?
Code: Select all
if ($a == "blahblah")
{ echo ("Hi"); }And, from what I can see, I follow what ole said too.
Edit: I see sometimes people just doing echo "Hi";, is that bad, or is my way bad?
Last edited by toasty2 on Sat Aug 12, 2006 10:04 am, edited 1 time in total.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
It really doesn't make a difference, just note that echo is a language construct, not a function. Personally, I don't use parentheses. Same goes with require_once and friends.
Last edited by Ambush Commander on Sat Aug 12, 2006 10:26 am, edited 1 time in total.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Do not use parentesis around language constructs that don't require it.
Do not use double quoted strings unless you are injecting variables or using escape characters:
Do not use double quoted strings unless you are injecting variables or using escape characters:
Code: Select all
$a = "Hello, world"; // bad
$a = 'Hello, world'; // good
$a = "Hello, world\n"; // acceptable
$b = 'World';
$a = "Hello, $b\n"; // acceptableUh, I just thought of something.....how am I going to decrypt the data I encrypted? 
I guess I'll switch to plan B, it doesn't require encryption
However, that encryption might be useful later..
I guess I'll switch to plan B, it doesn't require encryption
Last edited by toasty2 on Sat Aug 12, 2006 10:30 am, edited 1 time in total.