I. Symmetric Cryptosystem In this programming Assignment, students are asked to
ID: 3757050 • Letter: I
Question
I. Symmetric Cryptosystem In this programming Assignment, students are asked to implement a toy symmetric cryptosystem based on the following method. a. Keys are 16-bit randomly generated values Messages are randomly generated strings with an even number of characters (Valid characters are upper and lower-case letters) The encryption of a message M of length n (in bytes) is given by b. c. EkCM) M (K). Where the key K is represented n/2 times and "ll means String Concatenation Operator. d. The decryption algorithm for a ciphertext C is the same as the encryption alyorihm: Students need to implement a brutal force decryption attack for this cryptosystem and test it on randomly generated English character message. Requirement: 1. You can use Javaprogramming language to finish this assignment and follow the given Structure. Your work is expected to be runnable. Any program has error or exception will receive 0 as grade 2.Explanation / Answer
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Demo
{
private String BFmsg =
"trnhflpctoq59627@#*%6"
+ "(p-q_lm+r{s][vs}v:u;y,z.!";
public String encryption(String key, String msg)
{
byte[] bruteFm;
String encryptedString = null;
BASE64Decoder decoder = new BASE64Decoder();
BASE64Encoder bASE64Encoder
= new BASE64Encoder();
SecretKeySpec skeySpec;
Cipher cipher;
if (key != null && msg != null)
{
try
{
byte[] encryptText = key.getBytes();
bruteFm = decoder.decodeBuffer(msg);
skeySpec = new SecretKeySpec(bruteFm, "AES");
cipher = Cipher.getInstance("AES/ECB"
+ "/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,
skeySpec);
encryptedString = bASE64Encoder.encode(cipher.doFinal
(encryptText));
} catch (Exception e) {
e.printStackTrace();
}
}
return encryptedString;
}
public String decryption(String Key, String msg)
{
String decryptedString = null;
byte[] encryptText;
byte[] bruteFm;
BASE64Decoder decoder = new BASE64Decoder();
BASE64Decoder base64Decoder = new BASE64Decoder();
Cipher cipher;
SecretKeySpec skeySpec;
if (Key != null && msg != null)
{
try
{
bruteFm = decoder.decodeBuffer(msg);
skeySpec = new SecretKeySpec(bruteFm, "AES");
encryptText = base64Decoder.decodeBuffer(Key);
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
decryptedString = new String(cipher.doFinal
(encryptText));
}
catch (Exception e)
{
e.printStackTrace();
}
}
return decryptedString;
}
public String generatekey()
{
String Randomline = new String();
Random rand = new Random();
char tokenChar = '';
for (int j = 0; j < 300; j++)
{
tokenChar = BFmsg.charAt(rand.nextInt
(BFmsg.length()));
Randomline = Randomline + tokenChar;
}
return Randomline;
}
public String generateMsg()
{
KeyGenerator keyGen;
String strSecretkey = null;
try
{
keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretkey = keyGen.generateKey();
BASE64Encoder encode = new BASE64Encoder();
strSecretkey = encode.encode
(secretkey.getEncoded());
}
catch (Exception e)
{
e.printStackTrace();
}
return strSecretkey;
}
private static void bruteForce(String msgE)
{
Demo Bruteforce = new Demo();
String SecretKey = Bruteforce.generateMsg();
String key = Bruteforce.generatekey();
System.out.println("Brute Key:"+SecretKey);
System.out.println("Bruteforce msg:"+key);
String encrypt = Bruteforce.encryption
(key,SecretKey);
System.out.println("Brute Bruce "
+ "Encrypted Meaasage :"
+encrypt);
String decrypt = Bruteforce.decryption(encrypt,
SecretKey);
System.out.println(" Brutef force"
+ " Decrypted text:"+decrypt);
}
public static void main(String[] args)
{
Demo sTest = new Demo();
String SecretKey = sTest.generateMsg();
String key = sTest.generatekey();
System.out.println("Secret Key:"+SecretKey);
System.out.println("Original Data:"+key);
String encrypt = sTest.encryption(key,SecretKey);
System.out.println("Encrypted text:"+encrypt);
String decrypt = sTest.decryption(encrypt,SecretKey);
System.out.println("Decrypted text:"+decrypt);
}
}