<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="342272" data-post-id="342272">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zookzook" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zookzook/120/15276_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zookzook
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>With the help of <a href="https://github.com/rusterlium/rustler" rel="noopener nofollow ugc">rustler</a> you can use Rust to decrypt the AES key. I used the node.js example and was able to decode the encrypted AES key using Rust/Rustler:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">use base64::prelude::*;
use rustler::{Env, NifResult};
use rsa::{RsaPrivateKey, Oaep, sha2::Sha256};
use rsa::pkcs8::DecodePrivateKey;
use rustler::types::binary::{Binary, OwnedBinary};

#[rustler::nif(schedule = "DirtyCpu")]
fn decrypt&lt;'a&gt;(env: Env&lt;'a&gt;, private_file_path: String, cipher_text: String) -&gt; NifResult&lt;Binary&lt;'a&gt;&gt; {

    let padding = Oaep::new::&lt;Sha256&gt;();
    let content = std::fs::read_to_string(&amp;private_file_path).unwrap();
    let private_key = RsaPrivateKey::from_pkcs8_pem(&amp;content).unwrap();
    let enc_data = BASE64_STANDARD.decode(cipher_text).unwrap();
    let dec_data = private_key.decrypt(padding, &amp;enc_data).unwrap();
    let mut result: OwnedBinary = OwnedBinary::new(dec_data.len()).unwrap();
    result.as_mut_slice().copy_from_slice(&amp;dec_data);
    Ok(Binary::from_owned(result, env))
}

rustler::init!("Elixir.RCrypto");
</code></pre>
<p>Here is the output:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex [09:40 :: 2] &gt; RCrypto.decrypt("./private_unencrypted.pem", "hiHNE0AFZI6pn/Sbo0yPmbm37NQxuCc1rDxotfkeOHjjCO9BEVMtLQKfOE6w2/VQwISqJpAE0D9FlScvEdMgNjtbo5rNRutHFVF5xnvLsXyV228dxt5m8h+6k1LApk5TLOj97+fi4iiYq5Tr3n1E+4jpY0TTl30s/k0rhciOIy7HwFpaRWVSiOZFqx+PTYrJYcRot/DoArd4DLAdlbeoHcyoB4R0LV4DJRUmKwrcP/7J3njC1PpetGjgGiy5g/e/dKoxmcjjyhSwS7QeoCxBAF4ErHWHFfNOeNyCdwLmOtlLIa55nwGQ0Jyn0xp4WQj2xJqoHvCTcqbAxRtCJ5b5zA==")
"&lt;some-key-data&gt;"
iex [09:40 :: 3] &gt;
</code></pre>
<p>Of course, the code is just a quick demo.  Loading the key for each call should not be used at all.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="342272" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/need-help-with-basic-crypto-in-erlang-elixir-decryption-error-failed-to-decrypt/66623/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-342272" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="342272"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="342273" data-post-id="342273">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="CharlesO" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/CharlesO/120/34571_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  CharlesO
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is very helpful.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="342273" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/need-help-with-basic-crypto-in-erlang-elixir-decryption-error-failed-to-decrypt/66623/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-342273" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="342273"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="342314" data-post-id="342314">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="CharlesO" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/CharlesO/120/34571_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  CharlesO
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I eventually used <code>NodeJS :  {:nodejs, "~&gt; 2.0"}</code> to call the <code>encryption.j</code>s file provided in Meta’s examples.</p>
<p>works like a charm!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="342314" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/need-help-with-basic-crypto-in-erlang-elixir-decryption-error-failed-to-decrypt/66623/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-342314" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="342314"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="344022" data-post-id="344022">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zookzook" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zookzook/120/15276_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zookzook
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>As an exercise, I have written a package that provides the missing function for decrypting the AES key using Rust. At the same time, I looked at the solution with Node-Server and compared it. I also wrote a small load testing that performs the decryption of the request and the encryption of the response.</p>
<p>The <code>:observer</code> delivers the expected results. While the dirty CPU rises to 100% in the Rust implementation, the work in the node implementation is performed by the started node server. With the Node implementation, data must be serialised via port. You can clearly see the increased IO usage, although the Erlang schedulers are hardly loaded. The Rust implementation is faster with a factor of 1.37. This can be explained by the fact that the node implementation calls the OpenSSL functions, which means that encryption/decryption is performed almost as quickly for large data. Only the serialisation has an impact.</p>
<p></p><div class="lightbox-wrapper"><a class="lightbox" href="https://forum.elixirforum.com/uploads/default/original/3X/f/a/fa4db70762b85adb762448d00cdd728d2d8a29a9.jpeg" data-download-href="https://forum.elixirforum.com/uploads/default/fa4db70762b85adb762448d00cdd728d2d8a29a9" title="image" rel="nofollow"><img src="https://forum.elixirforum.com/uploads/default/optimized/3X/f/a/fa4db70762b85adb762448d00cdd728d2d8a29a9_2_690x454.jpeg" alt="image" data-base62-sha1="zIhS0EHNE7LxkE5Qt9wQmu5IOWB" width="690" height="454" srcset="https://forum.elixirforum.com/uploads/default/optimized/3X/f/a/fa4db70762b85adb762448d00cdd728d2d8a29a9_2_690x454.jpeg, https://forum.elixirforum.com/uploads/default/optimized/3X/f/a/fa4db70762b85adb762448d00cdd728d2d8a29a9_2_1035x681.jpeg 1.5x, https://forum.elixirforum.com/uploads/default/optimized/3X/f/a/fa4db70762b85adb762448d00cdd728d2d8a29a9_2_1380x908.jpeg 2x" data-dominant-color="E6E7E7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">2218×1462 107 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><p></p>
<p>The Rust implementation eliminates the need to manage additional node servers and reduces IO usage. You also gain a small speed advantage. The CPU usage is almost the same. The node server consume about 100%.</p>
<ul>
<li><a href="https://hexdocs.pm/whatsapp_flow_crypto/readme.html" rel="noopener nofollow ugc">hexdocs.pm</a></li>
<li><a href="https://github.com/zookzook/whatsapp_flow_crypto" rel="noopener nofollow ugc">GitHub</a></li>
</ul> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="344022" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/need-help-with-basic-crypto-in-erlang-elixir-decryption-error-failed-to-decrypt/66623/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-344022" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="344022"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #24"></div>
  </section>
</div>
    <div class="postbit" id="350505" data-post-id="350505">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="CharlesO" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/CharlesO/120/34571_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  CharlesO
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>hi, just to confirm that your library works excellently, I’ve moved over from the NodeJS version to your rust version.</p>
<p>Thanks for this lib!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="350505" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/need-help-with-basic-crypto-in-erlang-elixir-decryption-error-failed-to-decrypt/66623/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-350505" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="350505"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>