-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from MasterFlomaster1/dev
First pass on stream ciphers
- Loading branch information
Showing
10 changed files
with
514 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Create Release | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'v*.*.*' | ||
branches: | ||
- "master" | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
- name: Build with Maven | ||
run: mvn -B package --file pom.xml | ||
|
||
- name: Extract version | ||
run: echo "VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_ENV | ||
|
||
- name: Create GitHub Release | ||
uses: softprops/action-gh-release@v2 | ||
with: | ||
files: | | ||
target/simple-java-crypter-${{ env.VERSION }}.jar | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/dev/masterflomaster1/sjc/crypto/StreamCipherImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package dev.masterflomaster1.sjc.crypto; | ||
|
||
import javax.crypto.BadPaddingException; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.IllegalBlockSizeException; | ||
import javax.crypto.NoSuchPaddingException; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.security.InvalidAlgorithmParameterException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.NoSuchProviderException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public final class StreamCipherImpl { | ||
|
||
private StreamCipherImpl() { } | ||
|
||
public static byte[] encrypt(String algorithm, byte[] iv, byte[] inputData, byte[] key) { | ||
try { | ||
SecretKey secretKey = new SecretKeySpec(key, algorithm); | ||
Cipher cipher = Cipher.getInstance(algorithm, "BC"); | ||
|
||
if (getCorrespondingIvLengthBits(algorithm).isPresent()) | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); | ||
else | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | ||
|
||
return cipher.doFinal(inputData); | ||
} catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException | InvalidKeyException | | ||
IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static byte[] decrypt(String algorithm, byte[] iv, byte[] inputData, byte[] key) { | ||
try { | ||
SecretKey secretKey = new SecretKeySpec(key, algorithm); | ||
Cipher cipher = Cipher.getInstance(algorithm, "BC"); | ||
|
||
if (getCorrespondingIvLengthBits(algorithm).isPresent()) | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); | ||
else | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey); | ||
|
||
return cipher.doFinal(inputData); | ||
} catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException | InvalidKeyException | | ||
IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static List<Integer> getCorrespondingKeyLengths(String algorithm) { | ||
return switch (algorithm) { | ||
case "ARC4", "Grain128", "HC128", "ZUC-128", "VMPC", "VMPC-KSA3" -> List.of(128); | ||
case "CHACHA", "CHACHA7539", "CHACHA20-POLY1305", "HC256", "ZUC-256", "XSALSA20" -> List.of(256); | ||
case "SALSA20" -> List.of(128, 256); | ||
case "Grainv1" -> List.of(80); | ||
default -> throw new RuntimeException("Unsupported algorithm: " + algorithm); | ||
}; | ||
} | ||
|
||
public static Optional<List<Integer>> getCorrespondingIvLengthBits(String algorithm) { | ||
return switch (algorithm) { | ||
case "ARC4" -> Optional.empty(); | ||
case "CHACHA", "Grainv1", "SALSA20" -> Optional.of(List.of(64)); | ||
case "CHACHA7539", "CHACHA20-POLY1305", "Grain128" -> Optional.of(List.of(96)); | ||
case "HC128", "ZUC-128" -> Optional.of(List.of(128)); | ||
case "HC256" -> Optional.of(List.of(256)); | ||
case "XSALSA20" -> Optional.of(List.of(192)); | ||
case "ZUC-256" -> Optional.of(List.of(200)); | ||
case "VMPC", "VMPC-KSA3" -> Optional.of(List.of(8, 16, 32, 64, 128, 256, 512)); | ||
default -> throw new RuntimeException("Unsupported algorithm: " + algorithm); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.