Current section

Files

Jump to
diint_utilites_common_app src security diint_triple_des.erl
Raw

src/security/diint_triple_des.erl

%%%-------------------------------------------------------------------
%%% @author cheese
%%% @copyright (C) 2015, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 25. Nov 2015 10:26
%%%-------------------------------------------------------------------
-module(diint_triple_des).
-author("cheese").
%% API
-export([encript/2, decript/2]).
-include_lib("kernel/include/logger.hrl").
encript(Key, Bytes) ->
MD5 = erlang:md5(Key),
?LOG_INFO("MD5: ~s, ~w~n", [MD5, binary_to_list(MD5)]),
crypto:des_ecb_encrypt(hexstr2bin(binary_to_list(MD5)), <<1,2,3,4,5,6,7,8>>).
decript(Key, Bytes) ->
crypto:des_ecb_decrypt(erlang:md5(Key), Bytes).
% hexstr2bin
hexstr2bin(S) ->
list_to_binary(hexstr2list(S)).
hexstr2list([X,Y|T]) ->
[mkint(X)*16 + mkint(Y) | hexstr2list(T)];
hexstr2list([]) ->
[].
mkint(C) when $0 =< C, C =< $9 ->
C - $0;
mkint(C) when $A =< C, C =< $F ->
C - $A + 10;
mkint(C) when $a =< C, C =< $f ->
C - $a + 10.