/classes/AES.class.php

https://github.com/leemcd56/Minecraft-PHP-Client-2 · PHP · 147 lines · 80 code · 24 blank · 43 comment · 4 complexity · 088cf9be6f03c96fcc4ce218514d6599 MD5 · raw file

  1. <?php
  2. /*
  3. -
  4. / \
  5. / \
  6. / MINECRAFT \
  7. / PHP \
  8. |\ CLIENT /|
  9. |. \ 2 / .|
  10. | .. \ / .. |
  11. | .. | .. |
  12. | .. | .. |
  13. \ | /
  14. \ | /
  15. \ | /
  16. \ | /
  17. by @shoghicp
  18. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  19. Version 2, December 2004
  20. Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  21. Everyone is permitted to copy and distribute verbatim or modified
  22. copies of this license document, and changing it is allowed as long
  23. as the name is changed.
  24. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  25. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  26. 0. You just DO WHAT THE FUCK YOU WANT TO.
  27. */
  28. /*
  29. Incomplete AES class with buffering
  30. Only CFB8
  31. */
  32. if(CRYPTO_LIB === "openssl"){
  33. class AES{
  34. private $key, $encIV, $decIV, $IVLenght, $bytes, $mode;
  35. function __construct($bits, $mode, $blockSize){
  36. console("[DEBUG] [AES] Using OpenSSL extension", true, true, 2);
  37. $this->mode = "AES-".$bits."-".strtoupper($mode).$blockSize;
  38. $this->bytes = $blockSize >> 3;
  39. $this->key = $this->encIV = $this->decIV = str_repeat("\x00", openssl_cipher_iv_length($this->mode));
  40. $this->IVLenght = openssl_cipher_iv_length($this->mode);
  41. }
  42. public function setKey($key = ""){
  43. $this->key = str_pad($key, $this->IVLenght, "\x00", STR_PAD_RIGHT);
  44. }
  45. public function setIV($IV = ""){
  46. $this->encIV = $this->decIV = str_pad($IV, $this->IVLenght, "\x00", STR_PAD_RIGHT);
  47. }
  48. protected function _shiftIV($IV, $str){ //Only for CFB
  49. if(!isset($str{$this->IVLenght - 1})){
  50. $len = min($this->IVLenght, strlen($str));
  51. return substr($IV, $len).substr($str, -$len);
  52. }
  53. return substr($str, -$this->IVLenght);
  54. }
  55. public function encrypt($plaintext){
  56. $ciphertext = openssl_encrypt($plaintext, $this->mode, $this->key, true, $this->encIV);
  57. $this->encIV = $this->_shiftIV($this->encIV, $ciphertext);
  58. return $ciphertext;
  59. }
  60. public function decrypt($ciphertext){
  61. $plaintext = openssl_decrypt($ciphertext, $this->mode, $this->key, true, $this->decIV);
  62. $this->decIV = $this->_shiftIV($this->decIV, $ciphertext);
  63. return $plaintext;
  64. }
  65. public function init(){
  66. }
  67. }
  68. }else{ //mcrypt
  69. class AES{
  70. private $key, $keyLenght, $IV, $IVLenght, $enc, $dec, $mode, $algorithm;
  71. function __construct($bits, $mode, $blockSize){
  72. console("[DEBUG] [AES] Using Mcrypt extension", true, true, 2);
  73. $this->algorithm = "rijndael-".intval($bits);
  74. $this->mode = strtolower($mode);
  75. $mcrypt = mcrypt_module_open($this->algorithm, "", $this->mode, "");
  76. $this->IVLenght = mcrypt_enc_get_iv_size($mcrypt);
  77. mcrypt_module_close($mcrypt);
  78. $this->keyLenght = $bits >> 3;
  79. $this->setKey();
  80. $this->setIV();
  81. $this->init();
  82. }
  83. public function init(){
  84. if(is_resource($this->enc)){
  85. mcrypt_generic_deinit($this->enc);
  86. mcrypt_module_close($this->enc);
  87. }
  88. $this->enc = mcrypt_module_open($this->algorithm, "", $this->mode, "");
  89. mcrypt_generic_init($this->enc, $this->key, $this->IV);
  90. if(is_resource($this->dec)){
  91. mcrypt_generic_deinit($this->dec);
  92. mcrypt_module_close($this->dec);
  93. }
  94. $this->dec = mcrypt_module_open($this->algorithm, "", $this->mode, "");
  95. mcrypt_generic_init($this->dec, $this->key, $this->IV);
  96. }
  97. public function setKey($key = ""){
  98. $this->key = str_pad($key, $this->keyLenght, "\x00", STR_PAD_RIGHT);
  99. }
  100. public function setIV($IV = ""){
  101. $this->IV = str_pad($IV, $this->IVLenght, "\x00", STR_PAD_RIGHT);
  102. }
  103. public function encrypt($plaintext){
  104. return mcrypt_generic($this->enc, $plaintext);
  105. }
  106. public function decrypt($ciphertext){
  107. return mdecrypt_generic($this->dec, $ciphertext);
  108. }
  109. }
  110. }