BufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), padding); cipher.reset(); cipher.init(false, params);
/** * initialise the cipher. * * @param forEncryption if true the cipher is initialised for * encryption, if false for decryption. * @param params the key and other data required by the cipher. * @exception IllegalArgumentException if the params argument is * inappropriate. */ public void init( boolean forEncryption, CipherParameters params) throws IllegalArgumentException { this.forEncryption = forEncryption; reset(); cipher.init(forEncryption, params); }
/** * initialise the cipher. * * @param forEncryption if true the cipher is initialised for * encryption, if false for decryption. * @param params the key and other data required by the cipher. * @exception IllegalArgumentException if the params argument is * inappropriate. */ public void init( boolean forEncryption, CipherParameters params) throws IllegalArgumentException { this.forEncryption = forEncryption; reset(); cipher.init(forEncryption, params); }
/** * * @param key AES key (256 bit Buffer) * @param data e.g. "{'aaa':'bbb'}" * @return * @throws InvalidCipherTextException */ public static byte[] encryptWithKey(byte[] key, String data) throws InvalidCipherTextException, UnsupportedEncodingException { byte[] iv = getSalt(); byte[] dataBytes = data.getBytes("utf-8"); KeyParameter keyParam = new KeyParameter(key); CipherParameters params = new ParametersWithIV(keyParam, iv); BlockCipher cipherMode = new CBCBlockCipher(new AESFastEngine()); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cipherMode, new ISO10126d2Padding()); cipher.reset(); cipher.init(true, params); byte[] outBuf = cipherData(cipher, dataBytes); // Concatenate iv int len1 = iv.length; int len2 = outBuf.length; byte[] ivAppended = new byte[len1 + len2]; System.arraycopy(iv, 0, ivAppended, 0, len1); System.arraycopy(outBuf, 0, ivAppended, len1, len2); return Base64.encodeBase64(ivAppended); }
cipher.reset(); cipher.init(true, params);
/** * * @param key AES key (256 bit Buffer) * @param ciphertext Base64 encoded concatenated payload + iv * @return * @throws InvalidCipherTextException * @throws UnsupportedEncodingException */ public static String decryptWithKey(byte[] key, String ciphertext) throws InvalidCipherTextException, UnsupportedEncodingException { byte[] dataBytesB64 = Base64.decodeBase64(ciphertext.getBytes("utf-8")); //Separate the IV and cipher data byte[] iv = copyOfRange(dataBytesB64, 0, AESBlockSize * 4); byte[] dataBytes = copyOfRange(dataBytesB64, AESBlockSize * 4, dataBytesB64.length); KeyParameter keyParam = new KeyParameter(key); CipherParameters params = new ParametersWithIV(keyParam, iv); BlockCipher cipherMode = new CBCBlockCipher(new AESFastEngine()); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cipherMode, new ISO10126d2Padding()); cipher.reset(); cipher.init(false, params); //Create a temporary buffer to decode into (includes padding) byte[] buf = new byte[cipher.getOutputSize(dataBytes.length)]; int len = cipher.processBytes(dataBytes, 0, dataBytes.length, buf, 0); len += cipher.doFinal(buf, len); //Remove padding byte[] out = new byte[len]; System.arraycopy(buf, 0, out, 0, len); return new String(out, "UTF-8"); }
cipher.reset(); cipher.init(false, params);