I'm trying to reproduce an old encryption/decryption done in Java to a new one in Ruby one because I'm rebuilding the whole app. All this to change this encryption asap, obviously.
Here is the Java code:
public class MyClass {
    private static String algo;
    private static SecretKey key;
    static {
        algo = "AES";
        String keyString = "someString";
        byte[] decodedKey = Base64.getDecoder().decode(keyString);
        key = new SecretKeySpec(decodedKey, 0, decodedKey.length, algo);
    }
    private static String decrypt(String encrypted) {
        try {
            Cipher cipher = Cipher.getInstance(algo);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decodedBytes = Base64.getDecoder().decode(encrypted.getBytes());
            byte[] original = cipher.doFinal(decodedBytes);
            return new String(original);
        }
        catch (Exception e) {
            // Some error
            return "bad";
        }
    }
    private static String encrypt(String toEncrypt) {
        try {
            Cipher cipher = Cipher.getInstance(algo);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
            byte[] encryptedValue = Base64.getEncoder().encode(encrypted);
            return new String(encryptedValue);
        }
        catch (Exception e) {
            // Some error
            return "bad";
        }
    }
}
Java code comes from here
I have a problem with the decryption. Here is my Ruby code:
key = Digest::SHA256.digest(key)
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.decrypt
aes.key = Digest::SHA256.digest(key)
aes.update(secretdata) + aes.final
# => OpenSSL::Cipher::CipherError: bad decrypt
What am I doing wrong?
 
     
    