assigment3

This commit is contained in:
k0rrluna 2024-12-24 04:22:54 +03:00
parent a616052ff5
commit db2dbc5c19
5 changed files with 45 additions and 40 deletions

View File

@ -1,31 +1,33 @@
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64; import java.util.Base64;
public class Blowfish implements Cryptable { public class Blowfish implements Cryptable {
private static final String SECRET_KEY = "MySecretKey"; // Simple key for XOR private static final String ALGORITHM = "Blowfish";
private SecretKey secretKey;
public Blowfish() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(128); // Blowfish key length is 128 bits
this.secretKey = keyGen.generateKey();
}
@Override @Override
public String encrypt(String data) { public String encrypt(String data) throws Exception {
byte[] dataBytes = data.getBytes(); Cipher cipher = Cipher.getInstance(ALGORITHM);
byte[] encryptedData = new byte[dataBytes.length]; cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
// 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); return Base64.getEncoder().encodeToString(encryptedData);
} }
@Override @Override
public String decrypt(String cryptedData) { public String decrypt(String cryptedData) throws Exception {
byte[] encryptedData = Base64.getDecoder().decode(cryptedData); Cipher cipher = Cipher.getInstance(ALGORITHM);
byte[] decryptedData = new byte[encryptedData.length]; cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(cryptedData));
// 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); return new String(decryptedData);
} }
} }

View File

@ -19,4 +19,4 @@ public class Cryptabletest {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@ -1,6 +1,7 @@
public class HumanTest { public class HumanTest {
public static void main(String[] args) { public static void main(String[] args) {
Worker worker = new Worker("Koray", "Altinsoy", "Embedded Systems Engineer"); Worker worker = new Worker("Koray", "Altinsoy", "Embedded Systems Engineer");
System.out.println("First name: "+worker.getFName()+", Last Name: "+worker.getLName()+", Job Title:"+worker.getJobTitle()); System.out.println("First name: "+worker.getFName()+", Last Name: "+
worker.getLName()+", Job Title:"+worker.getJobTitle());
} }
} }

View File

@ -1,31 +1,33 @@
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64; import java.util.Base64;
public class Twofish implements Cryptable { public class Twofish implements Cryptable {
private static final String SECRET_KEY = "AnotherSecretKey"; // Simple key for XOR private static final String ALGORITHM = "Twofish";
private SecretKey secretKey;
public Twofish() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(256); // Twofish key length can be 128, 192, or 256 bits
this.secretKey = keyGen.generateKey();
}
@Override @Override
public String encrypt(String data) { public String encrypt(String data) throws Exception {
byte[] dataBytes = data.getBytes(); Cipher cipher = Cipher.getInstance(ALGORITHM);
byte[] encryptedData = new byte[dataBytes.length]; cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
// 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); return Base64.getEncoder().encodeToString(encryptedData);
} }
@Override @Override
public String decrypt(String cryptedData) { public String decrypt(String cryptedData) throws Exception {
byte[] encryptedData = Base64.getDecoder().decode(cryptedData); Cipher cipher = Cipher.getInstance(ALGORITHM);
byte[] decryptedData = new byte[encryptedData.length]; cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(cryptedData));
// 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); return new String(decryptedData);
} }
} }