使用矩阵加密文本
Encryption of text using a matrix
[![скрин][1]][1][我需要让它看起来像屏幕上的样子,但我确定我做错了。
关键是有一个字符串 str,在本例中是(我非常爱我妈妈,也很喜欢汽车),在矩阵中输入这个字符串后,您需要将它分成 5 个字母一组。
结果应该如下(irlyo bvieg maynm dolto hveer cvaer).
public static void main(String[] args) {
String[][] matrixA;
matrixA = new String[2][15];
matrixA[0][0] = "i";
matrixA[0][1] = "l";
matrixA[0][2] = "o";
matrixA[0][3] = "v";
matrixA[0][4] = "e";
matrixA[0][5] = "m";
matrixA[0][6] = "y";
matrixA[0][7] = "m";
matrixA[0][8] = "o";
matrixA[0][9] = "t";
matrixA[0][10] = "h";
matrixA[0][11] = "e";
matrixA[0][12] = "r";
matrixA[0][13] = "v";
matrixA[0][14] = "e";
matrixA[1][0] = "r";
matrixA[1][1] = "y";
matrixA[1][2] = "b";
matrixA[1][3] = "i";
matrixA[1][4] = "g";
matrixA[1][5] = "a";
matrixA[1][6] = "n";
matrixA[1][7] = "d";
matrixA[1][8] = "l";
matrixA[1][9] = "o";
matrixA[1][10] = "v";
matrixA[1][11] = "e";
matrixA[1][12] = "c";
matrixA[1][13] = "a";
matrixA[1][14] = "r";
for (int i = 0; i < matrixA.length; i++) {
for (int j = 0; j < matrixA[i].length; j++) {
System.out.print(matrixA[i][j] + "\t");
}
System.out.println();
}
}
我看到底部有两个 for-loops 在矩阵上循环。为了实现您要求的算法,我们需要做的就是遍历矩阵但翻转维度。然后使用模数检查是否是时候添加 space 了。试试这个功能吧,有问题可以私信我
public static String getGroups(String[][] matrix, int groupSize) {
String output = "";
int matrixWidth = matrix.length;
int matrixHeight = matrix[0].length;
int k = 0;
for (int i = 0; i < matrixHeight; i++) {
for (int j = 0; j < matrixWidth; j++) {
k++;
output += matrix[j][i];
if(k % groupSize == 0) output += " ";
}
}
return output;
}
您需要做的就是调用 getGroups,传入 matrixA 并传入 groupSize 为 5。这就是它的样子:System.out.println(getGroups(matrixA, 5));
另外请注意,您在此行 double-quotation 标记 (") matrixA[0][10] = h";
这意味着完整的代码应该如下所示:
public static void main(String[] args) {
String[][] matrixA;
matrixA = new String[2][15];
matrixA[0][0] = "i";
matrixA[0][1] = "l";
matrixA[0][2] = "o";
matrixA[0][3] = "v";
matrixA[0][4] = "e";
matrixA[0][5] = "m";
matrixA[0][6] = "y";
matrixA[0][7] = "m";
matrixA[0][8] = "o";
matrixA[0][9] = "t";
matrixA[0][10] = "h";
matrixA[0][11] = "e";
matrixA[0][12] = "r";
matrixA[0][13] = "v";
matrixA[0][14] = "e";
matrixA[1][0] = "r";
matrixA[1][1] = "y";
matrixA[1][2] = "b";
matrixA[1][3] = "i";
matrixA[1][4] = "g";
matrixA[1][5] = "a";
matrixA[1][6] = "n";
matrixA[1][7] = "d";
matrixA[1][8] = "l";
matrixA[1][9] = "o";
matrixA[1][10] = "v";
matrixA[1][11] = "e";
matrixA[1][12] = "c";
matrixA[1][13] = "a";
matrixA[1][14] = "r";
for (int i = 0; i < matrixA.length; i++) {
for (int j = 0; j < matrixA[i].length; j++) {
System.out.print(matrixA[i][j] + "\t");
}
System.out.println();
}
System.out.println(getGroups(matrixA, 5));
}
public static String getGroups(String[][] matrix, int groupSize) {
String output = "";
int matrixWidth = matrix.length;
int matrixHeight = matrix[0].length;
int k = 0;
for (int i = 0; i < matrixHeight; i++) {
for (int j = 0; j < matrixWidth; j++) {
k++;
output += matrix[j][i];
if(k % groupSize == 0) output += " ";
}
}
return output;
}
@IvanKolisnik,要处理您指定的字符串:i love my mother very big and love car
并使用 getGroups()
提供的方法 @MorganS42 你需要做这样的事情:
创建矩阵(就像您展示的示例一样)
String strg = "i love my mother very big and love car";
strg = strg.replaceAll("\s+", ""); // Remove all whitespaces!
int columns = strg.length() / 2;
String[][] matrix = new String[2][columns];
int indexer = 0;
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = Character.toString(strg.charAt(indexer));
indexer++;
}
}
// Display Current Generated Matrix...
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j].isEmpty()) {
break;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
System.out.println();
显示加密行(根据您的规格):
/* Get the desired grouping from the generated
matrix and displsy it to console... */
String encryptedString = getGroups(matrix, 5);
System.out.println(encryptedString);
到控制台的输出应该是:
ilovemymotherve
rybigandlovecar
irlyo bvieg maynm dolto hveer cvaer
因此...如果要加密的字符串是:
I love my mother very big but I love my car bigger!
控制台输出为:
Ilovemymotherverybig
butIlovemycarbigger!
Ibluo tvIel moyvm eomty hcear rvbei rgygb eirg!
[![скрин][1]][1][
public static void main(String[] args) {
String[][] matrixA;
matrixA = new String[2][15];
matrixA[0][0] = "i";
matrixA[0][1] = "l";
matrixA[0][2] = "o";
matrixA[0][3] = "v";
matrixA[0][4] = "e";
matrixA[0][5] = "m";
matrixA[0][6] = "y";
matrixA[0][7] = "m";
matrixA[0][8] = "o";
matrixA[0][9] = "t";
matrixA[0][10] = "h";
matrixA[0][11] = "e";
matrixA[0][12] = "r";
matrixA[0][13] = "v";
matrixA[0][14] = "e";
matrixA[1][0] = "r";
matrixA[1][1] = "y";
matrixA[1][2] = "b";
matrixA[1][3] = "i";
matrixA[1][4] = "g";
matrixA[1][5] = "a";
matrixA[1][6] = "n";
matrixA[1][7] = "d";
matrixA[1][8] = "l";
matrixA[1][9] = "o";
matrixA[1][10] = "v";
matrixA[1][11] = "e";
matrixA[1][12] = "c";
matrixA[1][13] = "a";
matrixA[1][14] = "r";
for (int i = 0; i < matrixA.length; i++) {
for (int j = 0; j < matrixA[i].length; j++) {
System.out.print(matrixA[i][j] + "\t");
}
System.out.println();
}
}
我看到底部有两个 for-loops 在矩阵上循环。为了实现您要求的算法,我们需要做的就是遍历矩阵但翻转维度。然后使用模数检查是否是时候添加 space 了。试试这个功能吧,有问题可以私信我
public static String getGroups(String[][] matrix, int groupSize) {
String output = "";
int matrixWidth = matrix.length;
int matrixHeight = matrix[0].length;
int k = 0;
for (int i = 0; i < matrixHeight; i++) {
for (int j = 0; j < matrixWidth; j++) {
k++;
output += matrix[j][i];
if(k % groupSize == 0) output += " ";
}
}
return output;
}
您需要做的就是调用 getGroups,传入 matrixA 并传入 groupSize 为 5。这就是它的样子:System.out.println(getGroups(matrixA, 5));
另外请注意,您在此行 double-quotation 标记 (") matrixA[0][10] = h";
这意味着完整的代码应该如下所示:
public static void main(String[] args) {
String[][] matrixA;
matrixA = new String[2][15];
matrixA[0][0] = "i";
matrixA[0][1] = "l";
matrixA[0][2] = "o";
matrixA[0][3] = "v";
matrixA[0][4] = "e";
matrixA[0][5] = "m";
matrixA[0][6] = "y";
matrixA[0][7] = "m";
matrixA[0][8] = "o";
matrixA[0][9] = "t";
matrixA[0][10] = "h";
matrixA[0][11] = "e";
matrixA[0][12] = "r";
matrixA[0][13] = "v";
matrixA[0][14] = "e";
matrixA[1][0] = "r";
matrixA[1][1] = "y";
matrixA[1][2] = "b";
matrixA[1][3] = "i";
matrixA[1][4] = "g";
matrixA[1][5] = "a";
matrixA[1][6] = "n";
matrixA[1][7] = "d";
matrixA[1][8] = "l";
matrixA[1][9] = "o";
matrixA[1][10] = "v";
matrixA[1][11] = "e";
matrixA[1][12] = "c";
matrixA[1][13] = "a";
matrixA[1][14] = "r";
for (int i = 0; i < matrixA.length; i++) {
for (int j = 0; j < matrixA[i].length; j++) {
System.out.print(matrixA[i][j] + "\t");
}
System.out.println();
}
System.out.println(getGroups(matrixA, 5));
}
public static String getGroups(String[][] matrix, int groupSize) {
String output = "";
int matrixWidth = matrix.length;
int matrixHeight = matrix[0].length;
int k = 0;
for (int i = 0; i < matrixHeight; i++) {
for (int j = 0; j < matrixWidth; j++) {
k++;
output += matrix[j][i];
if(k % groupSize == 0) output += " ";
}
}
return output;
}
@IvanKolisnik,要处理您指定的字符串:i love my mother very big and love car
并使用 getGroups()
提供的方法 @MorganS42 你需要做这样的事情:
创建矩阵(就像您展示的示例一样)
String strg = "i love my mother very big and love car";
strg = strg.replaceAll("\s+", ""); // Remove all whitespaces!
int columns = strg.length() / 2;
String[][] matrix = new String[2][columns];
int indexer = 0;
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = Character.toString(strg.charAt(indexer));
indexer++;
}
}
// Display Current Generated Matrix...
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j].isEmpty()) {
break;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
System.out.println();
显示加密行(根据您的规格):
/* Get the desired grouping from the generated
matrix and displsy it to console... */
String encryptedString = getGroups(matrix, 5);
System.out.println(encryptedString);
到控制台的输出应该是:
ilovemymotherve
rybigandlovecar
irlyo bvieg maynm dolto hveer cvaer
因此...如果要加密的字符串是:
I love my mother very big but I love my car bigger!
控制台输出为:
Ilovemymotherverybig
butIlovemycarbigger!
Ibluo tvIel moyvm eomty hcear rvbei rgygb eirg!