mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
fix bugs in AES.java(#765)
This commit is contained in:
@ -52,7 +52,7 @@ public class CircleLinkedList<E> {
|
||||
before = before.next;
|
||||
}
|
||||
E saved = iterator.value;
|
||||
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
|
||||
// assigning the next reference to the the element following the element we want to remove... the last element will be assigned to the head.
|
||||
before.next = iterator.next;
|
||||
// scrubbing
|
||||
iterator.next = null;
|
||||
|
@ -1,269 +0,0 @@
|
||||
package DataStructures.Matrix;
|
||||
|
||||
/**
|
||||
* Matrix data-type.
|
||||
*
|
||||
* @author Kyler Smith, 2017
|
||||
*/
|
||||
|
||||
|
||||
public class Matrix {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[][] data1 = new int[0][0];
|
||||
int[][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
int[][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
|
||||
|
||||
Matrix m1 = new Matrix(data1);
|
||||
Matrix m2 = new Matrix(data2);
|
||||
Matrix m3 = new Matrix(data3);
|
||||
|
||||
System.out.println("m1 --> Rows: " + m1.getRows() + " Columns: " + m1.getColumns());
|
||||
System.out.println("m2 --> Rows: " + m2.getRows() + " Columns: " + m2.getColumns());
|
||||
System.out.println("m3 --> Rows: " + m3.getRows() + " Columns: " + m3.getColumns());
|
||||
|
||||
//check for reference issues
|
||||
System.out.println("m2 -->\n" + m2);
|
||||
data2[1][1] = 101;
|
||||
System.out.println("m2 -->\n" + m2);
|
||||
|
||||
//test equals
|
||||
System.out.println("m2==null: " + m2.equals(null)); //false
|
||||
System.out.println("m3==\"MATRIX\": " + m2.equals("MATRIX")); //false
|
||||
System.out.println("m2==m1: " + m2.equals(m1)); //false
|
||||
System.out.println("m2==m2: " + m2.equals(m2)); //true
|
||||
System.out.println("m2==m3: " + m2.equals(m3)); //false
|
||||
|
||||
//test operations (valid)
|
||||
System.out.println("2 * m2:\n" + m2.scale(2));
|
||||
System.out.println("m2 / 2:\n" + m2.divide(2));
|
||||
System.out.println("m2 + m3:\n" + m2.plus(m3));
|
||||
System.out.println("m2 - m3:\n" + m2.minus(m3));
|
||||
System.out.println("m2 * m3: \n" + m2.multiply(m3));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Data needs to be a deep copy as not to change the original state.
|
||||
*/
|
||||
private int[][] data;
|
||||
|
||||
/**
|
||||
* Constructor for the matrix takes in a 2D array
|
||||
*
|
||||
* @param pData
|
||||
*/
|
||||
public Matrix(int[][] pData) {
|
||||
|
||||
/** Make a deep copy of the data */
|
||||
if (pData.length != 0) {
|
||||
int[][] newData = new int[pData.length][pData[0].length];
|
||||
|
||||
for (int i = 0; i < pData.length; i++)
|
||||
for (int j = 0; j < pData[0].length; j++)
|
||||
newData[i][j] = pData[i][j];
|
||||
|
||||
this.data = newData;
|
||||
} else {
|
||||
this.data = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element specified by the given location
|
||||
*
|
||||
* @param x : x cooridinate
|
||||
* @param y : y cooridinate
|
||||
* @return int : value at location
|
||||
*/
|
||||
public int getElement(int x, int y) {
|
||||
return data[x][y];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of rows in the Matrix
|
||||
*
|
||||
* @return rows
|
||||
*/
|
||||
public int getRows() {
|
||||
if (this.data == null)
|
||||
return 0;
|
||||
|
||||
return data.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of rows in the Matrix
|
||||
*
|
||||
* @return columns
|
||||
*/
|
||||
public int getColumns() {
|
||||
if (this.data == null)
|
||||
return 0;
|
||||
return data[0].length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this matrix scaled by a factor. That is, computes sA where s is a
|
||||
* constant and A is a matrix (this object).
|
||||
*
|
||||
* @param scalar : value to scale by
|
||||
* @return A new matrix scaled by the scalar value
|
||||
*/
|
||||
public Matrix scale(int scalar) {
|
||||
|
||||
int[][] newData = new int[this.data.length][this.data[0].length];
|
||||
|
||||
for (int i = 0; i < this.getRows(); ++i)
|
||||
for (int j = 0; j < this.getColumns(); ++j)
|
||||
newData[i][j] = this.data[i][j] * scalar;
|
||||
|
||||
return new Matrix(newData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this matrix divided by a factor. That is, computes sA where s is a
|
||||
* constant and A is a matrix (this object).
|
||||
*
|
||||
* @param scalar : value to divide by
|
||||
* @return A new matrix scaled by the scalar value
|
||||
*/
|
||||
public Matrix divide(int scalar) {
|
||||
|
||||
int[][] newData = new int[this.data.length][this.data[0].length];
|
||||
|
||||
for (int i = 0; i < this.getRows(); ++i)
|
||||
for (int j = 0; j < this.getColumns(); ++j)
|
||||
newData[i][j] = this.data[i][j] / scalar;
|
||||
|
||||
return new Matrix(newData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds this matrix to another matrix.
|
||||
*
|
||||
* @param other : Matrix to be added
|
||||
* @return addend
|
||||
*/
|
||||
public Matrix plus(Matrix other) throws RuntimeException {
|
||||
|
||||
int[][] newData = new int[this.data.length][this.data[0].length];
|
||||
|
||||
if (this.getRows() != other.getRows() || this.getColumns() != other.getColumns())
|
||||
throw new RuntimeException("Not the same size matrix.");
|
||||
|
||||
for (int i = 0; i < this.getRows(); ++i)
|
||||
for (int j = 0; j < this.getColumns(); ++j)
|
||||
newData[i][j] = this.data[i][j] + other.getElement(i, j);
|
||||
|
||||
return new Matrix(newData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts this matrix from another matrix.
|
||||
*
|
||||
* @param other : Matrix to be subtracted
|
||||
* @return difference
|
||||
*/
|
||||
public Matrix minus(Matrix other) throws RuntimeException {
|
||||
|
||||
int[][] newData = new int[this.data.length][this.data[0].length];
|
||||
|
||||
if (this.getRows() != other.getRows() || this.getColumns() != other.getColumns())
|
||||
throw new RuntimeException("Not the same size matrix.");
|
||||
|
||||
for (int i = 0; i < this.getRows(); ++i)
|
||||
for (int j = 0; j < this.getColumns(); ++j)
|
||||
newData[i][j] = this.data[i][j] - other.getElement(i, j);
|
||||
|
||||
return new Matrix(newData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies this matrix with another matrix.
|
||||
*
|
||||
* @param other : Matrix to be multiplied with
|
||||
* @return product
|
||||
*/
|
||||
public Matrix multiply(Matrix other) throws RuntimeException {
|
||||
|
||||
int[][] newData = new int[this.data.length][other.getColumns()];
|
||||
|
||||
if (this.getColumns() != other.getRows())
|
||||
throw new RuntimeException("The two matrices cannot be multiplied.");
|
||||
int sum;
|
||||
for (int i = 0; i < this.getRows(); ++i)
|
||||
for (int j = 0; j < other.getColumns(); ++j) {
|
||||
sum = 0;
|
||||
for (int k = 0; k < this.getColumns(); ++k) {
|
||||
sum += this.data[i][k] * other.getElement(k, j);
|
||||
}
|
||||
newData[i][j] = sum;
|
||||
}
|
||||
|
||||
|
||||
return new Matrix(newData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the matrix passed is equal to this matrix
|
||||
*
|
||||
* @param other : the other matrix
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean equals(Matrix other) {
|
||||
if (this.getRows() != other.getRows() || this.getColumns() != other.getColumns())
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < this.data.length; i++)
|
||||
for (int j = 0; j < this.data[0].length; j++)
|
||||
if (this.data[i][j] != other.data[i][j])
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Matrix as a String in the following format
|
||||
* <p>
|
||||
* [ a b c ] ...
|
||||
* [ x y z ] ...
|
||||
* [ i j k ] ...
|
||||
* ...
|
||||
*
|
||||
* @return Matrix as String
|
||||
* TODO: Work formatting for different digit sizes
|
||||
*/
|
||||
public String toString() {
|
||||
String str = "";
|
||||
|
||||
for (int i = 0; i < this.data.length; i++) {
|
||||
str += "[ ";
|
||||
for (int j = 0; j < this.data[0].length; j++) {
|
||||
str += data[i][j];
|
||||
str += " ";
|
||||
}
|
||||
str += "]";
|
||||
str += "\n";
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns transposed matrix of this matrix.
|
||||
*
|
||||
* @return transposed Matrix.
|
||||
*/
|
||||
public Matrix transpose() {
|
||||
|
||||
int[][] newData = new int[this.data[0].length][this.data.length];
|
||||
|
||||
for (int i = 0; i < this.getColumns(); ++i)
|
||||
for (int j = 0; j < this.getRows(); ++j)
|
||||
newData[i][j] = this.data[j][i];
|
||||
|
||||
return new Matrix(newData);
|
||||
}
|
||||
}
|
@ -6,8 +6,7 @@ import java.util.Scanner;
|
||||
/**
|
||||
* This class is build to demonstrate the application of the AES-algorithm on a
|
||||
* single 128-Bit block of data.
|
||||
*
|
||||
* @see khalil2535
|
||||
*
|
||||
*/
|
||||
public class AES {
|
||||
|
||||
@ -202,8 +201,7 @@ public class AES {
|
||||
String rBytes = t.toString(16);
|
||||
|
||||
// Add zero padding
|
||||
int rBytesLength = rBytes.length();
|
||||
while (rBytesLength < 8) {
|
||||
while (rBytes.length() < 8) {
|
||||
rBytes = "0" + rBytes;
|
||||
}
|
||||
|
||||
@ -228,8 +226,8 @@ public class AES {
|
||||
currentByteBits = Integer.toHexString(currentByte);
|
||||
|
||||
// Add zero padding
|
||||
int currentByteBitsLength = currentByteBits.length();
|
||||
while (currentByteBitsLength < 2) {
|
||||
|
||||
while (currentByteBits.length() < 2) {
|
||||
currentByteBits = '0' + currentByteBits;
|
||||
}
|
||||
|
||||
@ -304,8 +302,7 @@ public class AES {
|
||||
String blockBits = block.toString(2);
|
||||
|
||||
// Append leading 0 for full "128-bit" string
|
||||
int blockBitsLength = blockBits.length();
|
||||
while (blockBitsLength < 128) {
|
||||
while (blockBits.length() < 128) {
|
||||
blockBits = '0' + blockBits;
|
||||
}
|
||||
|
||||
@ -333,8 +330,7 @@ public class AES {
|
||||
String cellBits = Integer.toBinaryString(cells[i]);
|
||||
|
||||
// Append leading 0 for full "8-bit" strings
|
||||
int cellBitsLength = cellBits.length();
|
||||
while (cellBitsLength < 8) {
|
||||
while (cellBits.length() < 8) {
|
||||
cellBits = '0' + cellBits;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user