Friday, May 12, 2017

It's a notes about Erlang. It's a work with "crypto" (DES CBC).

Hi, my readers!

I continue the experiments with Erlang. Today I want to introduce a code that works with library "crypto".
I'm trying to encrypt any strings. I'm using DES CBC algorithm.

Look at this!

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Module: des.erl
% Description: it's a program for a work with crypto module (tests)
% Author: Vasiliy V. Bodrov aka Bodro (bodrov@qtech.ru)
% Date: it's the 19-th of April, 2017 year
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The MIT License (MIT)
%
% Copyright (c) 2017 Vasiliy V. Bodrov aka Bodro
%
% Permission is hereby granted, free of charge, to any person obtaining a
% copy of this software and associated documentation files (the "Software"),
% Software is furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included
% in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
% OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
% IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
% CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
% OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
% THE USE OR OTHER DEALINGS IN THE SOFTWARE.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% See: http://directory.fsf.org/wiki/License:Expat
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(des).
-export([test/0, license/0]).
-vsn(1.0).

-define(BLOCK_SIZE, 8).
-define(INPUT_PROMPT, "    Input data: ").

test() ->
    io:format("*****************************************************~n", []),
    io:format("TEST #1:~n~n", []),
    test_impl(),
    io:format("*****************************************************~n", []).

test_impl() ->
    Key = <<16#01,16#23,16#45,16#67,16#89,16#ab,16#cd,16#ef>>,
    IVec = <<16#12,16#34,16#56,16#78,16#90,16#ab,16#cd,16#ef>>,
    Str = io:get_line(?INPUT_PROMPT),
    P = normalize_str(Str),
    C = crypto:block_encrypt(des_cbc, Key, IVec, P),
    R = crypto:block_decrypt(des_cbc, Key, IVec, C),
    print({P, C, R}).

print({P, C, R}) ->
    io:format("    --------------------------------------------~n", []),
    io:format("    Source: ~p~n", [P]),
    io:format("    --------------------------------------------~n", []),
    io:format("    Source as string: ~p~n", [denormalize_str(P)]),
    io:format("    --------------------------------------------~n", []),
    io:format("    Encrypted: ~p~n", [C]),
    io:format("    --------------------------------------------~n", []),
    io:format("    Decrypted: ~p~n", [R]),
    io:format("    --------------------------------------------~n", []),
    io:format("    Decrypted as string: ~p~n", [denormalize_str(erlang:binary_to_list(R))]).

list_size(L) when is_list(L) ->
    list_size_impl(L, 0).

list_size_impl([], S) ->
    S;
list_size_impl([_|T], S) ->
    list_size_impl(T, S + 1).

normalize_str([]) ->
    [];
normalize_str(L) when is_list(L)->
    C = list_size(L) rem ?BLOCK_SIZE,
    normalizator(L, ?BLOCK_SIZE - C).
    
normalizator(L, 0) ->
    L;
normalizator(L, N) ->
    normalizator(L ++ [0], N - 1).

denormalize_str([]) ->
    [];
denormalize_str(L) when is_list(L) ->
    denormalizator(L, []).

denormalizator([], X) ->
    X;
denormalizator([0|_], X) ->
    X;
denormalizator([10|_], X) ->
    X;
denormalizator([H|T], X) ->
    denormalizator(T, X ++ [H]).

license() ->
    io:format(" The MIT License (MIT)~n"),
    io:format("~n"),
    io:format(" Copyright (c) 2017 Vasiliy V. Bodrov aka Bodro~n"),
    io:format("~n"),
    io:format(" Permission is hereby granted, free of charge, to any person obtaining a~n"),
    io:format(" copy of this software and associated documentation files (the \"Software\"),~n"),
    io:format(" Software is furnished to do so, subject to the following conditions:~n"),
    io:format("~n"),
    io:format(" The above copyright notice and this permission notice shall be included~n"),
    io:format(" in all copies or substantial portions of the Software.~n"),
    io:format("~n"),
    io:format(" THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS~n"),
    io:format(" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF~n"),
    io:format(" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.~n"),
    io:format(" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY~n"),
    io:format(" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT~n"),
    io:format(" OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR~n"),
    io:format(" THE USE OR OTHER DEALINGS IN THE SOFTWARE.").
    
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End of file
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



It's a result:
bodro@vbodrov-pc:~/workspace/develop/test/test56$ erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> des:license().
 The MIT License (MIT)

 Copyright (c) 2017 Vasiliy V. Bodrov aka Bodro

 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 THE USE OR OTHER DEALINGS IN THE SOFTWARE.ok
2> des:test().
*****************************************************
TEST #1:

    Input data: Hello, world!
    --------------------------------------------
    Source: [72,101,108,108,111,44,32,119,111,114,108,100,33,10,0,0]
    --------------------------------------------
    Source as string: "Hello, world!"
    --------------------------------------------
    Encrypted: <<202,49,22,168,11,91,77,221,161,37,37,51,151,179,205,184>>
    --------------------------------------------
    Decrypted: <<72,101,108,108,111,44,32,119,111,114,108,100,33,10,0,0>>
    --------------------------------------------
    Decrypted as string: "Hello, world!"
*****************************************************
ok
3> q().
ok
4> bodro@vbodrov-pc:~/workspace/develop/test/test56$

See you later! Bye! :)

Best regards,
Vasiliy V. Bodrov aka Bodro,
May 12, 2017.

No comments:

Post a Comment

How to reverse singly linked list in C (trivial way)

There is a beautiful morning. It's snowing. And I decided to write one more article. Today it will be just simple article where I would...