Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String encryptWithManagedIV(String data) throws Exception {
- byte[] random = new byte[IV_LENGTH];
- new Random().nextBytes(random);
- return encrypt(data.getBytes(), random);
- }
- protected String encrypt(byte[] data, byte[] initialVector) throws Exception {
- Cipher c = Cipher.getInstance(AES_CIPHER);
- IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
- c.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
- byte[] encVal = c.doFinal(data);
- byte[] encValWithIv = new byte[encVal.length + IV_LENGTH];
- System.arraycopy(initialVector, 0, encValWithIv, 0, IV_LENGTH);
- System.arraycopy(encVal, 0, encValWithIv, IV_LENGTH, encVal.length);
- String encryptedValue = Base64.encodeBase64String(encValWithIv);
- return encryptedValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement