/encrypt.class.php

https://github.com/nootanghimire/php-classes · PHP · 178 lines · 123 code · 29 blank · 26 comment · 9 complexity · 684d2ead8df6eb5997c51925f4e1c986 MD5 · raw file

  1. <?php
  2. /**
  3. * encrypt.class.php -- class for controlling encryption and decryption
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt. If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category Core_Classes
  14. * @package Security_Module
  15. * @author Nootan Ghimire <nootan.ghimire@gmail.com>
  16. * @copyright 2012 Nootan Ghimire
  17. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  18. * @link http://www.facebook.com/noootan.ghimire
  19. */
  20. class encrypt {
  21. protected $a_nam, $a_mod, $iv, $skey, $encrypted_data, $decrypted_data, $td;
  22. protected $a_dir="", $a_mod_dir="";
  23. public $ClearedOn=0;
  24. /**
  25. * constructer function
  26. *
  27. * @param $alg_name
  28. * @param $alg_mode
  29. *
  30. * @return void
  31. */
  32. public function __construct($alg_name, $alg_mode) {
  33. $this->a_nam=$alg_name;
  34. $this->a_mod=$alg_mode;
  35. $this->td=mcrypt_module_open($this->a_nam,$this->a_dir,$this->a_mod,$this->a_mod_dir);
  36. }
  37. public function get_td() {
  38. return $this->td;
  39. }
  40. public function change_algorithm($new_algorithm){
  41. $this->a_nam=$new_algorithm;
  42. }
  43. public function change_mode($new_mode) {
  44. $this->a_nam=$new_mode;
  45. }
  46. public function set_iv($iv){
  47. $this->iv=$iv;
  48. }
  49. public function set_algortihm_dir($dir) {
  50. $this->a_dir=$dir;
  51. }
  52. public function set_mode_dir($dir) {
  53. $this->a_mod_dir=$dir;
  54. }
  55. public function set_key($skey) {
  56. $this->skey=$skey;
  57. }
  58. protected function make_iv() {
  59. if((!isset($this->iv)) || $this->iv==""){
  60. $this->iv=mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td),MCRYPT_DEV_RANDOM);
  61. }
  62. }
  63. protected function init() {
  64. mcrypt_generic_init($this->td,$this->skey, $this->iv);
  65. }
  66. protected function deinit() {
  67. mcrypt_generic_deinit($this->td);
  68. }
  69. public function get_iv($type="true"){
  70. if($type=="encoded"){
  71. return base64_encode($this->iv);
  72. }
  73. else{
  74. return $this->iv;
  75. }
  76. }
  77. public function close_module(){
  78. if(isset($this->td) || (!$this->td="")){
  79. mcrypt_module_close($this->td);
  80. }
  81. }
  82. protected function encrypt($key, $data){
  83. self::close_module();
  84. self::__construct($this->a_nam, $this->a_mod);
  85. self::set_key($key);
  86. self::make_iv();
  87. self::init();
  88. $enc=mcrypt_generic($this->td,$data);
  89. $this->encrypted_data=$enc;
  90. self::deinit();
  91. }
  92. protected function decrypt($key, $edata, $iv, $bool_base64){
  93. self::close_module();
  94. self::__construct($this->a_nam, $this->a_mod);
  95. self::set_key($key);
  96. self::set_iv($iv);
  97. self::make_iv();
  98. self::init();
  99. if($bool_base64){
  100. $data = base64_decode($edata);
  101. } else {
  102. $data = $edata;
  103. }
  104. $dec=mdecrypt_generic($this->td, $data);
  105. $this->decrypted_data=$dec;
  106. self::deinit();
  107. }
  108. public function getEncrypted($data, $key, $base64){
  109. self::encrypt($key, $data);
  110. if($base64) {
  111. return base64_encode($this->encrypted_data);
  112. }
  113. else {
  114. return $this->encrypted_data;
  115. }
  116. }
  117. public function getDecrypted($data, $key, $iv, $bool) {
  118. self::decrypt($key, $data, $iv, $bool);
  119. return $this->decrypted_data;
  120. }
  121. public function get_iv_size() {
  122. return mcrypt_enc_get_iv_size($this->td);
  123. }
  124. public function get_key() {
  125. return $this->skey;
  126. }
  127. public function get_last_encrypted_data(){
  128. return $this->encrypted_data;
  129. }
  130. public function get_last_decrypted_data() {
  131. return $this->decrypted_data;
  132. }
  133. public function get_key_size() {
  134. return mcrypt_enc_get_key_size($this->td);
  135. }
  136. public function ClearAllData() {
  137. self::close_module();
  138. $this->td="";
  139. $this->a_dir="";
  140. $this->a_nam="";
  141. $this->a_mod="";
  142. $this->a_mod_dir="";
  143. $this->skey="";
  144. $this->iv="";
  145. $this->encrypted_data="";
  146. $this->decrypted_data="";
  147. $this->ClearedOn=time();
  148. }
  149. } //End class