chemist

chemist

Applying :crypto and Jason.encode on Unicode characters

Spent quite a bit of time pondering this as I am unable to get the desired output similar to the PHP function that im trying to duplicate:

plaintext = ["<foo>","'bar'","\"baz\"","&blong&"] |> Jason.encode!
key = "secreted"
iv = "PlayerAZ"
:crypto.crypto_one_time(:des_cbc, key, iv, plaintext, [{:encrypt, :true},{:padding, :pkcs_padding}] ) |> Base.encode64

PHP:

<?php
        $plaintext = json_encode(array('<foo>',"'bar'",'"baz"','&blong&'));
        $key = 'secreted';
        $vector = 'PlayerAZ';
        $encryptedString = openssl_encrypt($plaintext, 'des-cbc', $key, OPENSSL_RAW_DATA, $vector);
        echo 'Encrypted: '.$encryptedString."\n";
        echo ''.$plaintext."\n";
        
        $encryptedStringBase64 = base64_encode($encryptedString);
        echo 'Encrypted + base 64:'.$encryptedStringBase64."\n";

        echo 'Decrypted:'.openssl_decrypt(base64_decode($encryptedStringBase64),'des-cbc',  $key, OPENSSL_RAW_DATA, $vector)."\n";

Sandbox: https://wtools.io/php-sandbox/b8Xw
In both cases: the output is the same for both: “rQnm2tysllww+bZLSwOa9UQUc0EEH3sNlu84k5o33VgbmbnHnIZx0g==”

The problem when i change the plaintext variable to include unicode character, “\xc3\xa9” as follows:
array(‘’,“‘bar’”,‘“baz”’,‘&blong&’, “\xc3\xa9” (PHP)
[“”,“‘bar’”,“"baz"”,“&blong&”,“\xc3\xa9”] (Elixir)

Both function outputs diverge and no longer agree.

May I know what am i doing wrong? Thanks.

First Post!

derek-zhou

derek-zhou

Just curious, why do you want to do that? The point of encryption is to decrypt, so shouldn’t you check the decrypted data instead of the encrypted data?

If you just want to checksum, there are much easy ways. Nut then the JSON format is not unique, there could be insignificant spaces. So 2 equivalent JSON may not match in checksum.

Most Liked

derek-zhou

derek-zhou

You can normalize the unicode string in one of 2 ways: NFC or NFD:

iex(3)> :unicode.characters_to_nfc_list("\u00e9")  
[233]
iex(4)> :unicode.characters_to_nfd_list("\u00e9")
[101, 769]
iex(5)> :unicode.characters_to_nfd_list("\xc3\xa9")
[101, 769]
iex(6)> :unicode.characters_to_nfc_list("\xc3\xa9")
[233]
al2o3cr

al2o3cr

Trying out the Unicode version in that PHP sandbox shows that PHP encodes é as \u00e9 in JSON.

Jason (and the JSON standard) permits Unicode characters in strings, so they pass the character through unescaped.

You can tell Jason to escape everything but ASCII characters by passing the escape: :unicode_safe option to Jason.encode!

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement