Giter Club home page Giter Club logo

Comments (16)

brainfoolong avatar brainfoolong commented on July 20, 2024

Please post all code, without knowing the "some_encrypted_data" and "some_data" i cannot help you.

from cryptojs-aes-php.

fcarstensen avatar fcarstensen commented on July 20, 2024

I'm sorry, you're right, here comes the code:

private function cryptoJsAesEncrypt($passphrase, $value)
{
    $salt = openssl_random_pseudo_bytes(8);
    $salted = '';
    $dx = '';
    while (strlen($salted) < 48) {
        $dx = md5($dx.$passphrase.$salt, true);
        $salted .= $dx;
    }
    $key = substr($salted, 0, 32);
    $iv  = substr($salted, 32,16);
    $encrypted_data = openssl_encrypt(json_encode($value), 'aes-256-cbc', $key, true, $iv);
    $data = array("ct" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "s" => bin2hex($salt));
    return json_encode($data);
}

private function getTopClanToonsCount()
{    
    $sql = "SELECT COUNT( t.game_id ) AS toon_amount, t.toon_clan
            FROM toon t
            WHERE t.deleted =0
            AND t.toon_clan != ''
            GROUP BY t.toon_clan
            ORDER BY toon_amount DESC 
            LIMIT 5";

    $stmt = DB::getInstance()->prepare($sql);
    $stmt->execute();

    $clanCount = $stmt->fetchAll();
        if (is_array($clanCount))
        {
            return $this->cryptoJsAesEncrypt('test123', $clanCount);
        }
    return null;
}
      function getTopClanToons(){  
      $http.post("../some_url.php?action=getTopClanToonsCount").success(function(data){   

            var CryptoJSAesJson = {
                stringify: function (cipherParams) {
                    var j = {ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)};
                    if (cipherParams.iv) j.iv = cipherParams.iv.toString();
                    if (cipherParams.salt) j.s = cipherParams.salt.toString();
                    return JSON.stringify(j);
                },
                parse: function (jsonStr) {
                    var j = JSON.parse(jsonStr);
                    var cipherParams = CryptoJS.lib.CipherParams.create({ciphertext: CryptoJS.enc.Base64.parse(j.ct)});
                    if (j.iv) cipherParams.iv = CryptoJS.enc.Hex.parse(j.iv)
                    if (j.s) cipherParams.salt = CryptoJS.enc.Hex.parse(j.s)
                    return cipherParams;
                }
            }

            console.log(JSON.parse(CryptoJS.AES.decrypt(data, "test123", {format: CryptoJSAesJson}).toString(CryptoJS.enc.Utf8)));
            //$scope.topClanToonsCount = JSON.parse(CryptoJS.AES.decrypt(data, "test", {format: CryptoJSAesJson}).toString(CryptoJS.enc.Utf8));
            //$scope.topClanToonsCount = data;
           });
      }; 

I hope this will help you... and finally me ;)

from cryptojs-aes-php.

fcarstensen avatar fcarstensen commented on July 20, 2024

This is a php result after the cryptoJsAesEncrypt:

"{\"ct\":\"OBPmdQVbGuxXVl7MBZCntJwVnDFWnyqIif2B1YyH7BVn4eROzq4i0cus5g3+ZPDTBQbcSe+blmYsc9p7VYb98koVUy06qjrLXr9IPjt9uR\\\/o4aFWd1tdlLQHvtE90D+b\",\"iv\":\"ab044b14232442e4e48f06c195e3b7b4\",\"s\":\"39b096acaba76884\"}"

from cryptojs-aes-php.

brainfoolong avatar brainfoolong commented on July 20, 2024

What data is in data of the JS callback, do a console.log(data) and post the result.

from cryptojs-aes-php.

fcarstensen avatar fcarstensen commented on July 20, 2024
"{\"ct\":\"dxZkCuq\\\/H7YqQs3E5lrs9ZZ2c9M3O5TCL1Bdtnm\\\/1mkOyEVUOtNk8ZVNPnqp1Ca966dRjOV9k4FT+z7TpEmeYFZv2I1L+q6fWXIIQQIV\\\/m4RiA2V8MF4VEq7y5QBpteN\",\"iv\":\"5c0757952300fe019196d02a830c0af2\",\"s\":\"0c4a63c4dd46ac5b\"}"

from cryptojs-aes-php.

fcarstensen avatar fcarstensen commented on July 20, 2024

console_log
may be this is the break after the "

from cryptojs-aes-php.

brainfoolong avatar brainfoolong commented on July 20, 2024

When i do just this, it works perfectly fine. So i guess you have some problems with your returned data from the ajax request, a line break or whatever.

var str = "{\"ct\":\"dxZkCuq\\\/H7YqQs3E5lrs9ZZ2c9M3O5TCL1Bdtnm\\\/1mkOyEVUOtNk8ZVNPnqp1Ca966dRjOV9k4FT+z7TpEmeYFZv2I1L+q6fWXIIQQIV\\\/m4RiA2V8MF4VEq7y5QBpteN\",\"iv\":\"5c0757952300fe019196d02a830c0af2\",\"s\":\"0c4a63c4dd46ac5b\"}";
console.log(JSON.parse(CryptoJS.AES.decrypt(str, "test123", {format: CryptoJSAesJson}).toString(CryptoJS.enc.Utf8)));

from cryptojs-aes-php.

brainfoolong avatar brainfoolong commented on July 20, 2024

Or you mabe doesn't have included cryptojs properly in your page, what does console.log(CryptoJS.AES.decrypt) give you?

from cryptojs-aes-php.

fcarstensen avatar fcarstensen commented on July 20, 2024

I have included the following js files:
aes.js
aes-json-format.js

The result of console.log(CryptoJS.AES.decrypt) is:
function (b,k,d){return("string"==typeof k?c:a).decrypt(e,b,k,d)}

from cryptojs-aes-php.

brainfoolong avatar brainfoolong commented on July 20, 2024

so, what you get when you do just this for a test case on your page?
#8 (comment)

i don't think that this library or cryptojs have a problem here.

from cryptojs-aes-php.

fcarstensen avatar fcarstensen commented on July 20, 2024

Then I get the correct data:

[Object, Object]
0: Object
toon_amount: "3"
toon_clan: "testclan"
__proto__: Object
1: Object
toon_amount: "1"
toon_clan: "neuclan"
__proto__: Object
length: 2
__proto__: Array[0]

I think it's the break after the "
but I have no idea to fix it...

from cryptojs-aes-php.

brainfoolong avatar brainfoolong commented on July 20, 2024

Have you already tried just running the example files without modification?
If that work than i cannot help you in this specific task because it obviously as a bug in your code.

from cryptojs-aes-php.

fcarstensen avatar fcarstensen commented on July 20, 2024

Ok, now I tried some things... on php side at the return value with json_encode / decode and escape characters... nothing. Then I played with some things on the client side at the js function. A few more console.logs and I found a solution.

$scope.topClanToonsCount = JSON.parse(CryptoJS.AES.decrypt(JSON.parse(data), "test123", {format: CryptoJSAesJson}).toString(CryptoJS.enc.Utf8));

JSON.parse(data) was the solution.

brainfoolong thanks for your help and for pushing me to dig deeper in the code

from cryptojs-aes-php.

brainfoolong avatar brainfoolong commented on July 20, 2024

This is weird, It should not be required to double JSON.parse the encrypted string.
Do you maybe have a second json_encode in your code outside of cryptoJsAesEncrypt

from cryptojs-aes-php.

fcarstensen avatar fcarstensen commented on July 20, 2024

Yes, I do:

json_encode($app->handleAction($_REQUEST)

This issue can be closed.

from cryptojs-aes-php.

brainfoolong avatar brainfoolong commented on July 20, 2024

Ok, that's the bug :) You don't have to do this, the json_encode inside the cryptoJsAesEncrypt already does it. Closed.

from cryptojs-aes-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.