# How should I assign a letter at random to another?

For my first ever project in Python, I wanted to code a cipher to encrypt simple messages ( no spaces or lower case to avoid that extra hassle for now ).

My idea was to either:

1. Choose a letter of the alphabet at random to assign to a letter of the message using lists ( without duplicates ) or

2. Shuffle the alphabet and assign a new letter using its position ( if a = 0 and b = 1 and so forth, the letter that took those position would be assigned to the letters of the message )

I managed to get a list of the letters of the alphabet using list.ascii_lowercase and a list of the letters in the message that needs to be encrypted but I'm not sure where to go from there. The second option is more appealing to me, but I don't know how to tackle the problem.

I hope I was clear enough, I'm very new to all of this. Thank you for your understanding.

• It's nice to see people getting enthusiastic with technologies, but nonetheless, this is a slightly bit off-topic here. What you're trying to achieve is known as "deterministic permutation of an alphabet", and probably based on a seed that's used as a key. – DannyNiu Apr 20 at 11:00
• So, if you are looking for algorithmic help I think this question could be on-topic for us (i.e. you don't insist on us helping you with concrete python code). If you are looking for implementation help (i.e. how to do simple thing X in python) then look on Stack Overflow first. As it is I think this question would be too broad for Stack Overflow. – SEJPM Apr 20 at 11:20
• I'd use the shuffling method yourself. The shuffled alphabet is then your key. After that you can simply iterate over the message and grow your ciphertext by replacing one character by the character mapped to the same position. The so called permutation from alphabet to shuffled alphabet can be done by indexing or by mapping (0 -> 'C' or 'A' -> 'C'). – Maarten Bodewes Apr 20 at 12:28

The programming part would be off-topic here, and I believe as an enthusiastic practitioner, you can figure it on your own.

Here's an idea for creating an alphabet map:

1. Initialization,

1.1. set n to the number of letters in the alphabet,

1.2. set Permute to an empty associative array (known as dict in python),

1.3. set Alphabet to the list of letters,

1. Seeding the permutation.

2.1. hash chr(n)+seed and obtain the result as an integer v,

2.2. calculate v%n, assign that to u.

2.3. remove Alphabet[u] from the list, assign it to x,

2.4. assign x to the key chr(n) of Permute,

2.5. Decrease n by 1, and if it's non-zero, repeat step 2.

1. Output Permute as the codebook.

Note that the chr in the illustrated steps takes the letter index instead of ASCII code, which you may need to do a bit of extra coding to take care of that.

• Permute = random.shuffle(Alphabet) ;) – Conrado Apr 20 at 11:23