Implement a toy symmetric cryptosystem based on the following method. a. Keys ar
ID: 3877793 • Letter: I
Question
Implement a toy symmetric cryptosystem based on the following method.
a. Keys are 16-bit randomly generated values.
b. Messages are randomly generated strings with an even number of characters. (Valid characters are upper and lower case letters, as well as spaces.) One can always add a blank at the end of an odd-length string.
c. The encryption of a message M of length n (in bytes) is given by where the key K is repeated n/2 times.
d. The decryption algorithm for a ciphertext C is the same as the encryption algorithm: Implement a brute-force decryption attack for this cryptosystem and test it on randomly generated English text messages. Automate the process of detecting whether a decrypted message is English text.
Instruction: 1. You should use Java programming language to do this assignment.
Please Use Below Structure to do the program
/**
* This is a structure for class CSc4222 Assignment1
*/
public class Assignment1Demo {
private static String msg;
private static String msgE;
private static String msgD;
private static int key;
public static void main(String[] args){
//TODO: You can only call methods in main method
key = generateKey();
msg = generateMsg();
msgE = encryption(key,msg);
bruteForce(msgE);
}
private static int generateKey() {
//TODO: implement step a (randomly generate 16-bit key)
return 0;
}
private static String generateMsg() {
//TODO: implement step b (randonly generate a string with an even number of characters)
return "";
}
private static String encryption(int key, String msg) {
//TODO: implement step c (encrypt the message)
return "";
}
private static void decryption(int key, String msgE) {
//TODO: implement step d (decryption)
}
private static void bruteForce(String msgE) {
//TODO: implement bruteForce algorithm, you may need the above decryption(key,msgE) method
}
}
Explanation / Answer
package com.chegg;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
public class Assignment1Demo {
protected KEMCipher kemCipher;
protected AlgorithmParameterSpec iv;
protected AsymmetricCipherKeyPair keyPair;
public Assignment1Demo() throws GeneralSecurityException {
this.kemCipher = new KEMCipher(
Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"),
new GGHSW13KEMEngine()
);
// build the initialization vector. This example is all zeros, but it
// could be any value or generated using a random number generator.
iv = new IvParameterSpec(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
}
public AsymmetricCipherKeyPair setup(int n) {
GGHSW13KeyPairGenerator setup = new GGHSW13KeyPairGenerator();
setup.init(new GGHSW13KeyPairGenerationParameters(
new SecureRandom(),
new GGHSW13ParametersGenerator().init(
PairingFactory.getPairing("params/mm/ctl13/toy.properties"),
n).generateParameters()
));
return (keyPair = setup.generateKeyPair());
}
public byte[] initEncryption(String assignment) {
try {
return kemCipher.init(
true,
new KEMCipherEncryptionParameters(
128,
new GGHSW13EncryptionParameters(
(GGHSW13PublicKeyParameters) keyPair.getPublic(),
assignment
)
),
iv
);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public byte[] encrypt(String message) {
try {
return kemCipher.doFinal(message.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public CipherParameters keyGen(Circuit circuit) {
GGHSW13SecretKeyGenerator keyGen = new GGHSW13SecretKeyGenerator();
keyGen.init(new GGHSW13SecretKeyGenerationParameters(
(GGHSW13PublicKeyParameters) keyPair.getPublic(),
(GGHSW13MasterSecretKeyParameters) keyPair.getPrivate(),
circuit
));
return keyGen.generateKey();
}
public byte[] decrypt(CipherParameters secretKey, byte[] encapsulation, byte[] ciphertext) {
try {
kemCipher.init(
false,
new KEMCipherDecryptionParameters(secretKey, encapsulation, 128),
iv
);
return kemCipher.doFinal(ciphertext);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
Security.addProvider(new BouncyCastleProvider());
try {
// Setup
int n = 4;
Assignment1Demo engine = new Assignment1Demo();
engine.setup(n);
// Encrypt
String message = "Hello World!!!";
byte[] encapsulation = engine.initEncryption("1101");
byte[] ciphertext = engine.encrypt(message);
// Decrypt
int q = 3;
Circuit circuit = new DefaultCircuit(n, q, 3, new DefaultCircuit.DefaultGate[]{
new DefaultCircuit.DefaultGate(INPUT, 0, 1),
new DefaultCircuit.DefaultGate(INPUT, 1, 1),
new DefaultCircuit.DefaultGate(INPUT, 2, 1),
new DefaultCircuit.DefaultGate(INPUT, 3, 1),
new DefaultCircuit.DefaultGate(AND, 4, 2, new int[]{0, 1}),
new DefaultCircuit.DefaultGate(OR, 5, 2, new int[]{2, 3}),
new DefaultCircuit.DefaultGate(AND, 6, 3, new int[]{4, 5}),
});
byte[] plaintext = engine.decrypt(engine.keyGen(circuit), encapsulation, ciphertext);
assertEquals(true, message.equals(new String(plaintext)));
} catch (Exception e) {
e.printStackTrace();
} finally {
ExecutorServiceUtils.shutdown();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.