# A good way to encrypt whatever you want

**URL:** <https://forum.kirupa.com/t/a-good-way-to-encrypt-whatever-you-want/70532>\
**Category:** Uncategorized\
**Created:** [November 15, 2004, 4:31am UTC](https://forum.kirupa.com/t/a-good-way-to-encrypt-whatever-you-want/70532 "2004-11-15T04:31:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfino](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@alfino](https://forum.kirupa.com/u/alfino)\
**Post date:** [November 15, 2004, 4:31am UTC](https://forum.kirupa.com/t/a-good-way-to-encrypt-whatever-you-want/70532/1 "2004-11-15T04:31:44Z")

</div>

```php

<?php
function get_rnd_iv($iv_len)
{
   $iv = '';
   while ($iv_len-- > 0) {
	   $iv .= chr(mt_rand() & 0xff);
   }
   return $iv;
}
function md5_encrypt($plain_text, $password, $iv_len = 16)
{
   $plain_text .= "\x13";
   $n = strlen($plain_text);
   if ($n % 16) $plain_text .= str_repeat("\0", 16 - ($n % 16));
   $i = 0;
   $enc_text = get_rnd_iv($iv_len);
   $iv = substr($password ^ $enc_text, 0, 512);
   while ($i < $n) {
	   $block = substr($plain_text, $i, 16) ^ pack('H*', md5($iv));
	   $enc_text .= $block;
	   $iv = substr($block . $iv, 0, 512) ^ $password;
	   $i += 16;
   }
   return base64_encode($enc_text);
}
function md5_decrypt($enc_text, $password, $iv_len = 16)
{
   $enc_text = base64_decode($enc_text);
   $n = strlen($enc_text);
   $i = $iv_len;
   $plain_text = '';
   $iv = substr($password ^ substr($enc_text, 0, $iv_len), 0, 512);
   while ($i < $n) {
	   $block = substr($enc_text, $i, 16);
	   $plain_text .= $block ^ pack('H*', md5($iv));
	   $iv = substr($block . $iv, 0, 512) ^ $password;
	   $i += 16;
   }
   return preg_replace('/\\x13\\x00*$/', '', $plain_text);
}
/ ****************************************** /
$text = "321";
$password = "123";
/*
echo "plain text is: [${plain_text}]<br />
";
echo "password is: [${password}]<br />
";
$enc_text = md5_encrypt($plain_text, $password);
echo "encrypted text is: [${enc_text}]<br />
";
$plain_text2 = md5_decrypt($enc_text, $password);
echo "decrypted text is: [${plain_text2}]<br />
";
*/
?>

```

I found this on the PHP website. I’ve used it and it’s good. 🙂
