1.
Introduction
1.1.
What is encryption?
1.2.
What is decryption?
2.
What are Ciphers?
3.
Permutation Ciphers
3.1.
Implementing permutation ciphers
3.2.
What is a permutation key here?
3.3.
Example
3.4.
A special case
3.5.
Identification of permutation ciphers
4.
4.1.
What are the necessary conditions for a function to be a permutation function?
4.2.
How is a longer size of the permutation key more secure than that of a shorter size?
4.3.
How to identify permutation ciphers?
4.4.
What to do if the length of the plaintext is not a multiple of permutation key length?
5.
Conclusion
Last Updated: Mar 27, 2024

# Cipher-Permutate It, Keeping Plaintext

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

Hello Ninjas, in this modern world, it is very important to secure data and communications. So, to enforce security, we use various techniques called cryptography. Through this, we can encode the data so that only the intended person can decrypt it. Hence, to perform the encryption and decryption, we need Ciphers.

### What is encryption?

Encryption is the process of converting information into some secret code so that the true meaning of the message can be hidden. This is done to keep the information safe from unauthorized persons.

### What is decryption?

Decryption is the process of converting the secret code back to the original information so that the message can be read and understood. This can be done only by the intended recipient.

## What are Ciphers?

In cryptography, ciphers are algorithms for the encryption and decryption of data. It is a well-defined set of rules that we use for encrypting and decrypting messages. A cipher is a code that changes the original message by processing its plaintext characters into ciphertext. This ciphertext is a random string of characters that can’t be understood until decoded, ensuring security. There are majorly two traditional types of ciphers:

• Transpositional Ciphers: In this, we keep all the characters as it is but just change their positions according to a fixed, well-defined set of rules. Some common transpositional ciphers are Columnar Transpositional Ciphers and Permutation Ciphers.

• Substitutional Ciphers: We replace the original data sequences with other data sequences. Replace some plaintext characters or words with ciphertext characters or words.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Permutation Ciphers

They are a form of transpositional cipher. Permutation ciphers are based on keeping the plaintext characters unchanged but changing the positions and rearranging their order. In this form of cipher, the key is the permutation followed to encode the data.

### Implementing permutation ciphers

To implement permutation cipher on a message, we generate a permutation of size k first, and then we divide the plaintext message into segments of size k. The larger we take the value of k harder it becomes to decode so that we can get a more secure cipher. After which, the letters within each segment are rearranged according to the permutation key.

### What is a permutation key here?

A permutation key is a function(having a domain of 1 to k) followed to encrypt every message segment of size k. This function must be bijective. That is, it should be both injective and surjective. Injective meaning it should be a one-to-one function, i.e., mapping every distinct element in its domain to some distinct element. Surjective meaning it should be an onto function,i.e., the range should be equal to the codomain.

Since the function is bijective, it is invertible. We can find the inverse of the function, and this inverse function acts as the permutation key to decrypt the encrypted message.

### Example

Let us understand with an example:

For the example, we have to assume a plaintext message, a permutation key size k, and a permutation function. Let the plaintext message be CODINGNINJAS, let k = 4, and let the function ∏(x) be:

Every element of the first row maps a distinct element in the second row. Also, every element of the second row is, 1≤ x ≤ 4, which is equal to the function's domain, so the function is bijective.

So to encrypt the plaintext, first of all, we need to divide it into segments of size 4. After that, each segment is rearranged as per the permutation ∏(x)

So, the ciphertext after encryption is, IDOCINGNSAJN.

After which, the inverse permutation function ∏-1(x) is required at the receiver's end to decode the ciphertext.

Similarly, the ciphertext can be decrypted to plaintext using the table above.

### A special case

There can be cases when the length of the plain text is not perfectly divisible by the permutation key length k. In such cases, there is a need for some special method. We can solve this problem by padding the last block of characters(which have less than k characters). Padding should be chosen so the intended receiver can distinguish the message from the padding after deciphering. For example, if k=5, the message is CODINGNINJAS; when we divide it into groups of 5, we face a problem.

The size of all the segments is not equal to k. To solve this, we can pad it with special characters like to make the length a multiple of 5. CODINGNINJAS***. So that the segments can be formed,

after this, the above procedure can be applied.

### Identification of permutation ciphers

To identify if the message is encoded with permutation ciphers, we can use frequency analysis. Since we do not change any characters in permutation ciphers, the frequency of all the characters in the plaintext will be the same as that of all characters in the ciphertext. We can just find the frequencies of all the characters and compare them. If any of the characters’ frequencies are not the same in plaintext and ciphertext, it is not possible that it was encoded by permutation cipher.

### What are the necessary conditions for a function to be a permutation function?

For a function to be a permutation function, it must be a one-to-one function and an onto function. Simply means that the function should be bijective to be able to qualify for being a permutation function.

### How is a longer size of the permutation key more secure than that of a shorter size?

A longer-size permutation key is said to be more secure. The reason behind it is the number of permutations in which a segment can be arranged is equal to the factorial of the size of the permutation key(k!). So as we increase the value of k, the larger will be k factorial. Making it more and more challenging to crack.

### How to identify permutation ciphers?

If the frequencies of all the characters in the plaintext and ciphertext are equal, then it may be a permutation cipher.

### What to do if the length of the plaintext is not a multiple of permutation key length?

Suppose the length of the message is not divisible by the permutation key length(k). We use padding to increase the plaintext length and make it a multiple of k. Such characters are padded so the receiver can easily distinguish them from the message after decryption.

## Conclusion

In this article, we learned about permutation ciphers. We started our journey by defining ciphers and their uses and types. Then we thoroughly discussed permutation ciphers and their implementation and identification.

We hope this blog is helpful to you and increase your knowledge about permutation ciphers. Do upvote our blogs if you find them helpful and engaging! For more about Ciphers and cryptography, go to Keyword CipherSubstitution Cipher, and What is Cryptography.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Keep learning, and Keep Growing!

Happy Learning!

Live masterclass