erlang aes加密

aes128 + pkcs#7填充

encode(Bin) ->
    Len = erlang:size(Bin),
    Value = 16 - (Len rem 16),
    PadBin = binary:copy(<>, Value),
    EncodeB = crypto:block_encrypt(aes_cbc128, ?AES_KEY, ?AES_IV, <>),
    base64:encode(EncodeB).


decode(Bin) ->
    Bin1 = base64:decode(Bin),
    case erlang:size(Bin1) rem 16 of
        0 -> 
            Bin2 = crypto:block_decrypt(aes_cbc128, ?AES_KEY, ?AES_IV, Bin1),
            binary:part(Bin2, {0, byte_size(Bin2) - binary:last(Bin2)});
        _ -> 
            {error, 1102}
    end.

你可能感兴趣的:(erlang aes加密)