assigment3
This commit is contained in:
parent
a616052ff5
commit
db2dbc5c19
@ -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;
|
||||
|
||||
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
|
||||
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()));
|
||||
}
|
||||
|
||||
public String encrypt(String data) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||
byte[] encryptedData = cipher.doFinal(data.getBytes());
|
||||
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()));
|
||||
}
|
||||
|
||||
public String decrypt(String cryptedData) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
||||
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(cryptedData));
|
||||
return new String(decryptedData);
|
||||
}
|
||||
}
|
||||
|
@ -19,4 +19,4 @@ public class Cryptabletest {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
@ -1,6 +1,7 @@
|
||||
public class HumanTest {
|
||||
public static void main(String[] args) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
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
|
||||
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()));
|
||||
}
|
||||
|
||||
public String encrypt(String data) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||
byte[] encryptedData = cipher.doFinal(data.getBytes());
|
||||
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()));
|
||||
}
|
||||
|
||||
public String decrypt(String cryptedData) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
||||
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(cryptedData));
|
||||
return new String(decryptedData);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user