AES IV Update

I currently have this class for encrypting/decrypting AES. How do I update the IV? I already have an updateIV() function but it doesn’t seem to update the REAL IVParameterSpec.

Thanks.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aescipher;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author Ido
 */
public class Encryption {
    public static enum Mode {
        ENCRYPT_MODE, DECRYPT_MODE;
    }
    
    public static final String CIPHER = "AES/CBC/PKCS5Padding";
    
    protected Cipher cipher;
    protected Mode mode;
    protected byte[] iv;
    protected IvParameterSpec ivSpec;
    protected byte[] key;
    
    /**
     * Creates this Encryptor.
     * @param cipherMode Mode to use
     * @param key 128 bit AES key to use
     * @param iv 128 bit [16 byte] AES iv to use
     */
    public Encryption(Mode cipherMode, byte[] key, byte[] iv) {
        try {
            this.key = key;
            cipher = Cipher.getInstance(CIPHER);
            ivSpec = new IvParameterSpec(iv);
            SecretKeySpec aesKey = new SecretKeySpec(key, "AES");
            this.mode = cipherMode;
            if (cipherMode == Mode.ENCRYPT_MODE) {
                cipher.init(Cipher.ENCRYPT_MODE, aesKey, ivSpec);
            } else {
                cipher.init(Cipher.DECRYPT_MODE, aesKey, ivSpec);
            }
        } catch (InvalidKeyException ex) {
          Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAlgorithmParameterException ex) {
          Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.iv = iv;
    }
    
    public byte[] crypt(byte[] in) {
        try {
          updateIV();
          System.out.print("New IV: ");
          printBytes(this.cipher.getIV());
          return cipher.doFinal(in);
        } catch (IllegalBlockSizeException ex) {
          Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
          Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public void printBytes(byte[] b) {
        System.out.print("(");
        for (int i = 0; i < b.length; i++) {
            System.out.print("0x" + Integer.toHexString(b[i]));
            if (i < b.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println(")");
    }
    
    public void updateIV() {
        for (int i = 0; i < this.iv.length; i++) {
            if (i < iv.length/2) { //16/2
                iv[i] ^= iv[i + 1];
                if (iv[i] > 127) {
                    iv[i] = 0x0A;
                }
                iv[i] ^= iv[i + 3];
                if (iv[i] > 127) {
                    iv[i] = 0x0D;
                }
            } else { //16/2
                iv[i] ^= iv[i - 3];
                if (iv[i] > 127) {
                    iv[i] = 0x0C;
                }
                iv[i] ^= iv[i - 2];
                if (iv[i] > 127) {
                    iv[i] = 0x1D;
                }                
            }
        }
        //cipher.update(iv, 0, iv.length);
    }
}

Never mind, I figured it out :smiley: