Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
53544c2
Update stale.yml
siriak Sep 26, 2021
ee33e3c
Update stale.yml
siriak Sep 26, 2021
96b66c7
Update stale.yml
siriak Sep 26, 2021
3dc5378
Cleanup build errors
siriak Sep 26, 2021
b253105
updating DIRECTORY.md
Sep 26, 2021
b2ada76
Fix indentation
siriak Sep 26, 2021
3ab3863
Merge branch 'siriak-patch-1' of https://github.com/TheAlgorithms/Jav…
siriak Sep 26, 2021
db3dfc7
Update Java
siriak Sep 26, 2021
5ac57b6
Remove Travis
siriak Sep 26, 2021
91ec6f2
Fix build
siriak Sep 26, 2021
1cd6b52
updating DIRECTORY.md
Sep 26, 2021
5396003
Cleanup README.md
siriak Sep 26, 2021
9525403
Merge branch 'siriak-patch-1' of https://github.com/TheAlgorithms/Jav…
siriak Sep 26, 2021
5a006ee
Enable formatter for all branches
siriak Sep 26, 2021
b424c32
Simplify workflows
siriak Sep 26, 2021
0fd704c
Simplify workflows
siriak Sep 26, 2021
4f8e68f
Prettify code
siriak Sep 26, 2021
86281de
Simplify workflows
siriak Sep 26, 2021
35813da
Merge branch 'siriak-patch-1' of https://github.com/TheAlgorithms/Jav…
siriak Sep 26, 2021
8026635
updating DIRECTORY.md
Sep 26, 2021
7c09263
Testing
siriak Sep 26, 2021
d882e9e
Prettify code
siriak Sep 26, 2021
b57b421
Testing
siriak Sep 26, 2021
82ec2a3
Merge branch 'siriak-patch-1' of https://github.com/TheAlgorithms/Jav…
siriak Sep 26, 2021
42454b8
updating DIRECTORY.md
Sep 26, 2021
1b9d4bc
Testing
siriak Sep 26, 2021
36dd133
Merge branch 'siriak-patch-1' of https://github.com/TheAlgorithms/Jav…
siriak Sep 26, 2021
d25a9bc
Testing
siriak Sep 26, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
Expand Down
7 changes: 3 additions & 4 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

**Is your feature request related to a problem? Please describe.**
Expand Down
14 changes: 4 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
name: Build Project

on:
pull_request:
push:
branches:
- master

name: Build
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: Set up JDK 12
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 12
java-version: 17
- run: find . -type f -name "*.java" > sources.txt
- run: javac @sources.txt
29 changes: 0 additions & 29 deletions .github/workflows/checkstyle.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
name: Prettier

on:
pull_request:
push:
branches:
- master
- Development

name: Prettify
on: push
jobs:
prettier:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -20,7 +12,7 @@ jobs:
- name: Prettify code
uses: creyD/prettier_action@v3.3
with:
prettier_options: --write **/*.{java}
commit_message: 'style: prettify code'
prettier_options: --write **/*.java
commit_message: 'Prettify code'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 1 addition & 2 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 'Close stale issues and PRs'
on:
schedule:
- cron: '0 */10 * * *'
- cron: '0 0 * * *'
jobs:
stale:
runs-on: ubuntu-latest
Expand All @@ -16,4 +16,3 @@ jobs:
exempt-pr-labels: 'dont-close'
days-before-stale: 30
days-before-close: 7
operations-per-run: 150
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# This GitHub Action updates the DIRECTORY.md file (if needed) when doing a git push
name: update_directory_md
on: [push]
name: Update Directory
on: push
jobs:
update_directory_md:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-python@master
- name: update_directory_md
- name: Update Directory
shell: python
run: |
import os
Expand Down Expand Up @@ -63,5 +63,5 @@ jobs:
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
git add DIRECTORY.md
git commit -am "updating DIRECTORY.md" || true
git commit -am "Update directory" || true
git push --force origin HEAD:$GITHUB_REF || true
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

2 changes: 0 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
:+1::tada: Before guiding you through the contribution process TheAlgorithms/Java thank you for being one of us! :+1::tada:

## How to contribute?

#### **Did you find a bug?**
Expand Down
69 changes: 24 additions & 45 deletions ciphers/AES.java → Ciphers/AES.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ciphers;
package Ciphers;

import java.math.BigInteger;
import java.util.Scanner;
Expand Down Expand Up @@ -215,58 +215,53 @@ public class AES {

/**
* Subroutine of the Rijndael key expansion.
*
* @param t
* @param rconCounter
* @return
*/
public static BigInteger scheduleCore(BigInteger t, int rconCounter) {
String rBytes = t.toString(16);
StringBuilder rBytes = new StringBuilder(t.toString(16));

// Add zero padding
while (rBytes.length() < 8) {
rBytes = "0" + rBytes;
rBytes.insert(0, "0");
}

// rotate the first 16 bits to the back
String rotatingBytes = rBytes.substring(0, 2);
String fixedBytes = rBytes.substring(2);

rBytes = fixedBytes + rotatingBytes;
rBytes = new StringBuilder(fixedBytes + rotatingBytes);

// apply S-Box to all 8-Bit Substrings
for (int i = 0; i < 4; i++) {
String currentByteBits = rBytes.substring(i * 2, (i + 1) * 2);
StringBuilder currentByteBits = new StringBuilder(rBytes.substring(i * 2, (i + 1) * 2));

int currentByte = Integer.parseInt(currentByteBits, 16);
int currentByte = Integer.parseInt(currentByteBits.toString(), 16);
currentByte = SBOX[currentByte];

// add the current RCON value to the first byte
if (i == 0) {
currentByte = currentByte ^ RCON[rconCounter];
}

currentByteBits = Integer.toHexString(currentByte);
currentByteBits = new StringBuilder(Integer.toHexString(currentByte));

// Add zero padding

while (currentByteBits.length() < 2) {
currentByteBits = '0' + currentByteBits;
currentByteBits.insert(0, '0');
}

// replace bytes in original string
rBytes = rBytes.substring(0, i * 2) + currentByteBits + rBytes.substring((i + 1) * 2);
rBytes = new StringBuilder(rBytes.substring(0, i * 2) + currentByteBits + rBytes.substring((i + 1) * 2));
}

// t = new BigInteger(rBytes, 16);
// return t;
return new BigInteger(rBytes, 16);
return new BigInteger(rBytes.toString(), 16);
}

/**
* Returns an array of 10 + 1 round keys that are calculated by using Rijndael key schedule
*
* @param initialKey
* @return array of 10 + 1 round keys
*/
public static BigInteger[] keyExpansion(BigInteger initialKey) {
Expand Down Expand Up @@ -332,11 +327,11 @@ public static BigInteger[] keyExpansion(BigInteger initialKey) {
public static int[] splitBlockIntoCells(BigInteger block) {

int[] cells = new int[16];
String blockBits = block.toString(2);
StringBuilder blockBits = new StringBuilder(block.toString(2));

// Append leading 0 for full "128-bit" string
while (blockBits.length() < 128) {
blockBits = '0' + blockBits;
blockBits.insert(0, '0');
}

// split 128 to 8 bit cells
Expand All @@ -356,24 +351,22 @@ public static int[] splitBlockIntoCells(BigInteger block) {
*/
public static BigInteger mergeCellsIntoBlock(int[] cells) {

String blockBits = "";
StringBuilder blockBits = new StringBuilder();
for (int i = 0; i < 16; i++) {
String cellBits = Integer.toBinaryString(cells[i]);
StringBuilder cellBits = new StringBuilder(Integer.toBinaryString(cells[i]));

// Append leading 0 for full "8-bit" strings
while (cellBits.length() < 8) {
cellBits = '0' + cellBits;
cellBits.insert(0, '0');
}

blockBits += cellBits;
blockBits.append(cellBits);
}

return new BigInteger(blockBits, 2);
return new BigInteger(blockBits.toString(), 2);
}

/**
* @param ciphertext
* @param key
* @return ciphertext XOR key
*/
public static BigInteger addRoundKey(BigInteger ciphertext, BigInteger key) {
Expand All @@ -383,7 +376,6 @@ public static BigInteger addRoundKey(BigInteger ciphertext, BigInteger key) {
/**
* substitutes 8-Bit long substrings of the input using the S-Box and returns the result.
*
* @param ciphertext
* @return subtraction Output
*/
public static BigInteger subBytes(BigInteger ciphertext) {
Expand All @@ -401,7 +393,6 @@ public static BigInteger subBytes(BigInteger ciphertext) {
* substitutes 8-Bit long substrings of the input using the inverse S-Box for decryption and
* returns the result.
*
* @param ciphertext
* @return subtraction Output
*/
public static BigInteger subBytesDec(BigInteger ciphertext) {
Expand All @@ -417,8 +408,6 @@ public static BigInteger subBytesDec(BigInteger ciphertext) {

/**
* Cell permutation step. Shifts cells within the rows of the input and returns the result.
*
* @param ciphertext
*/
public static BigInteger shiftRows(BigInteger ciphertext) {
int[] cells = splitBlockIntoCells(ciphertext);
Expand Down Expand Up @@ -454,8 +443,6 @@ public static BigInteger shiftRows(BigInteger ciphertext) {
/**
* Cell permutation step for decryption . Shifts cells within the rows of the input and returns
* the result.
*
* @param ciphertext
*/
public static BigInteger shiftRowsDec(BigInteger ciphertext) {
int[] cells = splitBlockIntoCells(ciphertext);
Expand Down Expand Up @@ -490,8 +477,6 @@ public static BigInteger shiftRowsDec(BigInteger ciphertext) {

/**
* Applies the Rijndael MixColumns to the input and returns the result.
*
* @param ciphertext
*/
public static BigInteger mixColumns(BigInteger ciphertext) {

Expand All @@ -511,8 +496,6 @@ public static BigInteger mixColumns(BigInteger ciphertext) {

/**
* Applies the inverse Rijndael MixColumns for decryption to the input and returns the result.
*
* @param ciphertext
*/
public static BigInteger mixColumnsDec(BigInteger ciphertext) {

Expand Down Expand Up @@ -563,7 +546,6 @@ public static BigInteger encrypt(BigInteger plainText, BigInteger key) {
* Decrypts the ciphertext with the key and returns the result
*
* @param cipherText The Encrypted text which we want to decrypt
* @param key
* @return decryptedText
*/
public static BigInteger decrypt(BigInteger cipherText, BigInteger key) {
Expand Down Expand Up @@ -596,30 +578,27 @@ public static void main(String[] args) {
char choice = input.nextLine().charAt(0);
String in;
switch (choice) {
case 'E':
case 'e':
case 'E', 'e' -> {
System.out.println("Choose a plaintext block (128-Bit Integer in base 16):");
in = input.nextLine();
BigInteger plaintext = new BigInteger(in, 16);
System.out.println("Choose a Key (128-Bit Integer in base 16):");
in = input.nextLine();
BigInteger encryptionKey = new BigInteger(in, 16);
System.out.println(
"The encrypted message is: \n" + encrypt(plaintext, encryptionKey).toString(16));
break;
case 'D':
case 'd':
"The encrypted message is: \n" + encrypt(plaintext, encryptionKey).toString(16));
}
case 'D', 'd' -> {
System.out.println("Enter your ciphertext block (128-Bit Integer in base 16):");
in = input.nextLine();
BigInteger ciphertext = new BigInteger(in, 16);
System.out.println("Choose a Key (128-Bit Integer in base 16):");
in = input.nextLine();
BigInteger decryptionKey = new BigInteger(in, 16);
System.out.println(
"The deciphered message is:\n" + decrypt(ciphertext, decryptionKey).toString(16));
break;
default:
System.out.println("** End **");
"The deciphered message is:\n" + decrypt(ciphertext, decryptionKey).toString(16));
}
default -> System.out.println("** End **");
}
}
}
Expand Down
Loading