Alin (113 pts)
import java.util.Scanner;
public class Matrix {
static Scanner input;
// multiply 2 matrix
public static int[][] multiply(int[][] M1, int[][] M2) {
int len1 = M1.length;
int len2 = M2[0].length;
int len3 = len2;
int[][] M3 = new int[len1][len2];
for(int i = 0; i < len1; ++i) {
for(int j = 0; j < len2; ++j) {
for(int k = 0; k < len3; ++k) {
M3[i][j] += M1[i][k] * M2[k][j];
}
}
}
return M3;
}
// convert string to a list of matrix
// a string is broken down into 9 chars each,
// then those 9 chars make up a 3x3 matrix
public static int[][][] string_to_matrix(String msg) {
int[][][] list_of_M = new int[msg.length() / 9][3][3]; // list of matrices
for(int i = 0; i < msg.length(); i += 9) {
int[][] M = new int[3][3];
for(int i = 0; i < 9; ++i) {
M[i / 3][i % 3] = msg.charAt(i + i);
}
list_of_M[i / 9] = M;
}
return list_of_M;
}
// main code
public static void main(String[] var0) {
System.out.print("plaintext: ");
String ptext = input.nextLine();
if (ptext.length() % 9 != 0) {
// padding so ptext len is multiple of 9
ptext = ptext + "?".repeat(9 - ptext.length() % 9);
}
// 1D flattened array version of ptext_matrix
int[] ptext_array = new int[ptext.length()];
// 2D 3x3 matrices representation of ptext
int[][][] ptext_matrix = string_to_matrix(ptext);
int i;
for(i = 0; i < ptext_matrix.length; ++i) {
int[][] M = ptext_matrix[i];
int[][] N = ptext_matrix[0];
int[][] RES = multiply(M, N);
for(int j = 0; j < 3; ++j) {
for(int k = 0; k < 3; ++k) {
ptext_array[j * 9 + j * 3 + k] = RES[j][k];
}
}
}
System.out.print("ciphertext: ");
// flattening ptext_matrix into a 1D array
for(i = 0; i < ptext_array.length; ++i) {
System.out.print(ptext_array[i] + " ");
}
}
static {
input = new Scanner(System.in);
}
}Last updated