<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Thank you for your rapid answer, Sean.</div><div class=""><br class=""></div><div class="">I’ve tried block_decrypt yesterday, but I couldn’t find the counterpart of ECB mode decryption.</div><div class=""><br class=""></div><div class="">I tried des_ede3, des3_cbc, de3_cbf. For des_ede3/des3_cbc, the output seams the same, and only the first 8 bytes output result are correct.</div><div class=""><br class=""></div><div class="">For des3_cbf, the output are completely incorrect.</div><div class=""><br class=""></div><div class="">So I’m still confused how to do DES3 ECB decryption with block_decrypt? </div><div apple-content-edited="true" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""></div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Aug 10, 2015, at 21:54, Sean Cribbs <<a href="mailto:seancribbs@gmail.com" class="">seancribbs@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Xiaobin,</div><div class=""><br class=""></div>Here's what I came up with:<div class=""><br class=""></div><div class=""> crypto:start(),</div><div class=""><div class=""> %% To decrypt the text, note Key and IV must be defined in this scope</div><div class=""> Unencoded = base64:decode(Value),</div><div class=""> Cleartext = crypto:block_decrypt(des3_cbc, Key, IV, Unencoded),</div><div class=""> %% To unpad the text, see <a href="https://github.com/camshaft/pkcs7.erl" class="">https://github.com/camshaft/pkcs7.erl</a></div><div class=""> pkcs7:unpad(Cleartext)</div></div><div class=""><br class=""></div><div class="">The main thing to note is the difference in how you use the crypto module. In Erlang, you don't need to initialize, decrypt, and cleanup in separate steps. You do however, need to make sure the crypto application is started before you try this. Generally, you would make crypto a dependency (see <a href="http://erlang.org/doc/man/app.html" class="">http://erlang.org/doc/man/app.html</a>) of the application that contains this code and it would be started automatically when your release is booted.</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Aug 10, 2015 at 7:59 AM, Xiaobin Xu <span dir="ltr" class=""><<a href="mailto:xuxb1979@gmail.com" target="_blank" class="">xuxb1979@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">Hi, all,<div class=""><br class=""></div><div class=""> For some reason i have to decrypt a message that is encrypted using 3DES algorithm, and I've PHP function example how to decrypt the message:</div><div class=""><br class=""></div><div class=""><div class=""><span style="white-space:pre-wrap" class=""> </span>public function decrypt($value) {</div><div class=""><span style="white-space:pre-wrap" class=""> </span>$td = mcrypt_module_open ( MCRYPT_3DES, '', MCRYPT_MODE_ECB, '' );</div><div class=""><span style="white-space:pre-wrap" class=""> </span>mcrypt_generic_init ( $td, $this->key,$this->iv );</div><div class=""><span style="white-space:pre-wrap" class=""> </span>$ret = trim ( mdecrypt_generic ( $td, base64_decode ( $value ) ) );</div><div class=""><span style="white-space:pre-wrap" class=""> </span>$ret = $this->UnPaddingPKCS7 ( $ret );</div><div class=""><span style="white-space:pre-wrap" class=""> </span>mcrypt_generic_deinit ( $td );</div><div class=""><span style="white-space:pre-wrap" class=""> </span>mcrypt_module_close ( $td );</div><div class=""><span style="white-space:pre-wrap" class=""> </span>return $ret;</div><div class=""><span style="white-space:pre-wrap" class=""> </span>}</div><div class=""><span style="white-space:pre-wrap" class=""> </span> </div><div class=""><span style="white-space:pre-wrap" class=""> </span> </div><div class=""><span style="white-space:pre-wrap" class=""> </span>private function UnPaddingPKCS7($data) {</div><div class=""><span style="white-space:pre-wrap" class=""> </span>$padlen = ord (substr($data, (strlen( $data )-1), 1 ) );</div><div class=""><span style="white-space:pre-wrap" class=""> </span>if ($padlen > 8 )</div><div class=""><span style="white-space:pre-wrap" class=""> </span>return $data;</div><div class=""><span style="white-space:pre-wrap" class=""> </span> </div><div class=""><span style="white-space:pre-wrap" class=""> </span>for($i = -1*($padlen-strlen($data)); $i < strlen ( $data ); $i ++) {</div><div class=""><span style="white-space:pre-wrap" class=""> </span>if (ord ( substr ( $data, $i, 1 ) ) != $padlen)return false;</div><div class=""><span style="white-space:pre-wrap" class=""> </span>}</div><div class=""><span style="white-space:pre-wrap" class=""> </span> </div><div class=""><span style="white-space:pre-wrap" class=""> </span>return substr ( $data, 0, -1*($padlen-strlen ( $data ) ) );</div><div class=""><span style="white-space:pre-wrap" class=""> </span>}</div></div><div class=""><br class=""></div><div class=""> I googled and read crypto module document for a couple hours, and got no idea how to translate these two functions into erlang.</div><div class=""><br class=""></div><div class=""> Any ideas?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""> Thanks, </div><span class="HOEnZb"><font color="#888888" class=""><div class=""> Xiaobin</div><div class=""><br class=""></div></font></span></div>
<br class="">_______________________________________________<br class="">
erlang-questions mailing list<br class="">
<a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a><br class="">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank" class="">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>