1 year ago

#67305

test-img

fabelmk1

Decrypting string with java/kotlin as is done with openssl enc

im trying to make an android app with inspiration from a bash script program, but im stuck here:

echo MTQ4MjY5 | openssl enc -aes256  -K "3235373436353338353932393338333936373634363632383739383333323838" -iv "34323036393133333738303038313335" -a

I need to find a way to encrypt MTQ4MjY5 in java/kotlin as they do with openssl, but im unable to do so.

I have tried:

UPDATE: Still returns wrong encrypted string, but getting closer. Any ideas why?

 public static byte[]  encrypt(byte[] plainText) throws Exception{

        byte[] key= DatatypeConverter.parseHexBinary("3235373436353338353932393338333936373634363632383739383333323838");
        byte[] iv= DatatypeConverter.parseHexBinary("34323036393133333738303038313335");
        SecretKeySpec secretKey=new SecretKeySpec(key,ALGORITHM);
        IvParameterSpec ivParameterSpec=new IvParameterSpec(iv);
        Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE,secretKey,ivParameterSpec);
        return cipher.doFinal(plainText);
    }

Called like this:

val result = DatatypeConverter.printBase64Binary(Encrypt.encrypt("MTQ4MjY5".toByteArray()))

Wrong output: PNKLL87lXkHjjYU7yMtObA==

I have no idea how to solve this, help much appreciated

The correct encrypted string is: juF+fDAsvFnKIfaXUzMoHQ==

java

kotlin

aes

0 Answers

Your Answer

Accepted video resources