-1

I need to do a 2-way encryption in PHP. Basically what I want to do is convert a string(Marshall for example) to something weird(s2323sdavrt44 for example) and then convert that (s2323sdavrt44) back to clear text string(Marshall).

How can I achieve this? And no I'm not doing this for passwords or anything, for div id attribute, so later I can split it for SQL queries.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Marshall Mathews
  • 347
  • 5
  • 18

5 Answers5

4

Try this:

$key = 'password to (en/de)crypt';
$string = 'string to be encrypted';

$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

var_dump($encrypted);
var_dump($decrypted);
Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
Neeloy
  • 41
  • 2
  • 1
    For future reference: using mcrypt is a bad idea. It has security holes and unpatched bugs and is even deprecated in PHP 7. – PoeHaH Sep 14 '17 at 03:09
0

One way to do this is using XOR encryption wikipedia. In this case you will have to use a "key" to encode and decode the text.

There is an example php.net for php, look at the example#2

Bakudan
  • 19,134
  • 9
  • 53
  • 73
0

It is not in the php core but you can take a look at

http://php.net/manual/en/function.mcrypt-decrypt.php and http://php.net/manual/en/function.mcrypt-encrypt.php where you can crypt and decrypt strings with a valid key.

There is a good post about it at Best way to use PHP to encrypt and decrypt passwords?

Community
  • 1
  • 1
Mattias
  • 9,211
  • 3
  • 42
  • 42
-1
$string === str_rot13( str_rot13($string) );
miki
  • 695
  • 3
  • 8
  • unless you wanna use something like open ssl, which would be a total overkill here, nothing really is "encryption" – scibuff Feb 26 '12 at 09:00
  • though he indeed says encryption in the question, i believe str_rot13 qualifies for "something weird" – miki Feb 26 '12 at 09:02
  • True. For what OP seems to want rot13 would probably work, but it should be noted that it is not, in fact, encryption. – HellaMad Feb 26 '12 at 09:03
  • in fact, it is, it's just that the knowledge for decryption is readily available – miki Feb 26 '12 at 09:08
-2

If u want to use any encryption then u can use 'openssl' in which u can use any reverseble encryptiopn algorithm to encrypt and decrypt. I have used this in php and works just fine.

All details are on this page : http://php.net/manual/en/book.openssl.php

Pankaj Anand
  • 475
  • 1
  • 6
  • 15