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.

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

freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement