EncryptionController.php 4.5 KB

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