assignment 3
This commit is contained in:
31
assignments/assignment3/Twofish.java
Normal file
31
assignments/assignment3/Twofish.java
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user