assignment 3

This commit is contained in:
k0rrluna 2024-12-23 03:12:44 +03:00
parent 16ffb9844f
commit a616052ff5
14 changed files with 192 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,31 @@
import java.util.Base64;
public class Blowfish implements Cryptable {
private static final String SECRET_KEY = "MySecretKey"; // Simple key for XOR
@Override
public String encrypt(String data) {
byte[] dataBytes = data.getBytes();
byte[] encryptedData = new byte[dataBytes.length];
// XOR encryption
for (int i = 0; i < dataBytes.length; i++) {
encryptedData[i] = (byte) (dataBytes[i] ^ SECRET_KEY.charAt(i % SECRET_KEY.length()));
}
return Base64.getEncoder().encodeToString(encryptedData);
}
@Override
public String decrypt(String cryptedData) {
byte[] encryptedData = Base64.getDecoder().decode(cryptedData);
byte[] decryptedData = new byte[encryptedData.length];
// XOR decryption (same as encryption)
for (int i = 0; i < encryptedData.length; i++) {
decryptedData[i] = (byte) (encryptedData[i] ^ SECRET_KEY.charAt(i % SECRET_KEY.length()));
}
return new String(decryptedData);
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,4 @@
public interface Cryptable {
String encrypt(String data) throws Exception;
String decrypt(String cryptedData) throws Exception;
}

Binary file not shown.

View File

@ -0,0 +1,22 @@
public class Cryptabletest {
public static void main(String[] args) {
try {
// Blowfish Example
Cryptable blowfish = new Blowfish();
String blowfishEncrypted = blowfish.encrypt("Hello, Blowfish!");
System.out.println("Blowfish Encrypted: " + blowfishEncrypted);
String blowfishDecrypted = blowfish.decrypt(blowfishEncrypted);
System.out.println("Blowfish Decrypted: " + blowfishDecrypted);
// Twofish Example
Cryptable twofish = new Twofish();
String twofishEncrypted = twofish.encrypt("Hello, Twofish!");
System.out.println("Twofish Encrypted: " + twofishEncrypted);
String twofishDecrypted = twofish.decrypt(twofishEncrypted);
System.out.println("Twofish Decrypted: " + twofishDecrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,76 @@
// Define the NumberSorter interface
interface NumberSorter {
void sortNumbers(int[] array);
}
// BubbleSort class implementing the NumberSorter interface
class BubbleSort implements NumberSorter {
@Override
public void sortNumbers(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] < array[j + 1]) {
// Swap array[j] and array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
// MergeSort class implementing the NumberSorter interface
class MergeSort implements NumberSorter {
@Override
public void sortNumbers(int[] array) {
mergeSort(array, 0, array.length - 1);
}
// Helper function for merge sort
private void mergeSort(int[] array, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(array, left, mid); // Sort the left half
mergeSort(array, mid + 1, right); // Sort the right half
merge(array, left, mid, right); // Merge the sorted halves
}
}
// Merging two halves of the array in descending order
private void merge(int[] array, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] leftArray = new int[n1];
int[] rightArray = new int[n2];
System.arraycopy(array, left, leftArray, 0, n1);
System.arraycopy(array, mid + 1, rightArray, 0, n2);
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArray[i] > rightArray[j]) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1) {
array[k] = leftArray[i];
i++;
k++;
}
while (j < n2) {
array[k] = rightArray[j];
j++;
k++;
}
}
}

Binary file not shown.

View File

@ -0,0 +1,28 @@
public class SorterTest {
public static void main(String[] args) {
int[] array1 = {5, 3, 8, 6, 2, 7, 4};
int[] array2 = {9, 1, 4, 5, 3, 7, 8};
// Create objects of BubbleSort and MergeSort
NumberSorter bubbleSort = new BubbleSort();
NumberSorter mergeSort = new MergeSort();
// Sort using BubbleSort
System.out.println("Bubble Sort:");
bubbleSort.sortNumbers(array1);
printArray(array1);
// Sort using MergeSort
System.out.println("\nMerge Sort:");
mergeSort.sortNumbers(array2);
printArray(array2);
}
// Utility function to print the array
private static void printArray(int[] array) {
for (int num : array) {
System.out.print(num + " ");
}
System.out.println();
}
}

Binary file not shown.

View File

@ -0,0 +1,31 @@
import java.util.Base64;
public class Twofish implements Cryptable {
private static final String SECRET_KEY = "AnotherSecretKey"; // Simple key for XOR
@Override
public String encrypt(String data) {
byte[] dataBytes = data.getBytes();
byte[] encryptedData = new byte[dataBytes.length];
// XOR encryption
for (int i = 0; i < dataBytes.length; i++) {
encryptedData[i] = (byte) (dataBytes[i] ^ SECRET_KEY.charAt(i % SECRET_KEY.length()));
}
return Base64.getEncoder().encodeToString(encryptedData);
}
@Override
public String decrypt(String cryptedData) {
byte[] encryptedData = Base64.getDecoder().decode(cryptedData);
byte[] decryptedData = new byte[encryptedData.length];
// XOR decryption (same as encryption)
for (int i = 0; i < encryptedData.length; i++) {
decryptedData[i] = (byte) (encryptedData[i] ^ SECRET_KEY.charAt(i % SECRET_KEY.length()));
}
return new String(decryptedData);
}
}