1- package ciphers ;
1+ package Ciphers ;
22
33import java .math .BigInteger ;
44import java .util .Scanner ;
@@ -215,58 +215,53 @@ public class AES {
215215
216216 /**
217217 * Subroutine of the Rijndael key expansion.
218- *
219- * @param t
220- * @param rconCounter
221- * @return
222218 */
223219 public static BigInteger scheduleCore (BigInteger t , int rconCounter ) {
224- String rBytes = t .toString (16 );
220+ StringBuilder rBytes = new StringBuilder ( t .toString (16 ) );
225221
226222 // Add zero padding
227223 while (rBytes .length () < 8 ) {
228- rBytes = "0" + rBytes ;
224+ rBytes . insert ( 0 , "0" ) ;
229225 }
230226
231227 // rotate the first 16 bits to the back
232228 String rotatingBytes = rBytes .substring (0 , 2 );
233229 String fixedBytes = rBytes .substring (2 );
234230
235- rBytes = fixedBytes + rotatingBytes ;
231+ rBytes = new StringBuilder ( fixedBytes + rotatingBytes ) ;
236232
237233 // apply S-Box to all 8-Bit Substrings
238234 for (int i = 0 ; i < 4 ; i ++) {
239- String currentByteBits = rBytes .substring (i * 2 , (i + 1 ) * 2 );
235+ StringBuilder currentByteBits = new StringBuilder ( rBytes .substring (i * 2 , (i + 1 ) * 2 ) );
240236
241- int currentByte = Integer .parseInt (currentByteBits , 16 );
237+ int currentByte = Integer .parseInt (currentByteBits . toString () , 16 );
242238 currentByte = SBOX [currentByte ];
243239
244240 // add the current RCON value to the first byte
245241 if (i == 0 ) {
246242 currentByte = currentByte ^ RCON [rconCounter ];
247243 }
248244
249- currentByteBits = Integer .toHexString (currentByte );
245+ currentByteBits = new StringBuilder ( Integer .toHexString (currentByte ) );
250246
251247 // Add zero padding
252248
253249 while (currentByteBits .length () < 2 ) {
254- currentByteBits = '0' + currentByteBits ;
250+ currentByteBits . insert ( 0 , '0' ) ;
255251 }
256252
257253 // replace bytes in original string
258- rBytes = rBytes .substring (0 , i * 2 ) + currentByteBits + rBytes .substring ((i + 1 ) * 2 );
254+ rBytes = new StringBuilder ( rBytes .substring (0 , i * 2 ) + currentByteBits + rBytes .substring ((i + 1 ) * 2 ) );
259255 }
260256
261257 // t = new BigInteger(rBytes, 16);
262258 // return t;
263- return new BigInteger (rBytes , 16 );
259+ return new BigInteger (rBytes . toString () , 16 );
264260 }
265261
266262 /**
267263 * Returns an array of 10 + 1 round keys that are calculated by using Rijndael key schedule
268264 *
269- * @param initialKey
270265 * @return array of 10 + 1 round keys
271266 */
272267 public static BigInteger [] keyExpansion (BigInteger initialKey ) {
@@ -332,11 +327,11 @@ public static BigInteger[] keyExpansion(BigInteger initialKey) {
332327 public static int [] splitBlockIntoCells (BigInteger block ) {
333328
334329 int [] cells = new int [16 ];
335- String blockBits = block .toString (2 );
330+ StringBuilder blockBits = new StringBuilder ( block .toString (2 ) );
336331
337332 // Append leading 0 for full "128-bit" string
338333 while (blockBits .length () < 128 ) {
339- blockBits = '0' + blockBits ;
334+ blockBits . insert ( 0 , '0' ) ;
340335 }
341336
342337 // split 128 to 8 bit cells
@@ -356,24 +351,22 @@ public static int[] splitBlockIntoCells(BigInteger block) {
356351 */
357352 public static BigInteger mergeCellsIntoBlock (int [] cells ) {
358353
359- String blockBits = "" ;
354+ StringBuilder blockBits = new StringBuilder () ;
360355 for (int i = 0 ; i < 16 ; i ++) {
361- String cellBits = Integer .toBinaryString (cells [i ]);
356+ StringBuilder cellBits = new StringBuilder ( Integer .toBinaryString (cells [i ]) );
362357
363358 // Append leading 0 for full "8-bit" strings
364359 while (cellBits .length () < 8 ) {
365- cellBits = '0' + cellBits ;
360+ cellBits . insert ( 0 , '0' ) ;
366361 }
367362
368- blockBits += cellBits ;
363+ blockBits . append ( cellBits ) ;
369364 }
370365
371- return new BigInteger (blockBits , 2 );
366+ return new BigInteger (blockBits . toString () , 2 );
372367 }
373368
374369 /**
375- * @param ciphertext
376- * @param key
377370 * @return ciphertext XOR key
378371 */
379372 public static BigInteger addRoundKey (BigInteger ciphertext , BigInteger key ) {
@@ -383,7 +376,6 @@ public static BigInteger addRoundKey(BigInteger ciphertext, BigInteger key) {
383376 /**
384377 * substitutes 8-Bit long substrings of the input using the S-Box and returns the result.
385378 *
386- * @param ciphertext
387379 * @return subtraction Output
388380 */
389381 public static BigInteger subBytes (BigInteger ciphertext ) {
@@ -401,7 +393,6 @@ public static BigInteger subBytes(BigInteger ciphertext) {
401393 * substitutes 8-Bit long substrings of the input using the inverse S-Box for decryption and
402394 * returns the result.
403395 *
404- * @param ciphertext
405396 * @return subtraction Output
406397 */
407398 public static BigInteger subBytesDec (BigInteger ciphertext ) {
@@ -417,8 +408,6 @@ public static BigInteger subBytesDec(BigInteger ciphertext) {
417408
418409 /**
419410 * Cell permutation step. Shifts cells within the rows of the input and returns the result.
420- *
421- * @param ciphertext
422411 */
423412 public static BigInteger shiftRows (BigInteger ciphertext ) {
424413 int [] cells = splitBlockIntoCells (ciphertext );
@@ -454,8 +443,6 @@ public static BigInteger shiftRows(BigInteger ciphertext) {
454443 /**
455444 * Cell permutation step for decryption . Shifts cells within the rows of the input and returns
456445 * the result.
457- *
458- * @param ciphertext
459446 */
460447 public static BigInteger shiftRowsDec (BigInteger ciphertext ) {
461448 int [] cells = splitBlockIntoCells (ciphertext );
@@ -490,8 +477,6 @@ public static BigInteger shiftRowsDec(BigInteger ciphertext) {
490477
491478 /**
492479 * Applies the Rijndael MixColumns to the input and returns the result.
493- *
494- * @param ciphertext
495480 */
496481 public static BigInteger mixColumns (BigInteger ciphertext ) {
497482
@@ -511,8 +496,6 @@ public static BigInteger mixColumns(BigInteger ciphertext) {
511496
512497 /**
513498 * Applies the inverse Rijndael MixColumns for decryption to the input and returns the result.
514- *
515- * @param ciphertext
516499 */
517500 public static BigInteger mixColumnsDec (BigInteger ciphertext ) {
518501
@@ -563,7 +546,6 @@ public static BigInteger encrypt(BigInteger plainText, BigInteger key) {
563546 * Decrypts the ciphertext with the key and returns the result
564547 *
565548 * @param cipherText The Encrypted text which we want to decrypt
566- * @param key
567549 * @return decryptedText
568550 */
569551 public static BigInteger decrypt (BigInteger cipherText , BigInteger key ) {
@@ -596,30 +578,27 @@ public static void main(String[] args) {
596578 char choice = input .nextLine ().charAt (0 );
597579 String in ;
598580 switch (choice ) {
599- case 'E' :
600- case 'e' :
581+ case 'E' , 'e' -> {
601582 System .out .println ("Choose a plaintext block (128-Bit Integer in base 16):" );
602583 in = input .nextLine ();
603584 BigInteger plaintext = new BigInteger (in , 16 );
604585 System .out .println ("Choose a Key (128-Bit Integer in base 16):" );
605586 in = input .nextLine ();
606587 BigInteger encryptionKey = new BigInteger (in , 16 );
607588 System .out .println (
608- "The encrypted message is: \n " + encrypt (plaintext , encryptionKey ).toString (16 ));
609- break ;
610- case 'D' :
611- case 'd' :
589+ "The encrypted message is: \n " + encrypt (plaintext , encryptionKey ).toString (16 ));
590+ }
591+ case 'D' , 'd' -> {
612592 System .out .println ("Enter your ciphertext block (128-Bit Integer in base 16):" );
613593 in = input .nextLine ();
614594 BigInteger ciphertext = new BigInteger (in , 16 );
615595 System .out .println ("Choose a Key (128-Bit Integer in base 16):" );
616596 in = input .nextLine ();
617597 BigInteger decryptionKey = new BigInteger (in , 16 );
618598 System .out .println (
619- "The deciphered message is:\n " + decrypt (ciphertext , decryptionKey ).toString (16 ));
620- break ;
621- default :
622- System .out .println ("** End **" );
599+ "The deciphered message is:\n " + decrypt (ciphertext , decryptionKey ).toString (16 ));
600+ }
601+ default -> System .out .println ("** End **" );
623602 }
624603 }
625604 }
0 commit comments