Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Simple Output Encrypted String #25

Closed
FranGhe opened this issue Sep 22, 2020 · 3 comments
Closed

Simple Output Encrypted String #25

FranGhe opened this issue Sep 22, 2020 · 3 comments

Comments

@FranGhe
Copy link

FranGhe commented Sep 22, 2020

Thanks for your code... it's most appreciate. I have a question... is it possible to have in output a simpler encryption string?

This is the code that I following:

        <?php
            $valTxt="MyText";
            $pswd="MyPassword";
            echo "<p>Text to Crypt: ".$valTxt." </p>";
            echo "<p>Password: ".$pswd." </p>";
            $encrypted = cryptoJsAesEncrypt($pswd, $valTxt);
            echo "<p> Encrypted PHP: ".$encrypted." </p>";
            $decrypted = cryptoJsAesDecrypt($pswd, $encrypted);
            echo "<p> Decrypted PHP: ".$decrypted." </p>";
        ?>
        
        <script>
            var valEnc='<?php echo $encrypted;?>';
            var pswdDec='<?php echo $pswd;?>';
        </script>
        
        <p>Testo Decriptato JS: -------- <span id="result"></span></p><br><br>

        <script>
            var decrypted = JSON.parse(CryptoJS.AES.decrypt(valEnc, pswdDec, {format: CryptoJSAesJson}).toString(CryptoJS.enc.Utf8));
            document.getElementById("result").innerHTML = decrypted;
        </script>

As you can see in output I have a string like this:
{"ct":"UbDpkBRT1R5Tbt+hp0nX6w==","iv":"6c113edfbc0d7d917294ecdfc4d61d6f","s":"a872cd319b0d2a52"}

I would like to have an encrypted string more simple because I would like that an user can write it.
I thought that the user digits "ct" (better if the string is 16 char between A-Za-z0-9) and I pass to the function "iv" and "s".

An example from your is most appreciate because I'm a beginner... thanks

@brainfoolong
Copy link
Owner

Hi, it is impossible to reduce the size of the encrypted string. You can encode it in base64 with btoa() to only have chars from A-Z0-9=.

But it increases the size of the string by around 1/3 of it's original encrypted length. In your case this will be eyJjdCI6IlViRHBrQlJUMVI1VGJ0K2hwMG5YNnc9PSIsIml2IjoiNmMxMTNlZGZiYzBkN2Q5MTcyOTRlY2RmYzRkNjFkNmYiLCJzIjoiYTg3MmNkMzE5YjBkMmE1MiJ9.

Nobody can write this by hand :) It's the nature of encryption that the encrypted text is more or less longer than the original.

@FranGhe
Copy link
Author

FranGhe commented Sep 22, 2020

Thank you for your support.
There is a misunderstanding ... I wish that:

  • the encrypted string is static and not random;
  • the password and vector will be fixed and passed to the function by the system;
  • the encrypted string has has chars from A-Za-z0-9;

Sorry for confusion

Ps. I have also made a post on Stackoverflow:
https://stackoverflow.com/questions/64016241/cryptojs-encrypt-decrypt-by-php-and-javascript-simple-output-encrypted-strin

@brainfoolong
Copy link
Owner

Ok, i think you already got a good answer in stackoverflow. This library is not designed for what you want to accomplish.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants