views:

256

answers:

6

I'm looking for an algorithm that can do a one-to-one mapping of a string onto another string. I want an algorithm that given an alphabet I can perform a symmetric mapping function.

For example: Let's consider that I have the alphabet "A","B","C","D","E","F". I want something like F("ABC") = "CEA" and F("CEA") = "ABC" for every N letter permutation.

Surely, an algorithm like this exists. If you know of an algorithm, please post the name of it and I can research it. If I haven't been clear enough in my request, please let me know.

Thanks in advance.

Edit 1: I should clarify that I want enough entropy so that F("ABC") would equal "CEA" and F("CEA") = "ABC" but then I do NOT want F("ABD") to equal "CEF". Notice how two input letters stayed the same and the two corresponding output letters stayed the same?

So a Caesar Cipher/ROT13 or shuffling the array would not be sufficient. However, I don't need any "real" security. Just enough entropy for the output of the function to appear random. Weak encryption algorithms welcome.

+1  A: 

Take RC4, settle for some password, and you're done. (Not that this would be very safe.)

sbi
Encryption functions aren't necessarily surjective, so they don't fit the bill.
Jim Puls
True, but more to the point, encryption and decription aren't necessarily the same function either (as specified in the question). The symmetry refers to the keys not the functions.
Draemon
Fine, but I think RC4 would still do what JP wants. :)
sbi
Fair enough. Probably more complicated than he's looking for though?
Draemon
I don't think RC4 will work. For very small inputs (two bytes or less), the output contains very little entropy. In fact, it seems that if the input is a difference of one for small inputs, the output will be a difference of one. It seems to work well for large inputs, but I'm more concerned with small inputs three to six bytes. So I don't think it will work for me. Thanks though.
JP
Ah, that would indeed be a problem. Sorry, I had never used it for smaller strings, so I wouldn't know.
sbi
+1  A: 

Just create an array of objects that contain 2 fields -- a letter, and a random number. Sort the array. By the random numbers. This creates a mapping where the i-th letter of the alphabet now maps to the i-th letter in the array.

Larry Watanabe
This is not symmetric, I guess.
Ralph Rickenbach
It is if you store the array
DaClown
+1  A: 

If simple transposition or substitution isn't quite enough, it sounds like you want to advance to a polyalphabetic cipher. The Vigenère cipher is extremely easy to implement in code, but is still difficult to break without using a computer.

Bill the Lizard
I like this... however, when I change one letter in the input, only one letter in the output changes. I would like an algorithm that if on the input one letter changes, in the output most of the letters change.
JP
A: 

Take the set of all permutations of your alphabet, shuffle it, and map the first half of the set onto the second half. Bad for large alphabets, of course. :)

Nah, thought that over, I forgot about character repetitions. Maybe divide the input into chunks without repeating chars and apply my suggestion to all of those chunks.

A: 

I suggest the following.

Perform a dense coding of the input to positive integers - with an alphabet size of n and string length of m you can code the string into integers between zero and n^m - 1. In your example this would be the range [0,215]. Now perform a fixed involution on the encoded number and decode it again.

Daniel Brückner
A: 

I would restate your problem thus, and give you a strategy for that restatement:
"A substitution cypher where a change in input leads to a larger change in output".
The blocking of characters is irrelevant-- in the end, it's just mappings between numbers. I'll speak of letters here, but you can extend it to any block of n characters.

One of the easiest routes for this is a rotating substitution based on input. Since you already looked at the Vigenere cipher, it should be easy to understand. Instead of making the key be static, have it be dependent on the previous letter. That is, rotate through substitutions a different amount per each input.

The variable rotation satisfies the condition of making each small change push out to a larger change. Note that the algorithm will only push changes in one direction such that changes towards the end have smaller effects. You could run the algorithm both ways (front-to-back, then back-to-front) so that every letter of cleartext changed has the possibility of changing the entire string.

The internal rotation strategy elides the need for keys, while of course losing of most of the cryptographic security. It makes sense in context, though, as you are aiming for entropy rather than security.

Chad Wellington