Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cryptography.class.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. Class Cryptography
  3. {
  4. function Encrypt($key, $iv, $source, $destination, $cipher) {
  5. if (extension_loaded('mcrypt') === true)
  6. {
  7. if (is_file($source) === true)
  8. {
  9. $source = file_get_contents($source);
  10. switch($cipher)
  11. {
  12. case 'tripleDES':
  13. $encryptedSource=$this->TripleDesEncrypt($source,$key,$iv);
  14. break;
  15. case 'AES':
  16. $encryptedSource=$this->AESEncrypt($source,$key,$iv);
  17. break;
  18. default:
  19. $encryptedSource=$this->TripleDesEncrypt($source,$key,$iv);
  20. break;
  21. }
  22. if (file_put_contents($destination,$encryptedSource, LOCK_EX) !== false)
  23. {
  24. return true;
  25. }
  26. return false;
  27. }
  28. return false;
  29. }
  30. return false;
  31. }
  32. function Decrypt($key, $iv, $source, $destination, $cipher) {
  33. if (extension_loaded('mcrypt') === true)
  34. {
  35. if (is_file($source) === true)
  36. {
  37. $source = file_get_contents($source);
  38. switch($cipher)
  39. {
  40. case 'tripleDES':
  41. $decryptedSource=self::TripleDesDecrypt($source,$key,$iv);
  42. break;
  43. case 'AES':
  44. $decryptedSource=self::AESDecrypt($source,$key,$iv);
  45. break;
  46. default:
  47. $decryptedSource=self::TripleDesDecrypt($source,$key,$iv);
  48. break;
  49. }
  50. if (file_put_contents($destination,$decryptedSource, LOCK_EX) !== false)
  51. {
  52. return true;
  53. }
  54. echo "no read";
  55. return false;
  56. }
  57. echo "no file";
  58. return false;
  59. }
  60. echo "no mcrypt";
  61. return false;
  62. }
  63. /*
  64. Apply tripleDES algorthim for encryption, append "___EOT" to encrypted file ,
  65. so that we can remove it while decrpytion also padding 0's
  66. */
  67. function TripleDesEncrypt($buffer,$key,$iv) {
  68. $cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', '');
  69. $buffer.='___EOT';
  70. // get the amount of bytes to pad
  71. $extra = 8 - (strlen($buffer) % 8);
  72. // add the zero padding
  73. if($extra > 0) {
  74. for($i = 0; $i < $extra; $i++) {
  75. $buffer .= '_';
  76. }
  77. }
  78. mcrypt_generic_init($cipher, $key, $iv);
  79. $result = mcrypt_generic($cipher, $buffer);
  80. mcrypt_generic_deinit($cipher);
  81. return base64_encode($result);
  82. }
  83. /*
  84. Apply tripleDES algorthim for decryption, remove "___EOT" from encrypted file ,
  85. so that we can get the real data.
  86. */
  87. function TripleDesDecrypt($buffer,$key,$iv) {
  88. $buffer= base64_decode($buffer);
  89. $cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', '');
  90. mcrypt_generic_init($cipher, $key, $iv);
  91. $result = mdecrypt_generic($cipher,$buffer);
  92. $result=substr($result,0,strpos($result,'___EOT'));
  93. mcrypt_generic_deinit($cipher);
  94. return $result;
  95. }
  96. /*
  97. Apply AES algorthim for encryption, append "___EOT" to encrypted file ,
  98. so that we can remove it while decrpytion also padding 0's
  99. */
  100. function AESEncrypt($buffer,$key,$iv) {
  101. $cipher = mcrypt_module_open('rijndael-256', '', 'cbc', '');
  102. $buffer.='___EOT';
  103. // get the amount of bytes to pad
  104. $extra = 8 - (strlen($buffer) % 8);
  105. // add the zero padding
  106. if($extra > 0) {
  107. for($i = 0; $i < $extra; $i++) {
  108. $buffer .= '_';
  109. }
  110. }
  111. mcrypt_generic_init($cipher, $key, $iv);
  112. $result = mcrypt_generic($cipher, $buffer);
  113. mcrypt_generic_deinit($cipher);
  114. return base64_encode($result);
  115. }
  116. /*
  117. Apply AES algorthim for decryption, remove "___EOT" from encrypted file ,
  118. so that we can get the real data.
  119. */
  120. function AESDecrypt($buffer,$key,$iv) {
  121. $buffer= base64_decode($buffer);
  122. $cipher = mcrypt_module_open('rijndael-256', '', 'cbc', '');
  123. mcrypt_generic_init($cipher, $key, $iv);
  124. $result = mdecrypt_generic($cipher,$buffer);
  125. $result=substr($result,0,strpos($result,'___EOT'));
  126. mcrypt_generic_deinit($cipher);
  127. return $result;
  128. }
  129. }
  130. ?>