· 7 years ago · Jun 22, 2018, 09:36 AM
1$cipher = "rijndael-128";
2$mode = "cbc";
3
4Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
5
6SecretKey secret = new SecretKeySpec(key, "AES");
7Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
8cipher.init(Cipher.ENCRYPT_MODE, secret);
9AlgorithmParameters param = cipher.getParameters();
10/* In addition to ciphertext in "cos", recipient needs IV. */
11byte[] iv = param.getParameterSpec(IvParameterSpec.class).getIV();
12CipherOutputStream cos = new CipherOutputStream(output, cipher);
13byte[] buf = new byte[2048];
14while (true) {
15 int n = input.read(buf, 0, buf.length);
16 if (n < 0)
17 break;
18 cos.write(buf, 0, n);
19}
20cos.flush();