chemist
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.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.
03juan
Is it possible that PHP is sending the unicode string for encrypting as-is “\xc3\xa9” and Elixir is interpreting it as “é” first?
Alternatively the unicode representation is being changed somehow depending on the language:
chemist
I need to duplicate the php encryption function in totality to have perfectly identical output as I am passing an encrypted string to an external software written in PHP.
chemist
Hey thats a possibility. Hmm so how do i do the same in elixir - passing it as is without modification. But part of me still thinks that even if elixir modifies the unicode map, its still the same character right
03juan
Those are the million dollar questions
There is a difference between the raw strings “\xc3\xa9” and “é”, one is interpreted as unicode and the like the charlist [?\, ?x, ?c, ?3, …etc].
derek-zhou
You can normalize the unicode string in one of 2 ways: NFC or NFD:
al2o3cr
Trying out the Unicode version in that PHP sandbox shows that PHP encodes
éas\u00e9in 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_safeoption toJason.encode!