1

Does anyone know do there have any way that I can encrypt the array in php??

for example:

$arr_value = array("1","2","3","4");

any way that I can encrypt $arr_value, also decrypt it later ini php?

Jin Yong
  • 42,698
  • 72
  • 141
  • 187

3 Answers3

1

First, see this please.

You can encrypt/decrypt something like this:

$arr_value = array("1","2","3","4");

function encrypt($text)
{
   return base64_encode($text);
}

function decrypt($text)
{
   return base64_decode($text);
}

Now to encrypt:

$encrypted = array_map("encrypt", $arr_value);
echo '<pre>';
print_r($encrypted);

And to decrypt:

$decrypted = array_map("decrypt", $arr_value);
echo '<pre>';
print_r($decrypted);

.

Note:

It is worth having a look at a better way of encryption library:

The mcrypt library.

Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • hmm... but it seem like only allow to encript for string but not array – Jin Yong Apr 08 '10 at 06:42
  • @Jin Yong: No actually the `array_map` function walks through entire array thus encrypting/decrypting each value of the array hence the entire array :) – Sarfraz Apr 08 '10 at 06:44
  • right, but do there have any way that we can use base64_encode come with password ot secret code? – Jin Yong Apr 08 '10 at 06:53
  • @Jin Yong: Yes you can encrypt secret codes of course. I would suggest you to encrypt like told in the link i posted on top of my answer. Thanks – Sarfraz Apr 08 '10 at 06:56
  • i tried the code that show on the link... Fatal error: Call to undefined function mcrypt_encrypt() show when i run the code – Jin Yong Apr 08 '10 at 06:59
  • @Jin Yong: because you are getting the error unknonw mycrupt library, it seems that it has not been enabled there on your server. You can use my code which doesn't have any dependencies. – Sarfraz Apr 08 '10 at 07:14
0

You might enjoy a look at mcrypt. I'm not sure where you're storing the encrypted values, or what cipher you want to use. mcrypt should let you accomplish whatever you need.

Examples are here.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
0

if you encrypt/decrypt string then use .

$str_value = $arr_value.join(",");

encrypt/decrypt $str_value

$arr_value=$str_value.split(",")
Salil
  • 46,566
  • 21
  • 122
  • 156