Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Resolves items 1, 2, 3, and 5. (Item 4 to be resolved next.)

...

Code Block
bgColor#FFCCCC
Cipher cipher = Cipher.getInstance("AES");  // defaults to         ECB mode
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may be unavailable

SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  // defaults to ECB mode
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

// Encode bytes as UTF8; strToBeEncrypted contains
// the input string that is to be encrypted 
byte[] encoded = strToBeEncrypted.getBytes("UTF8");
    
// Perform encryption
byte[] encrypted = cipher.doFinal(encoded);   

...

Code Block
bgColor#ccccff
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.SecureRandom;
import java.security.GeneralSecurityException;

class Msc61 {
    public static final int GCM_TAG_LENGTH = 1216;
    public static final int GCM_IV_LENGTH = 1612;

    public static SecretKey generateKey() throws GeneralSecurityException {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        return kgen.generateKey();
    }

    public static byte[] encrypt_gcm(SecretKey skey, String plaintext) throws GeneralSecurityException {
        byte[] ciphertext = null;
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");            
        byte[] initVector = new byte[GCM_IV_LENGTH];
        (new SecureRandom()).nextBytes(initVector);
        GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8java.lang.Byte.SIZE, initVector);
        cipher.init(Cipher.ENCRYPT_MODE, skey, spec);
        byte[] encoded = plaintext.getBytes(java.nio.charset.StandardCharsets.UTF_8);
        ciphertext = new byte[GCM_TAG_LENGTHinitVector.length + GCM_IV_LENGTH + cipher.getOutputSize(encoded.length)];
        for (int i=0; i < GCM_IV_LENGTHinitVector.length; i++) {
            ciphertext[i] = initVector[i];
        }
        // Perform encryption
        cipher.doFinal(encoded, 0, encoded.length, ciphertext, GCM_IV_LENGTHinitVector.length);
        return ciphertext;
    }

    public static String decrypt_gcm(SecretKey skey, byte[] ciphertext) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");            
        byte[] initVector = Arrays.copyOfRange(ciphertext, 0, GCM_IV_LENGTH);
        GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8java.lang.Byte.SIZE, initVector);
        cipher.init(Cipher.DECRYPT_MODE, skey, spec);
        byte[] plaintext = cipher.doFinal(ciphertext, GCM_IV_LENGTH, ciphertext.length - GCM_IV_LENGTH);
        return new String(plaintext);
    }
} 

...

Code Block
bgColor#ccccff
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.SecureRandom;
import java.security.GeneralSecurityException;

class Msc61 {
    public static SecretKey generateKey() throws GeneralSecurityException {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        return kgen.generateKey();
    }

    public static byte[] encrypt_cbc(SecretKey skey, String plaintext) throws GeneralSecurityException {
        byte[] ciphertext = null;
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");            
        final int blockSize = cipher.getBlockSize();
        byte[] initVector = new byte[blockSize];
        (new SecureRandom()).nextBytes(initVector);
        IvParameterSpec ivSpec = new IvParameterSpec(initVector);
        cipher.init(Cipher.ENCRYPT_MODE, skey, ivSpec);
        byte[] encoded = plaintext.getBytes(java.nio.charset.StandardCharsets.UTF_8);
        ciphertext = new byte[initVector.length + (cipher.getOutputSize(encoded.length / blockSize) + 1) * blockSize];
        for (int i=0; i < initVector.length; i++) {
            ciphertext[i] = initVector[i];
        }
        // Perform encryption
        cipher.doFinal(encoded, 0, encoded.length, ciphertext, initVector.length);
        return ciphertext;
    }

    public static String decrypt_cbc(SecretKey skey, byte[] ciphertext) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");            
        final int blockSize = cipher.getBlockSize();
        byte[] initVector = Arrays.copyOfRange(ciphertext, 0, blockSize);
        IvParameterSpec ivSpec = new IvParameterSpec(initVector);
        cipher.init(Cipher.DECRYPT_MODE, skey, ivSpec);
        byte[] plaintext = cipher.doFinal(ciphertext, blockSize, ciphertext.length - blockSize);
        return new String(plaintext);
    }
} 

...