Posted By

MMDeveloper on 09/16/08


Tagged

php crypt decrypt encrypt


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Scooter


Crypt Class


 / Published in: PHP
 

URL: http://www.mechanicmatt.com

  1. <?php
  2. class class_crypt {
  3. var $algorithms = NULL;
  4. var $mode = NULL;
  5. var $key = NULL;
  6. var $algorithm = NULL;
  7.  
  8. var $iv = NULL;
  9. var $iv_size = NULL;
  10.  
  11. function __construct() {
  12. $this->crypt();
  13. }
  14.  
  15. function crypt($alg = 13, $key = "") {
  16. $this->algorithms = array (
  17. MCRYPT_3DES,
  18. MCRYPT_BLOWFISH,
  19. MCRYPT_CAST_256,
  20. MCRYPT_GOST,
  21. MCRYPT_LOKI97,
  22. MCRYPT_RIJNDAEL_128,
  23. MCRYPT_RIJNDAEL_192,
  24. MCRYPT_RIJNDAEL_256,
  25. MCRYPT_RC2,
  26. MCRYPT_SAFERPLUS,
  27. MCRYPT_SERPENT,
  28. MCRYPT_TRIPLEDES,
  29. MCRYPT_TWOFISH,
  30. "xor"
  31. );
  32.  
  33. $this->algorithm = MCRYPT_CAST_256;
  34. $this->mode = MCRYPT_MODE_NOFB;
  35. $this->key = ($key != "") ? $key : "JF^#@&*973LSD38)(@#&_9&@#*";
  36.  
  37. $this->algorithm = $this->algorithms[intval($alg)];
  38. if ($this->algorithm == "") {
  39. $this->algorithm = $this->algorithms[13];
  40. } else {}
  41.  
  42. $this->iv_size = mcrypt_get_iv_size($this->algorithm, $this->mode);
  43. $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND);
  44. }
  45.  
  46.  
  47. function set_key($key) {
  48. $this->key = $key;
  49. }
  50.  
  51. function encrypt($data, $iv_len = 16) {
  52. switch($this->algorithm) {
  53. case "xor":
  54. $data .= "\x13";
  55. $n = strlen($data);
  56. if ($n % 16) {
  57. $data .= str_repeat("\0", 16 - ($n % 16));
  58. $i = 0;
  59. $data = $this->get_rnd_iv($iv_len);
  60. $iv = substr($this->key ^ $data, 0, 512);
  61. while ($i < $n) {
  62. $block = substr($data, $i, 16) ^ pack('H*', sha1($iv));
  63. $data .= $block;
  64. $iv = substr($block . $iv, 0, 512) ^ $this->key;
  65. $i += 16;
  66. }
  67. return base64_encode($data);
  68. } else {}
  69. break;
  70.  
  71. default:
  72. return mcrypt_encrypt($this->algorithm, $this->key, $data, $this->mode, $this->iv);
  73. break;
  74. }
  75. }
  76.  
  77. function decrypt($data, $iv_len = 16) {
  78. switch($this->algorithm) {
  79. case "xor":
  80. $data = base64_decode($data);
  81. $n = strlen($data);
  82. $i = $iv_len;
  83. $plain_text = '';
  84. $iv = substr($this->key ^ substr($data, 0, $iv_len), 0, 512);
  85. while ($i < $n) {
  86. $block = substr($data, $i, 16);
  87. $plain_text .= $block ^ pack("H*", sha1($iv));
  88. $iv = substr($block . $iv, 0, 512) ^ $this->key;
  89. $i += 16;
  90. }
  91. return stripslashes(preg_replace("/\\x13\\x00*$/", "", $plain_text));
  92. break;
  93.  
  94. default:
  95. return mcrypt_decrypt($this->algorithm, $this->key, $data, $this->mode, $this->iv);
  96. break;
  97. }
  98. }
  99. }
  100.  
  101. $crypt = new class_crypt(13, "Your Secret Private Key");
  102. $crypt->encrypt("raw data to encrypt");
  103. $crypt->decrypt("encrypted data to decrypt");
  104. ?>

Report this snippet  

You need to login to post a comment.