I have below code in java, which encrypting input data using AES
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256); SecretKey secret = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); if (data == null) return encryptedText; Cipher encryptCipher = Cipher.getInstance("AES/GCM/NoPadding"); // get IV byte[] iv = getRandomNonce(12); encryptCipher.init(Cipher.ENCRYPT_MODE, secret, new GCMParameterSpec(128, iv)); // encrypted data: byte[] encryptedBytes = encryptCipher.doFinal(data.getBytes("UTF-8")); // prefix IV and Salt to cipher text byte[] cipherTextWithIvSalt = ByteBuffer.allocate(iv.length + salt.length() + encryptedBytes.length).put(iv) .put(salt.getBytes()).put(encryptedBytes).array(); encryptedText = Base64.getEncoder().encodeToString(cipherTextWithIvSalt);
I have logstash pipeline where for encryption i'm trying with below code in ruby, however it is not working
ruby {code => " require 'openssl' require 'base64' password = 'secret_key' salt = 'random_salt' iterations = 65536 key_len = OpenSSL::Cipher.new('aes-256-gcm').key_len key = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_len, 'sha256') cipher = OpenSSL::Cipher.new('aes-256-gcm') cipher.encrypt cipher.key = key iv = Random.new.bytes(12) encrypted = cipher.update(event.get('secret')) + cipher.final length=iv.length+salt.length+encrypted.length // buffer =IO::Buffer.new(10)// not working encrypted_text = Base64.encode64(buffer).strip event.set('message_encrypted', encrypted_text)"add_field => {"new_secret" => "%{[message_encrypted]}" }
}
Can someone help to figure out the missing part in the code?