用于加密Java的矩阵元素交换 [英] Matrix element swapping for encryption Java

查看:101
本文介绍了用于加密Java的矩阵元素交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序来加密和解密给定的纯文本。输入将是一个没有空格的字符串。程序应该读取并将字符串存储为方形矩阵,矩阵中的每个元素都是一个字符。因此,输入字符串中的字符数只能是完美的正方形。如果不是,程序应该抛出错误。



加密技术:



输入:abcdefghi



abc



def



ghi



交换行将产生以下结果



ghi



def



abc



加密文本然后是ghidefabc



因此,对于3行的矩阵,交换行1和2.对于具有4行的矩阵,交换行1和4,交换行2和3.对于具有5行的矩阵,交换行1和5,交换行2和4,依此类推。程序应该打印纯文本输入字符串。打印加密文本。然后打印解密的文本。 [提示:加密的反向是解密,你可以只为这两个进程使用一种方法。这种密码称为对称密码]



样本输出:



纯文本:welcometosanjose


加密文字:joseosanometwelc



解密文字:welcometosanjose

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

推荐答案

你的作业将被设置为测试所学的东西,而不是互联网上一些随机的人所知道的。



如果你不能自己做功课,然后和老师谈谈。



虽然我们很乐意帮助解决有关代码的特定问题写了,这里没有人会为你做功课。
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.


这篇关于用于加密Java的矩阵元素交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆