Skip to content

Commit

Permalink
Resolve build errors & cleanup structure (TheAlgorithms#2334)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriak authored Sep 26, 2021
1 parent 3559002 commit dfe189b
Show file tree
Hide file tree
Showing 48 changed files with 418 additions and 1,102 deletions.
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.

16 changes: 4 additions & 12 deletions .github/workflows/prettier.yml → .github/workflows/prettify.yml
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/[email protected]
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

0 comments on commit dfe189b

Please sign in to comment.