# Byte swapping encryption algorithm security flaws?

Honesty commands me straight away to say that I'm not a professional, but I'm fascinated by data encryption. This algorithm won't be used in production code, so please don't flood me with "Don't design your own crypto", I know. I apologise in advance if the question is unfit for this site.

I was wondering how secure the following encryption algorithm would be. It would take:

• a 256-bytes long key, to be interpreted as a series of numbers.
• data to be encrypted, read in 256-bytes long blocks.

The key is such that no two values are repeated, and all values should be in the 0x00–0xFF range. This means there's 256! possible combinations, if I'm not wrong. Yet, some of those combinations are more entropic than others – which makes some of them less useful, right?

Now, a copy of the data chunk is made, and the key is iterated on. The first byte in the original data chunk is moved to the first value in the key; the second, to the second value in the key, and so on.

In pseudocode:

for index, destination in enumerate(key):
data_enc[destination] = data[index]

Not being a substitution cipher, it should be harder to guess which value is which. Yet, I am aware that the substitution is always the same throughout the data, so that surely counts as a weakness.

The main weakness is its simplicity, I suppose. What do you think?

• Please forgive those who are about to be brutal to you. They were human once. Commented Jul 11, 2014 at 18:01

some of those combinations are more entropic than others

Please don't torture words.

Entropy is a characteristic of a randomized process: it is a quantification of how many different outcomes you could have obtained. In your case, your "key" is a permutation of 256 values, so the process is the one which produces a new permutation. The process has entropy; no output permutation has any entropy. A permutation is just a value, it is what it is; it does not "contain" entropy. Correspondingly, it makes no sense to declare that any specific permutation is more or less "entropic" than any other; values are not "entropic" at all.

The main weakness is its simplicity, I suppose.

No, simplicity is its main strength, not its main weakness. Simplicity is good. The main weakness of your scheme is that it is weak.

What you describe is a permutation cipher, which is a sub-case of transposition ciphers. These are kind of algorithms which were popular back in the days when we could not do really better because it was before the invention of the computer. They are horribly leaky in many ways (e.g. they don't hide letter frequency, since all plaintext letters are still there). Edgar Allan Poe had famously boasted that he could break every kind of substitution or transposition cipher, and was not proven wrong before his death in 1849.

Yes, it’s really, really weak. Consider something you might want to encrypt: say, this HTTP request.

Accept-Encoding: gzip;q=0.9
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Host: example.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36

Random transposition on the first 256 bytes:

cT_
ox(tAHPM/ztz.au  knel-:amnie-eanL11AwlSCl  Mi.xti ;o 4c (-tpp.s.qpEetX3ewGo71x
P.pr ec H0/e1:iln9mKe/ie3plk8eTKg6fC1apo5 /  3 oe:T-
HrLvegy, S.g
u/1l5w7kne)W. c5
l0 0gn6tOo.x3m-o3i)6-rbo
oci1maioCodet6/h-dp5oc3enifa/cdnoT5lepinraen9UTAlits;ren:.cpin:=6

If you know what the plaintext is – and in many cases, it’s not hard to guess – there are only a few possibilities for the next block, and you’re guaranteed to get as much recognizable data as there are unique characters in the known plaintext. Given that, filling in the rest becomes easier, and it’s the case for every block you get with the same key.

That’s the obvious one, but there’s also the fact that you can do frequency analysis to find out exactly what’s being sent, or narrow down character sets for transferred passwords, just plain guess…

You can “fix” this in two ways:

• By using a different, unrelated key for each block. Congratulations; you have a weaker version of the one-time pad.

• By calculating a different key for each block. Congratulations; you have a weaker version of every cipher with key scheduling.