Question: Complete the implementation of the AES algorithm operating in ECB mode, partially implemented by the attached code. The class must be capable of both encrypting

Complete the implementation of the AES algorithm operating in ECB mode, partially implemented by the attached code. The class must be capable of both encrypting and decrypting a plain text into a cipher text and back when the same key is passed to the encrypt() and decrypt() methods. AES is a block cipher that operates on bytes directly, so your code should be capable of encrypting any string of Unicode characters and decrypting back to identical Unicode.
import java.util.Arrays;
public class AES {
public final Integer[] keyLengths ={128,192,256};
private int[] key;
private static final HashMap RC = new HashMap>();
static {
RC.put(1,0x01L);
RC.put(2,0x02L);
RC.put(3,0x04L);
RC.put(4,0x080L);
RC.put(5,0x10L);
RC.put(6,0x20L);
RC.put(7,0x40L);
RC.put(8,0x80L);
RC.put(9,0x1BL);
RC.put(10,0x36L);
}
public AES()
{
int index =(int)(Math.random()* keyLengths.length);
genKey(keyLengths[index]);
}
public AES(int keyLength)
{
genKey(keyLength);
}
public int getRandomLength()
{
int index =(int)(Math.random()* keyLengths.length);
return keyLengths[index];
}
private void genKey(int keyLength)
{
key = new int[keyLength /8];
for (int i =0; i key.length; i++)
{
key[i]= randByte();
}
}
private int[] keySchedule()
{
int N = key.length /4;
int R = key.length /4+7;
int WLen =4*R;
int[] W = new int[WLen*4];
for (int i =0; i WLen; i++)
{
if (i N)
{
int[] word = getKeyWord(i);
for (int j =0; j 4; j++)
{
W[4+i+j]= word[j];
}
} else if (i >=N && i % N ==0)
{
int[] word = new int[4];
for (int j =0; j 4; j++)
{
word[j]= W[4*(i-1)+j];
}
word = rotWord(word);
for (int j =0; j 4; j++)
{
word[j]= SBox.encrypt(word[j]);
}
W[4*i]= W[4*(i-N)]^ word[0]^ RC.get(i/N)
for (int j =1; j 4; j++)
{
W[4*i+j]= W[4*(i-N)+j]^ word[j];
}
} else if (i >= N && N >6 && i % N ==4)
{
int[] word = new int[4];
for (int j =0; j 4; j++)
{
word[j]= W[4*(i-1)+j];
}
for (int j =0; j 4; j++)
{
word[j]= SBox.encrypt(word[j]);
}
for (int j =0; j 4; j++)
{
W[4*i+j]= W[4*(i-N)+j]^ word[j];
{ else
{
int[] word = new int[4];
for (int j =0; j 4; j++)
{
word[j]= W[4*(i-1)+j];
}
for (int j =0; j 4; j++)
{
W[4*i+j]= W[4*(i-N)+j]^ word[j];
}
}
}
return W;
}
private int[] getKeyWord(int index)
{
int[] word = new int[4];
for (int i =0; i 4; i++)
{
word[i]= key[4*index +i];
}
return word;
}
private int[] rotWord(int[] word)
{
int[] rot = new int[4];
rot[0]= word[1];
rot[1]= word[2];
rot[2]= word[3];
rot[3]= word[0];
return rot;
}
prvate int randByte()
{
return (int)(Math.random()*256);
}
public String encrypt(String plaintext)
{
return cipher(plaintext, true);
}
public String decrypt(String cipherText)
{
return cipher(cipherText, false);
}
private String cipher(String inText, boolean mode)
{
char[] charArray = inText.toCharArray();
char[][] block = new char[4][4];
StringBuilder outText = new StringBuilder();
int index =0;
while(!getBlock(block, charArray, index))
{
outText.append(cipherBlock(block, mode));
index +=16;
}
return outText.toString();
}
private boolean getBlock(char[][] block, char[] chars, int index)
{
boolean lastBlock = false;
for (int col =0; col 4; col++)
{
for (int row =0; row 4; row++)
{
if (index >= chars.length)
{
block[row][col]=(char)randByte();
lastBlock = true;
} else {
block[row][col]= chars[index];
index++;
}
}
}
return lastBlock;
}
private String cipherBlock(char[][] block, boolean mode)
{
int[] roundKey = keySchedule();
AddRoundKey(block, roundKey, 0);
int numRounds = key.length /4+5;
for (int i =0; i numRounds; i++)
{
SubBytes(block, mode);
ShiftRows(block);
MixColumns(block);
AddRoundKey(block, roundKey, index:i+1);
}
SubBytes(block, mode)
 Complete the implementation of the AES algorithm operating in ECB mode,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!