[erlang-questions] 3des decryption in erlang

Sean Cribbs seancribbs@REDACTED
Mon Aug 10 15:54:30 CEST 2015


Xiaobin,

Here's what I came up with:

    crypto:start(),
    %% To decrypt the text, note Key and IV must be defined in this scope
    Unencoded = base64:decode(Value),
    Cleartext = crypto:block_decrypt(des3_cbc, Key, IV, Unencoded),
    %% To unpad the text, see https://github.com/camshaft/pkcs7.erl
    pkcs7:unpad(Cleartext)

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
http://erlang.org/doc/man/app.html) of the application that contains this
code and it would be started automatically when your release is booted.

On Mon, Aug 10, 2015 at 7:59 AM, Xiaobin Xu <xuxb1979@REDACTED> wrote:

> Hi, all,
>
>    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:
>
> public  function decrypt($value) {
> $td = mcrypt_module_open ( MCRYPT_3DES, '', MCRYPT_MODE_ECB, '' );
> mcrypt_generic_init ( $td, $this->key,$this->iv );
> $ret = trim ( mdecrypt_generic ( $td, base64_decode ( $value ) ) );
> $ret = $this->UnPaddingPKCS7 ( $ret );
> mcrypt_generic_deinit ( $td );
> mcrypt_module_close ( $td );
> return $ret;
> }
>
>
> private  function UnPaddingPKCS7($data) {
> $padlen = ord (substr($data, (strlen( $data )-1), 1 ) );
> if ($padlen > 8 )
> return $data;
>
> for($i = -1*($padlen-strlen($data)); $i < strlen ( $data ); $i ++) {
> if (ord ( substr ( $data, $i, 1 ) ) != $padlen)return false;
> }
>
> return substr ( $data, 0, -1*($padlen-strlen ( $data ) ) );
> }
>
>    I googled and read crypto module document for a couple hours, and got
> no idea how to translate these two functions into erlang.
>
>    Any ideas?
>
>
>    Thanks,
>     Xiaobin
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150810/3dd1ce9f/attachment.htm>


More information about the erlang-questions mailing list