15,994,059 members
See more:
I need to write a program to encrypt and decrypt a given plain text. The input will be a string without spaces. The program should read and store the string as a square matrix, with each element in the matrix being one character. Hence the number of characters in the input string can only be perfect squares. If they are not, the program should throw an error.

Encryption technique:

Input: abcdefghi

a b c

d e f

g h i

Exchanging rows will result in the following

g h i

d e f

a b c

The encrypted text then is ghidefabc

Therefore, For a matrix with 3 rows, swap rows 1 and 2. For a matrix with 4 rows, swap rows 1 and 4, swap rows 2 and 3. For a matrix with 5 rows, swap rows 1 and 5, swap rows 2 and 4 and so on. The program should print the input string which is plain text. Print the encrypted text. And then print the decrypted text. [Hint: Reverse of encryption is decryption and you may use just one method for both the processes. Such a cipher is called symmetric cipher]

Sample output:

Plain Text: welcometosanjose

Encrypted text: joseosanometwelc

Decrypted text: welcometosanjose
Posted
Richard MacCutchan 11-Mar-15 13:07pm
I guess it's more homework.
CPallini 11-Mar-15 13:15pm
Well, the requirements are pretty clear, have fun with the actual implementation.
Sergey Alexandrovich Kryukov 11-Mar-15 15:12pm
Why doing all that? There are real encryption algorithms...
—SA

## Solution 1

Your homework is set to test what you have learned, not what some random people on the Internet know.

If you can't do your homework yourself, then talk to your teacher.

Whilst we're happy to help with specific questions about code you have written, nobody here is going to do your homework for you.