EncryptionController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Http\Controllers;
  3. use ErrorException;
  4. use SodiumException;
  5. class EncryptionController extends Controller{
  6. private $public_key = "WrBEGoJnMnMhC/2hiaYypcmPwP9Z4p4/bNex0T/WUWA=";
  7. private $secret_key = "CEIBkv7QG1WoAoBO7Ny5cqzyJ5yCUwBMhnFYUBbCeTk=";
  8. private function encrypt_old($msg){
  9. $nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
  10. $nonceArr = array();
  11. for($i = 0; $i < strlen($nonce); $i++){
  12. $char = $nonce[$i];
  13. $ascii = ord($char);
  14. $nonceArr[] = $ascii;
  15. }
  16. $nonceJson = json_encode($nonceArr);
  17. $nonceStr = base64_encode($nonceJson);
  18. $encryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey(
  19. base64_decode($this->secret_key),
  20. base64_decode($this->public_key));
  21. $encrypted = sodium_crypto_box($msg, $nonce, $encryption_key);
  22. $encryptedArr = array();
  23. for($i = 0; $i < strlen($encrypted); $i++){
  24. $char = $encrypted[$i];
  25. $ascii = ord($char);
  26. $encryptedArr[] = $ascii;
  27. }
  28. $encryptedJson = json_encode($encryptedArr);
  29. $encryptedStr = base64_encode($encryptedJson);
  30. return base64_encode($encryptedStr . "|" . $nonceStr);
  31. }
  32. private function decrypt_old($enc){
  33. $dec = base64_decode($enc);
  34. $decArr = explode("|", $dec);
  35. if(count($decArr) < 2){
  36. return false;
  37. }
  38. $nonceDec = base64_decode($decArr[1]);
  39. $caddec = base64_decode($decArr[0]);
  40. $nonceArr = json_decode($nonceDec);
  41. $cadArr = json_decode($caddec);
  42. $nonceF = "";
  43. if(is_null($nonceArr)) return false;
  44. foreach($nonceArr as $asciiF){
  45. $nonceF .= chr($asciiF);
  46. }
  47. $cadF = "";
  48. foreach($cadArr as $asciiF){
  49. $cadF .= chr($asciiF);
  50. }
  51. $encryption_key_dec = sodium_crypto_box_keypair_from_secretkey_and_publickey(
  52. base64_decode($this->secret_key),
  53. base64_decode($this->public_key)
  54. );
  55. $decrypted = sodium_crypto_box_open($cadF, $nonceF, $encryption_key_dec);
  56. return $decrypted;
  57. }
  58. public function shortEnc($str){
  59. $nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
  60. $encKey = sodium_crypto_box_keypair_from_secretkey_and_publickey(
  61. base64_decode($this->secret_key),
  62. base64_decode($this->public_key)
  63. );
  64. $strEnc = sodium_crypto_box($str, $nonce, $encKey);
  65. return base64_encode($strEnc . "||" . $nonce);
  66. }
  67. public function encrypt(string $value) : string {
  68. $nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
  69. $encryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey(
  70. base64_decode($this->secret_key),
  71. base64_decode($this->public_key));
  72. $encrypted = sodium_crypto_box($value, $nonce, $encryption_key);
  73. $encrypted_string = base64_encode($encrypted);
  74. $nonce_string = base64_encode($nonce);
  75. return base64_encode($encrypted_string . "|" . $nonce_string);;
  76. }
  77. public function decrypt($value) {
  78. if(gettype($value) != 'string') return false;
  79. $value_string = base64_decode($value);
  80. $value_array = explode('|', $value_string);
  81. if(count($value_array) < 2) return false;
  82. $encoded_string = base64_decode($value_array[0]);
  83. $nonce_string = base64_decode($value_array[1]);
  84. $encryption_key_dec = sodium_crypto_box_keypair_from_secretkey_and_publickey(
  85. base64_decode($this->secret_key),
  86. base64_decode($this->public_key)
  87. );
  88. try{
  89. $decrypted = sodium_crypto_box_open($encoded_string, $nonce_string, $encryption_key_dec);
  90. return $decrypted;
  91. }catch(ErrorException $e){
  92. return false;
  93. }catch(SodiumException $e){
  94. return false;
  95. }
  96. }
  97. public function shortDec($enc){
  98. $enc = str_replace("=P=", "+", $enc);
  99. $enc = str_replace("=S=", "/", $enc);
  100. $strEnc = base64_decode($enc);
  101. $arrEnc = explode("||", $strEnc);
  102. $decKey = sodium_crypto_box_keypair_from_secretkey_and_publickey(
  103. base64_decode($this->secret_key),
  104. base64_decode($this->public_key)
  105. );
  106. try{
  107. return sodium_crypto_box_open($arrEnc[0], $arrEnc[1], $decKey);
  108. }catch(ErrorException $e){
  109. return false;
  110. }catch(SodiumException $e){
  111. return false;
  112. }
  113. }
  114. }