Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with the test book solution \"Introduction to computer security 1

ID: 657606 • Letter: P

Question

Please help me with the test book solution "Introduction to computer security 1st edition"

-- write a Java program for problem 1.5 (pp. 53)

Implement a toy symmetric cryptosystem based on the following method. Keys are 16-bit values. Messages are strings with an even number of characters. One can always add a blank at the end of an odd-length string. The encryption of a message M of length n (in bytes) is given by Ek(M) = where the key K is repeated n/2 times. The decryption algorithm for a ciphertext C is the same as the encryption algorithm: Dk(C) = C 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 test.

Explanation / Answer

package com.javaquery.security;
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 Symmetric {
private String AlphaNum = "A1B2C%D3EF)GH5IJ4K6L9MN7OP0QRS8TUVWXYZa!bc@de$fg#hij&k*l(m-n_op+q{r][st}u:v;w,x.?";
  
/**
* Encrypt the text using `SecretKey`
* @param originalText
* @param SecretKey
* @return String
*/
public String symmetricEncryption(String originalText, String SecretKey){   
byte[] raw;
String encryptedString = null;
BASE64Decoder decoder = new BASE64Decoder();
BASE64Encoder bASE64Encoder = new BASE64Encoder();
SecretKeySpec skeySpec;
Cipher cipher;

if (originalText != null && SecretKey != null) {
try {
byte[] encryptText = originalText.getBytes();
raw = decoder.decodeBuffer(SecretKey);
skeySpec = new SecretKeySpec(raw, "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;
}
  
/**
* Decrypt the encrypted text using same `SecretKey`
* @param Encryptedtext
* @param SecretKey
* @return String
*/
public String symmetricDecryption(String Encryptedtext, String SecretKey){
String decryptedString = null;
byte[] encryptText;
byte[] raw;
BASE64Decoder decoder = new BASE64Decoder();
BASE64Decoder base64Decoder = new BASE64Decoder();
Cipher cipher;
SecretKeySpec skeySpec;

if (Encryptedtext != null && SecretKey != null) {
try {
raw = decoder.decodeBuffer(SecretKey);
skeySpec = new SecretKeySpec(raw, "AES");
encryptText = base64Decoder.decodeBuffer(Encryptedtext);
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;
}
  
/**
* Generate random text/string of 200 characters
* @return String
*/
public String generateData() {
String Randomline = new String();
Random rand = new Random();
char tokenChar = '';
for (int i = 0; i < 200; i++) {
tokenChar = AlphaNum.charAt(rand.nextInt(AlphaNum.length()));
Randomline = Randomline + tokenChar;
}
return Randomline;
}
  
/**
* Generate Secret Key using javax.crypto.KeyGenerator class
* @return String
*/
public String getSecretKey() {
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;
}
  
public static void main(String[] args) {
Symmetric sTest = new Symmetric();
String SecretKey = sTest.getSecretKey();
String OriginalText = sTest.generateData();
  
/**
* You are adviced to check/validate null values before
* using this piece of code for real time application
*/
System.out.println("Secret Key:"+SecretKey);
System.out.println("Original Data:"+OriginalText);
  
String encrypt = sTest.symmetricEncryption(OriginalText,SecretKey);
System.out.println("Encrypted text:"+encrypt);
  
String decrypt = sTest.symmetricDecryption(encrypt,SecretKey);
System.out.println("Decrypted text:"+decrypt);
}
}