1

Possible Duplicate:
Best way to use PHP to encrypt and decrypt?

I am new in Two Way crypting technology in PHP. I have used sha512 as one way hashing, but now I really need two way ecryption. and I don't know where to start. Can you tell me which method I must use for the most security? and can you give me simple "hello world" example?

Edited

Thank you for all answers, I found mCrypt as an key for my problem, but I can use a lot of methods like MCRYPT_3DES, MCRYPT_CAST_128, MCRYPT_CAST_256.... so what to use?

Community
  • 1
  • 1
Irakli
  • 1,151
  • 5
  • 30
  • 55
  • 1
    try searching. There is plenty of information out there on how to do encryption with PHP. [Here On SO](http://stackoverflow.com/questions/5089841/php-2-way-encryption-i-need-to-store-passwords-that-can-be-retrieved/5093422#5093422), [On PHP.Net](http://php.net/manual/en/book.mcrypt.php), [Another On SO](http://stackoverflow.com/questions/1289061/best-way-to-use-php-to-encrypt-and-decrypt) – ircmaxell Feb 24 '12 at 18:00
  • sha512 is one way crypting system, that I use in PHP, I have googled, but is mCrypt installed as default in servers? and which method is most safe from mCrypt methods? – Irakli Feb 24 '12 at 18:00
  • @user1228636: No it's not. It's a cryptographic hashing function. See [this answer](http://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms/4948393#4948393) for more info – ircmaxell Feb 24 '12 at 18:01
  • There is plant of methods that I can use in mCrypt, can you suggest me the most secure one? – Irakli Feb 24 '12 at 18:04

3 Answers3

0

You can try using openssl functions for what you need: openssl_public_encrypt and openssl_private_decrypt.

To use them you will have to generate yourself a RSA public/private key pair.

gintas
  • 2,118
  • 1
  • 18
  • 28
  • Thank you for your answer, but can you suggest 1 or 2 more methods too, to have the choosing opportunity – Irakli Feb 24 '12 at 18:05
0

Don't mix up hashing with encryption.

You could use the mcrypt-expansion, its well documented, and they have some good examples, like this one:

 $key = "this is a secret key";
 $input = "This is a top secrep message.";

 $td = mcrypt_module_open('tripledes', '', 'ecb', '');
 $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 mcrypt_generic_init($td, $key, $iv);
 $encrypted_data = mcrypt_generic($td, $input);
 mcrypt_generic_deinit($td);
 mcrypt_module_close($td);

There are many other methods out there, openssl is one of them.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • I can use a lot of methods like MCRYPT_3DES, MCRYPT_CAST_128, MCRYPT_CAST_256.... so what to use? – Irakli Feb 24 '12 at 18:10
  • Whatever you want. The library supports a few algorithms, see here for more details: http://mcrypt.hellug.gr/#_mcrypt. Currently it seems to support BLOWFISH, TWOFISH, DES, TripleDES, 3-WAY, SAFER, LOKI97, GOST, RC2, MARS, RIJNDAEL, SERPENT, CAST, ARCFOUR and WAKE. I'm not an expert on the topic, so I'm not sure which one is safer than others (I'd advise against using DES). – Bjoern Feb 24 '12 at 19:09
0

You could make use of mcrypt which comes with PHP

This is the PHP Manual for Mcrypt (Manual is always the best to start with)

Since you have asked for an easy hello-world type. Have a look here in this link.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • Yes there is manual about mCrypt but with mCrypt, I think, I can use a lot of methods like MCRYPT_3DES, MCRYPT_CAST_128, MCRYPT_CAST_256.... so what to use? – Irakli Feb 24 '12 at 18:08