public static String encrypt(String cleartext, String password, int iterations) throws EncryptionException, UnsupportedEncodingException { return encryptWithSetMode(cleartext, password, iterations, MODE_CBC, new ISO10126d2Padding()); }
public static String decrypt(String ciphertext, String password, int iterations) throws UnsupportedEncodingException, InvalidCipherTextException, DecryptionException { return decryptWithSetMode(ciphertext, password, iterations, MODE_CBC, new ISO10126d2Padding()); }
/** * * @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); }
@Test public void decrypting_CBC_ISO10126_shouldPass() { try { String encrypted = AESUtil.encryptWithSetMode(cleartext, pw, iterations, AESUtil.MODE_CBC, new ISO10126d2Padding()); String decrypted = AESUtil.decryptWithSetMode(encrypted, pw, iterations, AESUtil.MODE_CBC, new ISO10126d2Padding()); Assert.assertTrue("Decryption failed", decrypted.equals(cleartext)); } catch (Exception e) { Assert.assertTrue("Encryption failed", false); } }
@Test public void decrypting_OFB_ISO10126_shouldPass() { try { String encrypted = AESUtil.encryptWithSetMode(cleartext, pw, iterations, AESUtil.MODE_OFB, new ISO10126d2Padding()); String decrypted = AESUtil.decryptWithSetMode(encrypted, pw, iterations, AESUtil.MODE_OFB, new ISO10126d2Padding()); Assert.assertTrue("Decryption failed", decrypted.equals(cleartext)); } catch (Exception e) { Assert.assertTrue("Encryption failed as expected.", false); } }
/** * * @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"); }
int modes[] = {AESUtil.MODE_CBC, AESUtil.MODE_OFB}; BlockCipherPadding[] paddings = { new ISO10126d2Padding(), new ISO7816d4Padding(), new ZeroBytePadding(),
cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO10126d2Padding());
cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO10126d2Padding());
cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO10126d2Padding());