How to encrypt in javascript and decrypt that in php with out using any external file -


this code take input , encrypt content java script
entire code available in github.you guys can search encrypt_js_decrypt_php. problem running since long.i have come solution.just import localhost.

<input type="text" id="code" name="code"/> <input type="submit" name="submit" value="submit" onclick="return encryptcode();"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript"> function rc4(key, str) {     var s = [], j = 0, x, res = '';     (var = 0; < 256; i++)      {         s[i] = i;     }     (i = 0; < 256; i++)      {         j = (j + s[i] + key.charcodeat(i % key.length)) % 256;         x = s[i];         s[i] = s[j];         s[j] = x;     }     = 0;     j = 0;     (var y = 0; y < str.length; y++)      {         = (i + 1) % 256;         j = (j + s[i]) % 256;         x = s[i];         s[i] = s[j];         s[j] = x;         res += string.fromcharcode(str.charcodeat(y) ^ s[(s[i] + s[j]) % 256]);     }     return res; }  function encryptcode() {   var value = document.getelementbyid("code").value;   var key = "secretkeytoprovide";  /*--provide secret key here--*/   var codevalue = rc4(key, value);   var arr = {code:codevalue, age:25};   $.ajax({                 url: "response.php",                 type: "post",                 data: json.stringify(arr),                 datatype: 'json',                 async: false,                 contenttype: 'application/json; charset=utf-8',                 success: function(data)                  {                     alert(data);                 }             });    } </script> </html> 

now,lets decrypt code in php

<?php  function mb_chr($char)  {     return mb_convert_encoding('&#'.intval($char).';', 'utf-8', 'html-entities'); }  function mb_ord($char) {     $result = unpack('n', mb_convert_encoding($char, 'ucs-4be', 'utf-8'));     if (is_array($result) === true)      {         return $result[1];     }         return ord($char); }  function rc4($key, $str)  {        if (extension_loaded('mbstring') === true)      {         mb_language('neutral');         mb_internal_encoding('utf-8');         mb_detect_order(array('utf-8', 'iso-8859-15', 'iso-8859-1', 'ascii'));     }     $s = array();     ($i = 0; $i < 256; $i++)     {         $s[$i] = $i;     }     $j = 0;     ($i = 0; $i < 256; $i++)     {         $j = ($j + $s[$i] + mb_ord(mb_substr($key, $i % mb_strlen($key), 1))) % 256;         $x = $s[$i];         $s[$i] = $s[$j];         $s[$j] = $x;     }     $i = 0;     $j = 0;     $res = '';     ($y = 0; $y < mb_strlen($str); $y++)     {         $i = ($i + 1) % 256;         $j = ($j + $s[$i]) % 256;         $x = $s[$i];         $s[$i] = $s[$j];         $s[$j] = $x;         $res .= mb_chr(mb_ord(mb_substr($str, $y, 1)) ^ $s[($s[$i] + $s[$j]) % 256]);     }     return $res; }  $request_body = file_get_contents('php://input'); $json = json_decode($request_body); $secretcode =$json->code ; $age =$json->age  ; $key = "secretkeytoprovide";  /*--provide secret key here have given in javascript--*/ $decryptedsecretcode  = rc4($key, $secretcode) ; echo $decryptedsecretcode; exit; ?> 


Comments