/*
* To change this
license header, choose License Headers in Project Properties.
* To change this
template file, choose Tools | Templates
* and open the
template in the editor.
*/
package aes;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import javax.crypto.*;
import java.security.*;
import java.security.cert.*;
import java.security.cert.X509Certificate;
import java.security.cert.X509Extension;
import java.util.Arrays;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author obobotette0
*/
public class AES {
/**
* @param args the
command line arguments
*/
public static void
main(String[] args) throws FileNotFoundException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, InvalidAlgorithmParameterException,
UnsupportedEncodingException, IOException, NoSuchProviderException,
CertificateException {
// TODO code
application logic here
//create
variables
//String
inFile = "..\\AES\\src\\aes\\input (another sample)(11).txt";
String inFile
= "..\\AES\\src\\aes\\input(11).txt";
String outFile
= "..\\AES\\src\\aes\\outputas1.txt";
String irpoly
= new String();
String key =
new String();
String
plaintext = new String();
String ciphertext = new String();
String print0
= new String();
String print1
= new String();
StringBuffer
concat0 = new StringBuffer();
StringBuffer
concat1 = new StringBuffer();
StringBuffer
concat2 = new StringBuffer();
StringBuffer
concat3 = new StringBuffer();
StringBuffer
concat4 = new StringBuffer();
int mod = 0;
//Read text
file
File input =
new File(inFile);
Scanner read =
new Scanner(input);
//Assign lines
as suggested
if
(read.hasNextLine()) {
irpoly =
"0000000" + read.nextLine();
key =
read.nextLine();
plaintext
= read.nextLine();
ciphertext
= read.nextLine();
}
read.close();
//Prepare
write file
FileWriter
output = new FileWriter(outFile);
PrintWriter
write = new PrintWriter(output);
irpoly =
irpoly.replace(" ", "");
//Variables
for the java library
//Divide each
string into two parts
String key0 =
key.substring(0, 16);
String key1 =
key.substring(16);
String
plaintext0 = plaintext.substring(0, 16);
String
plaintext1 = plaintext.substring(16);
String
ciphertext0 = ciphertext.substring(0, 16);
String
ciphertext1 = ciphertext.substring(16);
//Variables
for the scripted portion
int k[] = new
int[key.length() / 2];
int p[] = new
int[key.length() / 2];
int c[] = new
int[key.length() / 2];
int[][]
keymatrix = new int[4][4];
int[][]
plainmatrix = new int[4][4];
int[][]
ciphermatrix = new int[4][4];
int[][] result
= new int[4][4];
int[][] newkey
= new int[4][4];
//Loop to
convert 32 chars to 16 ints
for (int i = 0; i < key.length(); i += 2) {
if (Integer.parseInt(String.valueOf(key.codePointAt(i)), 10) - 48 < 10) {
if (Integer.parseInt(String.valueOf(key.codePointAt(i + 1)), 10) - 48 < 10) {
k[i / 2] = (Integer.parseInt(String.valueOf(key.codePointAt(i)), 10) - 48) * 16 + Integer.parseInt(String.valueOf(key.codePointAt(i + 1)), 10) - 48;
}else{
k[i / 2] = (Integer.parseInt(String.valueOf(key.codePointAt(i)), 10) - 48) * 16 + Integer.parseInt(String.valueOf(key.codePointAt(i + 1)), 10) - 87;
}
}else{
if (Integer.parseInt(String.valueOf(key.codePointAt(i + 1)), 10) - 48 < 10) {
k[i / 2] = (Integer.parseInt(String.valueOf(key.codePointAt(i)), 10) - 87) * 16 + Integer.parseInt(String.valueOf(key.codePointAt(i + 1)), 10) - 48;
}else{
k[i / 2] = (Integer.parseInt(String.valueOf(key.codePointAt(i)), 10) - 87) * 16 + Integer.parseInt(String.valueOf(key.codePointAt(i + 1)), 10) - 87;
}
}
if (Integer.parseInt(String.valueOf(plaintext.codePointAt(i)), 10) - 48 < 10) {
if (Integer.parseInt(String.valueOf(plaintext.codePointAt(i + 1)), 10) - 48 < 10) {
p[i / 2] = (Integer.parseInt(String.valueOf(plaintext.codePointAt(i)), 10) - 48) * 16 + Integer.parseInt(String.valueOf(plaintext.codePointAt(i + 1)), 10) - 48;
}else{
p[i / 2] = (Integer.parseInt(String.valueOf(plaintext.codePointAt(i)), 10) - 48) * 16 + Integer.parseInt(String.valueOf(plaintext.codePointAt(i + 1)), 10) - 87;
}
}else{
if (Integer.parseInt(String.valueOf(plaintext.codePointAt(i + 1)), 10) - 48 < 10) {
p[i / 2] = (Integer.parseInt(String.valueOf(plaintext.codePointAt(i)), 10) - 87) * 16 + Integer.parseInt(String.valueOf(plaintext.codePointAt(i + 1)), 10) - 48;
}else{
p[i / 2] = (Integer.parseInt(String.valueOf(plaintext.codePointAt(i)), 10) - 87) * 16 + Integer.parseInt(String.valueOf(plaintext.codePointAt(i + 1)), 10) - 87;
}
}
if (Integer.parseInt(String.valueOf(ciphertext.codePointAt(i)), 10) - 48 < 10) {
if (Integer.parseInt(String.valueOf(ciphertext.codePointAt(i + 1)), 10) - 48 < 10) {
c[i / 2] = (Integer.parseInt(String.valueOf(ciphertext.codePointAt(i)), 10) - 48) * 16 + Integer.parseInt(String.valueOf(ciphertext.codePointAt(i + 1)), 10) - 48;
}else{
c[i / 2] = (Integer.parseInt(String.valueOf(ciphertext.codePointAt(i)), 10) - 48) * 16 + Integer.parseInt(String.valueOf(ciphertext.codePointAt(i + 1)), 10) - 87;
}
}else{
if (Integer.parseInt(String.valueOf(ciphertext.codePointAt(i + 1)), 10) - 48 < 10) {
c[i / 2] = (Integer.parseInt(String.valueOf(ciphertext.codePointAt(i)), 10) - 87) * 16 + Integer.parseInt(String.valueOf(ciphertext.codePointAt(i + 1)), 10) - 48;
}else{
c[i / 2] = (Integer.parseInt(String.valueOf(ciphertext.codePointAt(i)), 10) - 87) * 16 + Integer.parseInt(String.valueOf(ciphertext.codePointAt(i + 1)), 10) - 87;
}
}
//System.out.println(Integer.parseInt(String.valueOf(key.codePointAt(i)), 10));
}
System.out.println(k.length + " k " + Arrays.toString(k));
System.out.println(p.length + " p " + Arrays.toString(p));
System.out.println(c.length + " c " + Arrays.toString(c));
System.out.println(key.length() + " key " + key);
System.out.println(plaintext.length() + " plaintext " + plaintext);
System.out.println(ciphertext.length() + " ciphertext " + ciphertext);
//loop to create
matrix for transformations
for (int i =
0; i < k.length; i++) {
keymatrix[i / 4][i % 4] = k[i];
plainmatrix[i / 4][i % 4] = p[i];
ciphermatrix[i / 4][i % 4] = c[i];
}
//Variables
for transformations
int[][] mixcol
= {{02, 03, 01, 01}, {01, 02, 03, 01}, {01, 01, 02, 03}, {03, 01, 01, 02}};
int[][]
invmixcol = {{Integer.parseInt("0e", 16),
Integer.parseInt("0b", 16), Integer.parseInt("0d", 16),
Integer.parseInt("09", 16)},
{Integer.parseInt("09", 16), Integer.parseInt("0e",
16), Integer.parseInt("0b", 16), Integer.parseInt("0d",
16)},
{Integer.parseInt("0d", 16), Integer.parseInt("09",
16), Integer.parseInt("0e", 16), Integer.parseInt("0b", 16)},
{Integer.parseInt("0b", 16), Integer.parseInt("0d",
16), Integer.parseInt("09", 16), Integer.parseInt("0e",
16)}};
AlgorithmParameters algo0 =
AlgorithmParameters.getInstance("AES");
IvParameterSpec iv0 = new IvParameterSpec(irpoly.getBytes("UTF-8"));
SecretKeySpec
skey0 = new SecretKeySpec(key0.getBytes("UTF-8"), "AES");
SecretKeySpec
skey1 = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
//Tried two
different block ciphers
Cipher transform
= Cipher.getInstance("AES/ECB/NoPadding");
Cipher
transform2 = Cipher.getInstance("AES/CBC/NoPadding");
//Show
plaintext
write.append("Plaintext " + plaintext);
write.println();
//Loop for
scripted AES process
for (int i =
0; i <= 10; i++) {
if (i ==
0) {
newkey
= keyexpansion(keymatrix, irpoly, i);
result
= addroundkey(plainmatrix, newkey);
System.out.println( " round " + i + " " + Arrays.deepToString(result));
} else if
(i > 0 && i < 10) {
result
= subbytes(result);
result
= shiftrows(result);
result
= mixcolumns(result, mixcol, irpoly);
newkey
= keyexpansion(newkey, irpoly, i);
System.out.println( " round " + i + " " + Arrays.deepToString(result));
//System.out.println(i);
} else if
(i == 10) {
result
= subbytes(result);
result
= shiftrows(result);
newkey
= keyexpansion(newkey, irpoly, i);
result
= addroundkey(result, newkey);
System.out.println( " round " + i + " " + Arrays.deepToString(result));
}
}
System.out.println(Integer.parseInt(irpoly, 2) + " nums " +
irpoly);
System.out.println(keymatrix.length + " matrix " +
Arrays.deepToString(keymatrix));
System.out.println(plainmatrix.length + " plain " +
Arrays.deepToString(plainmatrix));
//Return
absolute value of matrix cell as a value from 0 to 255
for (int i =
0; i < 16; i++){
concat0.append(Integer.toHexString(Math.abs(result[i/4][i%4]%256)));
}
System.out.println(result.length + " result " +
Arrays.deepToString(result));
System.out.println(concat0.length() + " concat " + concat0);
//Print results
for (int i = 0; i < k.length; i++) {
k[i] = keymatrix[i / 4][i % 4];
p[i] = plainmatrix[i / 4][i % 4];
c[i] = ciphermatrix[i / 4][i % 4];
}
System.out.println(k.length + " k " + Arrays.toString(k));
System.out.println(p.length + " p " + Arrays.toString(p));
System.out.println(c.length + " c " + Arrays.toString(c));
System.out.println();
write.append("Manual encryption AES " + concat0);
write.println();
//transform.init(Cipher.WRAP_MODE, skey0);
//transform.init(Cipher.WRAP_MODE, skey1);
//Java library
encryption with ECB
transform.init(Cipher.ENCRYPT_MODE,
skey0);
byte[] encText0 = transform.doFinal(plaintext0.getBytes("UTF-8"));
transform.init(Cipher.ENCRYPT_MODE, skey1);
byte[]
encText1 = transform.doFinal(plaintext1.getBytes("UTF-8"));
//Loop to print
result
for (int i =
0; i < 32; i++) {
if (i <
16) {
concat4.append(Integer.toHexString(Math.abs(encText0[i] % 16)));
} else {
concat4.append(Integer.toHexString(Math.abs(encText1[i % 16] % 16)));
}
}
System.out.println(encText0.length + encText1.length);
System.out.println(Arrays.toString(encText0) +
Arrays.toString(encText1));
System.out.println(concat4);
write.append("AES encryption using ECB " + concat4);
write.append("\n");
//System.out.println(Arrays.toString(result));
//transform2.init(Cipher.WRAP_MODE, skey0, iv0);
//transform2.init(Cipher.WRAP_MODE, skey1, iv0);
//Java
library with CBC
transform2.init(Cipher.ENCRYPT_MODE, skey0,
iv0);
encText0 = transform2.doFinal(plaintext0.getBytes("UTF-8"));
transform2.init(Cipher.ENCRYPT_MODE, skey1, iv0);
encText1 =
transform2.doFinal(plaintext1.getBytes("UTF-8"));
//Loop to print result
for (int i =
0; i < 32; i++) {
if (i <
16) {
concat1.append(Integer.toHexString(Math.abs(encText0[i] % 16)));
} else {
concat1.append(Integer.toHexString(Math.abs(encText1[i % 16] % 16)));
}
}
System.out.println(encText0.length + encText1.length);
System.out.println(Arrays.toString(encText0) +
Arrays.toString(encText1));
System.out.println(concat1);
write.append("AES encryption using CBC " + concat1);
write.append("\n");
//System.out.println(Arrays.toString(result));
//Decryption
with ECB
transform.init(Cipher.DECRYPT_MODE, skey0);//, iv0);
byte[] decText0 = transform.doFinal(ciphertext0.getBytes("UTF-8"));
transform.init(Cipher.DECRYPT_MODE, skey1);//, iv1);
byte[]
decText1 = transform.doFinal(ciphertext1.getBytes("UTF-8"));
//byte[]
decText0 = transform.doFinal(encText0);
//byte[]
decText1 = transform.doFinal(encText1);
for (int i =
0; i < 32; i++) {
if (i <
16) {
concat2.append(Integer.toHexString(Math.abs(decText0[i] % 16)));
} else {
concat2.append(Integer.toHexString(Math.abs(decText1[i % 16] % 16)));
}
}
System.out.println(decText0.length + decText1.length);
System.out.println(Arrays.toString(decText0) +
Arrays.toString(decText1));
System.out.println(concat2);
write.append("AES decryption using ECB " + concat2);
write.append("\n");
//System.out.println(Arrays.toString(result));
//Decryption
with CBC
transform2.init(Cipher.DECRYPT_MODE, skey0, iv0);
decText0 = transform2.doFinal(ciphertext0.getBytes("UTF-8"));
transform2.init(Cipher.DECRYPT_MODE, skey1, iv0);
decText1 =
transform2.doFinal(ciphertext1.getBytes("UTF-8"));
//decText0 =
transform2.doFinal(encText0);
//decText1 =
transform2.doFinal(encText1);
for (int i =
0; i < 32; i++) {
if (i <
16) {
concat3.append(Integer.toHexString(Math.abs(decText0[i] % 16)));
} else {
concat3.append(Integer.toHexString(Math.abs(decText1[i % 16] % 16)));
}
}
System.out.println(decText0.length
+ decText1.length);
System.out.println(Arrays.toString(decText0) +
Arrays.toString(decText1));
System.out.println(concat3);
write.append("AES decryption using CBC " + concat3);
write.append("\n");
//Expected
result
write.append("Expected cipher result " + ciphertext);
write.println();
write.flush();
//System.out.println(Arrays.toString(result));
}
public static
int[][] subbytes(int[][] text) {
String[] swap0 = {"63", "7c", "77", "7b", "f2", "6b", "6f", "c5", "30", "01", "67", "2b", "fe", "d7", "ab", "76","ca", "82", "c9", "7d", "fa", "59", "47", "f0", "ad", "d4", "a2", "af", "9c", "a4", "72", "c0","b7", "fd", "93", "26", "36", "3f", "f7", "cc", "34", "a5", "e5", "f1", "71", "d8", "31", "15","04", "c7", "23", "c3", "18", "96", "05", "9a", "07", "12", "80", "e2", "eb", "27", "b2", "75","09", "83", "2c", "1a", "1b", "6e", "5a", "a0", "52", "3b", "d6", "b3", "29", "e3", "2f", "84", "53", "d1", "00", "ed", "20", "fc", "b1", "5b", "6a", "cb", "be", "39", "4a", "4c", "58", "cf","d0", "ef", "aa", "fb", "43", "4d", "33", "85", "45", "f9", "02", "7f", "50", "3c", "9f", "a8","51", "a3", "40", "8f", "92", "9d", "38", "f5", "bc", "b6", "da", "21", "10", "ff", "f3", "d2","cd", "0c", "13", "ec", "5f", "97", "44", "17", "c4", "a7", "7e", "3d", "64", "5d", "19", "73","60", "81", "4f", "dc", "22", "2a", "90", "88", "46", "ee", "b8", "14", "de", "5e", "0b", "db","e0", "32", "3a", "0a", "49", "06", "24", "5c", "c2", "d3", "ac", "62", "91", "95", "e4", "79","e7", "c8", "37", "6d", "8d", "d5", "4e", "a9", "6c", "56", "f4", "ea", "65", "7a", "ae", "08","ba", "78", "25", "2e", "1c", "a6", "b4", "c6", "e8", "dd", "74", "1f", "4b", "bd", "8b", "8a","70", "3e", "b5", "66", "48", "03", "f6", "0e", "61", "35", "57", "b9", "86", "c1", "1d", "93","e1", "f8", "98", "11", "69", "d9", "8e", "94", "9b", "1e", "87", "e9", "ce", "55", "28", "df","8c", "a1", "89", "0d", "bf", "e6", "42", "68", "41", "99", "2d", "0f", "b0", "54", "bb", "16"};
for (int i =
0; i < 16; i++) {
for (int j
= 0; j < swap0.length; j++) {
//if
array element is equal to j
if
(text[i / 4][i % 4] == j) {
//swap with j element of sbox transformation
text[i / 4][i % 4] =
Integer.parseInt(swap0[j], 16);
}
}
}
return text;
}
public static
int[][] invsubbytes(int[][] text) {
String[] swap0 = {"52", "09", "6a", "d5", "30", "36", "a5", "38", "bf", "40", "a3", "9e", "81", "f3", "d7", "fb","7c", "e3", "39", "82", "9b", "2f", "ff", "87", "34", "8e", "43", "44", "c4", "de", "e9", "cb","54", "7b", "94", "32", "a6", "c2", "23", "3d", "ee", "4c", "95", "0b", "42", "fa", "c3", "4e","08", "2e", "a1", "66", "28", "d9", "24", "b2", "76", "5b", "a2", "49", "6d", "8b", "d1", "25","72", "f8", "f6", "64", "86", "68", "98", "16", "d4", "a4", "5c", "cc", "5d", "65", "b6", "92","6c", "70", "48", "50", "fd", "ed", "b9", "da", "5e", "15", "46", "57", "a7", "8d", "9d", "84","90", "d8", "ab", "00", "8c", "bc", "d3", "0a", "f7", "e4", "58", "05", "b8", "b3", "45", "06","d0", "2c", "1e", "8f", "ca", "3f", "0f", "02", "c1", "af", "bd", "03", "01", "13", "8a", "6b","3a", "91", "11", "41", "4f", "67", "dc", "ea", "97", "f2", "cf", "ce", "f0", "b4", "e6", "73","96", "ac", "74", "22", "e7", "ad", "35", "85", "e2", "f9", "37", "e8", "1c", "75", "df", "6e","47", "f1", "1a", "71", "1d", "29", "c5", "89", "6f", "b7", "62", "0e", "aa", "18", "be", "1b","fc", "56", "3e", "4b", "c6", "d2", "79", "20", "9a", "db", "c0", "fe", "78", "cd", "5a", "f4","1f", "dd", "a8", "33", "88", "07", "c7", "31", "b1", "12", "10", "59", "27", "80", "ec", "5f","60", "51", "7f", "a9", "19", "b5", "4a", "0d", "2d", "e5", "7a", "9f", "93", "c9", "9c", "ef","a0", "e0", "3b", "4d", "ae", "2a", "f5", "b0", "c8", "eb", "bb", "3c", "83", "53", "99", "61","17", "2b", "04", "7e", "ba", "77", "d6", "26", "e1", "69", "14", "63", "55", "21", "0c", "7d"};
for (int i =
0; i < 16; i++) {
for (int j
= 0; j < swap0.length; j++) {
//if
array element is equal to j
if
(text[i / 4][i % 4] == j) {
//swap with j element of sbox transformation
text[i / 4][i % 4] = Integer.parseInt(swap0[j], 16);
}
}
}
return text;
}
public static
int[][] shiftrows(int[][] text) {
int swap,
swap1, swap2;
swap =
text[1][0];
text[1][0] =
text[1][1];
text[1][1] =
text[1][2];
text[1][2] =
text[1][3];
text[1][3] =
swap;
swap =
text[2][0];
swap1 =
text[2][1];
text[2][0] =
text[2][2];
text[2][1] =
text[2][3];
text[2][2] =
swap;
text[2][3] =
swap1;
swap =
text[3][0];
swap1 =
text[3][1];
swap2 =
text[3][2];
text[3][0] =
text[3][3];
text[3][1] =
swap;
text[3][2] =
swap1;
text[3][3] =
swap2;
return text;
}
public static
int[][] invshiftrows(int[][] text) {
int swap,
swap1, swap2;
swap =
text[1][0];
swap1 =
text[1][1];
swap2 =
text[1][2];
text[1][0] =
text[1][3];
text[1][1] =
swap;
text[1][2] =
swap1;
text[1][3] =
swap2;
swap =
text[2][0];
swap1 =
text[2][1];
text[2][0] =
text[2][2];
text[2][1] =
text[2][3];
text[2][2] =
swap;
text[2][3] =
swap1;
swap =
text[3][0];
swap1 =
text[3][1];
swap2 = text[3][2];
text[3][0] =
swap1;
text[3][1] =
swap2;
text[3][2] =
text[3][3];
text[3][3] =
swap;
return text;
}
public static
int[][] mixcolumns(int[][] text, int[][] key, String poly) {
int[][] temp =
text;
//int[][]
mixcol = {{02, 03, 01, 01}, {01, 02, 03, 01}, {01, 01, 02, 03}, {03, 01, 01,
02}};
text[0][0] =
(temp[0][0] * key[0][0]) ^ (temp[0][1] * key[0][1]) ^ (temp[0][2] * key[0][2])
^ (temp[0][3] * key[0][3]);
text[1][0] =
(temp[0][0] * key[1][0]) ^ (temp[0][1] * key[1][1]) ^ (temp[0][2] * key[1][2])
^ (temp[0][3] * key[1][3]);
text[2][0] =
(temp[0][0] * key[2][0]) ^ (temp[0][1] * key[2][1]) ^ (temp[0][2] * key[2][2])
^ (temp[0][3] * key[2][3]);
text[3][0] =
(temp[0][0] * key[3][0]) ^ (temp[0][1] * key[3][1]) ^ (temp[0][2] * key[3][2])
^ (temp[0][3] * key[3][3]);
text[0][1] =
(temp[1][0] * key[0][0]) ^ (temp[1][1] * key[0][1]) ^ (temp[1][2] * key[0][2])
^ (temp[1][3] * key[0][3]);
text[1][1] =
(temp[1][0] * key[1][0]) ^ (temp[1][1] * key[1][1]) ^ (temp[1][2] * key[1][2])
^ (temp[1][3] * key[1][3]);
text[2][1] =
(temp[1][0] * key[2][0]) ^ (temp[1][1] * key[2][1]) ^ (temp[1][2] * key[2][2])
^ (temp[1][3] * key[2][3]);
text[3][1] =
(temp[1][0] * key[3][0]) ^ (temp[1][1] * key[3][1]) ^ (temp[1][2] * key[3][2])
^ (temp[1][3] * key[3][3]);
text[0][2] =
(temp[2][0] * key[0][0]) ^ (temp[2][1] * key[0][1]) ^ (temp[2][2] * key[0][2])
^ (temp[2][3] * key[0][3]);
text[1][2] =
(temp[2][0] * key[1][0]) ^ (temp[2][1] * key[1][1]) ^ (temp[2][2] * key[1][2])
^ (temp[2][3] * key[1][3]);
text[2][2] =
(temp[2][0] * key[2][0]) ^ (temp[2][1] * key[2][1]) ^ (temp[2][2] * key[2][2])
^ (temp[2][3] * key[2][3]);
text[3][2] =
(temp[2][0] * key[3][0]) ^ (temp[2][1] * key[3][1]) ^ (temp[2][2] * key[3][2])
^ (temp[2][3] * key[3][3]);
text[0][3] =
(temp[3][0] * key[0][0]) ^ (temp[3][1] * key[0][1]) ^ (temp[3][2] * key[0][2])
^ (temp[3][3] * key[0][3]);
text[1][3] =
(temp[3][0] * key[1][0]) ^ (temp[3][1] * key[1][1]) ^ (temp[3][2] * key[1][2])
^ (temp[3][3] * key[1][3]);
text[2][3] =
(temp[3][0] * key[2][0]) ^ (temp[3][1] * key[2][1]) ^ (temp[3][2] * key[2][2])
^ (temp[3][3] * key[2][3]);
text[3][3] =
(temp[3][0] * key[3][0]) ^ (temp[3][1] * key[3][1]) ^ (temp[3][2] * key[3][2])
^ (temp[3][3] * key[3][3]);
for (int i =
0; i < 16; i++) {
text[i /
4][i % 4] %= Integer.parseInt(poly, 2);
text[i / 4][i % 4] %= 256;
}
return text;
}
public static
int[][] invmixcolumns(int[][] text, int[][] key, String poly) {
int[][] temp =
text;
/*int[][]
invmixcol = {{Integer.parseInt("0e", 16), Integer.parseInt("0b",
16), Integer.parseInt("0d", 16), Integer.parseInt("09",
16)},
{Integer.parseInt("09", 16), Integer.parseInt("0e",
16), Integer.parseInt("0b", 16), Integer.parseInt("0d",
16)},
{Integer.parseInt("0d", 16), Integer.parseInt("09",
16), Integer.parseInt("0e", 16), Integer.parseInt("0b",
16)},
{Integer.parseInt("0b", 16), Integer.parseInt("0d",
16), Integer.parseInt("09", 16), Integer.parseInt("0e",
16)}};*/
text[0][0] =
temp[0][0] * key[0][0] ^ temp[0][1] * key[0][1] ^ temp[0][2] * key[0][2] ^
temp[0][3] * key[0][3];
text[1][0] =
temp[0][0] * key[1][0] ^ temp[0][1] * key[1][1] ^ temp[0][2] * key[1][2] ^
temp[0][3] * key[1][3];
text[2][0] =
temp[0][0] * key[2][0] ^ temp[0][1] * key[2][1] ^ temp[0][2] * key[2][2] ^
temp[0][3] * key[2][3];
text[3][0] =
temp[0][0] * key[3][0] ^ temp[0][1] * key[3][1] ^ temp[0][2] * key[3][2] ^
temp[0][3] * key[3][3];
text[0][1] =
temp[1][0] * key[0][0] ^ temp[1][1] * key[0][1] ^ temp[1][2] * key[0][2] ^
temp[1][3] * key[0][3];
text[1][1] =
temp[1][0] * key[1][0] ^ temp[1][1] * key[1][1] ^ temp[1][2] * key[1][2] ^
temp[1][3] * key[1][3];
text[2][1] =
temp[1][0] * key[2][0] ^ temp[1][1] * key[2][1] ^ temp[1][2] * key[2][2] ^
temp[1][3] * key[2][3];
text[3][1] =
temp[1][0] * key[3][0] ^ temp[1][1] * key[3][1] ^ temp[1][2] * key[3][2] ^
temp[1][3] * key[3][3];
text[0][2] =
temp[2][0] * key[0][0] ^ temp[2][1] * key[0][1] ^ temp[2][2] * key[0][2] ^
temp[2][3] * key[0][3];
text[1][2] =
temp[2][0] * key[1][0] ^ temp[2][1] * key[1][1] ^ temp[2][2] * key[1][2] ^
temp[2][3] * key[1][3];
text[2][2] =
temp[2][0] * key[2][0] ^ temp[2][1] * key[2][1] ^ temp[2][2] * key[2][2] ^
temp[2][3] * key[2][3];
text[3][2] =
temp[2][0] * key[3][0] ^ temp[2][1] * key[3][1] ^ temp[2][2] * key[3][2] ^
temp[2][3] * key[3][3];
text[0][3] =
temp[3][0] * key[0][0] ^ temp[3][1] * key[0][1] ^ temp[3][2] * key[0][2] ^
temp[3][3] * key[0][3];
text[1][3] =
temp[3][0] * key[1][0] ^ temp[3][1] * key[1][1] ^ temp[3][2] * key[1][2] ^
temp[3][3] * key[1][3];
text[2][3] =
temp[3][0] * key[2][0] ^ temp[3][1] * key[2][1] ^ temp[3][2] * key[2][2] ^
temp[3][3] * key[2][3];
text[3][3] =
temp[3][0] * key[3][0] ^ temp[3][1] * key[3][1] ^ temp[3][2] * key[3][2] ^
temp[3][3] * key[3][3];
for (int i =
0; i < 16; i++) {
text[i /
4][i % 4] %= Integer.parseInt(poly, 2);
}
return text;
}
public static
int[][] addroundkey(int[][] text, int[][] key) {
for (int i =
0; i < 16; i++) {
text[i /
4][i % 4] ^= key[i / 4][i % 4];
text[i /
4][i % 4] %= 256;
}
return text;
}
public static
int[][] keyexpansion(int[][] key, String poly, int num) {
if (num == 0)
{
return
key;
} else {
int mul =
(int) Math.pow(2, num);
int rcon =
Integer.parseInt(poly,2) * mul;
int[] word
= {key[3][1], key[3][2], key[3][3], key[3][0]};
String[] swap0 = {"63", "7c", "77", "7b", "f2", "6b", "6f", "c5", "30", "01", "67", "2b", "fe", "d7", "ab", "76","ca", "82", "c9", "7d", "fa", "59", "47", "f0", "ad", "d4", "a2", "af", "9c", "a4", "72", "c0","b7", "fd", "93", "26", "36", "3f", "f7", "cc", "34", "a5", "e5", "f1", "71", "d8", "31", "15","04", "c7", "23", "c3", "18", "96", "05", "9a", "07", "12", "80", "e2", "eb", "27", "b2", "75","09", "83", "2c", "1a", "1b", "6e", "5a", "a0", "52", "3b", "d6", "b3", "29", "e3", "2f", "84","53", "d1", "00", "ed", "20", "fc", "b1", "5b", "6a", "cb", "be", "39", "4a", "4c", "58", "cf","d0", "ef", "aa", "fb", "43", "4d", "33", "85", "45", "f9", "02", "7f", "50", "3c", "9f", "a8","51", "a3", "40", "8f", "92", "9d", "38", "f5", "bc", "b6", "da", "21", "10", "ff", "f3", "d2","cd", "0c", "13", "ec", "5f", "97", "44", "17", "c4", "a7", "7e", "3d", "64", "5d", "19", "73","60", "81", "4f", "dc", "22", "2a", "90", "88", "46", "ee", "b8", "14", "de", "5e", "0b", "db","e0", "32", "3a", "0a", "49", "06", "24", "5c", "c2", "d3", "ac", "62", "91", "95", "e4", "79","e7", "c8", "37", "6d", "8d", "d5", "4e", "a9", "6c", "56", "f4", "ea", "65", "7a", "ae", "08","ba", "78", "25", "2e", "1c", "a6", "b4", "c6", "e8", "dd", "74", "1f", "4b", "bd", "8b", "8a","70", "3e", "b5", "66", "48", "03", "f6", "0e", "61", "35", "57", "b9", "86", "c1", "1d", "93","e1", "f8", "98", "11", "69", "d9", "8e", "94", "9b", "1e", "87", "e9", "ce", "55", "28", "df","8c", "a1", "89", "0d", "bf", "e6", "42", "68", "41", "99", "2d", "0f", "b0", "54", "bb", "16"};
for (int i
= 0; i < 4; i++) {
for
(int j = 0; j < swap0.length; j++) {
//if array element is equal to j
if (word[i] == j) {
//swap with j element of sbox transformation
word[i] = Integer.parseInt(swap0[j], 16) ^ rcon;
}
}
}
for (int i
= 0; i < 4; i++) {
key[i
/ 4][i % 4] ^= word[i % 4];
key[i
/ 4][i % 4] %= 256;
}
for (int i
= 4; i < 16; i++) {
key[i
/ 4][i % 4] ^= key[(i / 4) - 1][(i % 4)];
key[i
/ 4][i % 4] %= 256;
}
return
key;
}
}
}
No comments:
Post a Comment
Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!