[erlang-questions] Block cipher with custom encoding

symk philippdumke@REDACTED
Wed Oct 17 16:56:20 CEST 2018


Hi,

I'm quite new to programming and Erlang as well. I'm trying to implement a block cipher algorithm. My main problem is the mapping of characters to a corresponding integer value. I do know about the representation of strings in lists but my professor asked for a custom encoding of each character. I approached the problem with a map where I assigned numbers to a map, which I'm reading from the filesystem.
I'm using this code to generate the map:

build_map(A) -> {ok,Map} = file:consult(A),
maps:from_list(Map).

The file I'm consulting looks like this:
{"A",0}.
{"B",1}.
{"C",2}.
{"D",3}.
{"E",4}.
...

This function is used for the generation of an integer for the block which a length of K.

toBlock(Input, K, Map, Result, Length) ->
	if K /= 0 ->
			Tail = tl(Input),NewK = K - 1, Char = string:slice(Input,0,1), TheChar = maps:get(Char,Map,""), Mult = fastExponentiation(Length,K),
			toBlock(Tail,NewK,Map,Result + (TheChar * Mult),Length);
		true -> [(Result + maps:get(string:slice(Input,0,1),Map,""))]
	end.

But this approach has certain drawbacks as I'm not able to include all characters I need for this map to work. I´d like to cover at least
all printable characters from the latin1 charset. A requirement for the map is, that after the encryption the cipher has to be converted
to text again. So all code-points have to be printable.
The conversion from String to Integer has to be like A * pow(Len,K) where A is the value of the character, Len the length of the map and K the position of the character inside of the block.

Can someone please give me a hint how I could improve my approach?

Thanks,
Philipp


More information about the erlang-questions mailing list