Erlang:aes_cbc128加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-define(AES_BLOCK_SIZE, 16).
-define(AES_TYPE, aes_cbc128).
-define(AES_VEC, <<16#00, 16#01, 16#02, 16#03, 16#04, 16#05, 16#06, 16#07, 16#08, 16#09, 16#0A, 16#0B, 16#0C, 16#0D, 16#0E, 16#0F>>).

%% @doc
test() ->
{ok, Socket} = gen_udp:open(10000, [binary]),
EncryptType = 0,
GameId = 10001,
GameKey = '233311dsdaadadssx',
OnlineMax = 5000,
Online = 1000,
PlatformId = 1,
TxServerId = 1,
EncryptKeyC = "adsdcsadadssssqsdcccsssxxzzzcsdqqqeqeqsssssssssssssxe",
EncryptKey = list_to_binary(lists:sublist(EncryptKeyC, 16)),
JsonData = switch_json_data(EncryptType, GameId, GameKey, OnlineMax, Online, PlatformId, TxServerId, EncryptKey),
ok = gen_udp:send(Socket, "127.0.0.1", 10002, JsonData),
%%%=================================================================== DEBUG部分
%% Value = receive
%% {udp, _Socket, _, _, Bin} ->
%% BinList = binary_to_list(Bin),
%% BinListN = lists:sublist(BinList, 1, length(BinList) - 1),
%% {struct, Param} = gs_json:decode(list_to_binary(BinListN)),
%% {_, AsyncId} = lists:keyfind(<<"asyncid">>, 1, Param),
%% {_, ResultCode} = lists:keyfind(<<"result_code">>, 1, Param),
%% {_, Desc} = lists:keyfind(<<"desc">>, 1, Param),
%% ?DEBUG("==============>AsyncId:~w, ResultCode:~w, Desc:~s~n", [AsyncId, ResultCode, Desc]),
%% ok
%% after 2000 ->
%% error
%% end,
%% ?DEBUG("==============>Value:~w~n", [Value]),
%%%===================================================================
ok.

%% @doc
request_header_data(EncryptType, GameId) ->
{
'asyncid', 1572537600,
'client_time', Now1572537600
'encrypt_type', EncryptType,
'gameid', GameId
}.

%% @doc
request_body_data(GameKey, OnlineMax, Online, PlatformId, TxServerId) ->
Now = gs_time:timestamp(),
{
'client_time', Now,
'gamekey', GameKey,
'msg_type', 'dir_status_report',
'userdata', {
'capacity', OnlineMax,
'online', Online,
'platform_id', PlatformId,
'server_id', TxServerId
}
}.

%% @doc
switch_json_data(EncryptType, GameId, GameKey, OnlineMax, Online, PlatformId, TxServerId, _EncryptKey) when EncryptType == 0 ->
Header = gs_json:to_json(request_header_data(EncryptType, GameId)),
Body = gs_json:to_json(request_body_data(GameKey, OnlineMax, Online, PlatformId, TxServerId)),
<<Header/binary, Body/binary>>;
switch_json_data(EncryptType, GameId, GameKey, OnlineMax, Online, PlatformId, TxServerId, EncryptKey) ->
Header = gs_json:to_json(request_header_data(EncryptType, GameId)),
Body = gs_json:to_json(request_body_data(GameKey, OnlineMax, Online, PlatformId, TxServerId)),
AesData = encrypt_aes(Body, EncryptKey),
<<Header/binary, AesData/binary>>.

%% @doc
encrypt_aes(Plain, Key) ->
crypto:block_encrypt(?AES_TYPE, Key, ?AES_VEC, pkcs5_padding(Plain)).

%% @doc
pkcs5_padding(PlainText) when is_binary(PlainText) ->
Rem = size(PlainText) rem ?AES_BLOCK_SIZE,
Padding = lists:duplicate(?AES_BLOCK_SIZE - Rem, 0),
Binary = list_to_binary(Padding),
<<PlainText/binary, Binary/binary>>;
pkcs5_padding(PlainText) when is_list(PlainText) ->
Rem = length(PlainText) rem ?AES_BLOCK_SIZE,
Padding = lists:duplicate(?AES_BLOCK_SIZE - Rem, 0),
PlainText ++ Padding.