Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No conversion of Base64 to Hex #10

Closed
nowlkaszick opened this issue Jan 4, 2018 · 3 comments
Closed

No conversion of Base64 to Hex #10

nowlkaszick opened this issue Jan 4, 2018 · 3 comments

Comments

@nowlkaszick
Copy link
Contributor

nowlkaszick commented Jan 4, 2018

The React encrypt method parameters are requesting a base64 encoded key and IV, however the private encrypt method requires hexadecimal key and IV values - however there is no explicit conversion that is occurring.

React method

@ReactMethod
    public void encrypt(String data, **String keyBase64, String ivBase64**, Promise promise) {
        try {
            // calls private method, that expects hex, with no conversion
            String result = encrypt(data, **keyBase64, ivBase64**);
            promise.resolve(result);
        } catch (Exception e) {
            promise.reject("-1", e.getMessage());
        }
    }

Private method

private static String encrypt(String text, **String hexKey, String hexIv**) throws Exception {
        if (text == null || text.length() == 0) {
            return null;
        }

        byte[] key = Hex.decode(hexKey);
        SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);

        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, hexIv == null ? emptyIvSpec : new IvParameterSpec(Hex.decode(hexIv)));
        byte[] encrypted = cipher.doFinal(text.getBytes("UTF-8"));
        return Base64.encodeToString(encrypted, Base64.NO_WRAP);
    }

Passing through base64 values (to the React method) throws an exception that states that hexadecimal values are required. Passing through hexadecimal values (to the React method) works successfully.

I propose that either:

  • The example in the readme, and the parameter names are updated to reflect that these values should be hexadecimal
  • An explicit conversion is added to the react method
  • The parameters in the private method are updated to accept base64, and the conversion is added to the private method (this may impact other code that relies on the private encrypt method)
@hardcodet
Copy link

I haven't tried it out yet on iOS, but is it possible that iOS expects base64 and Android a Hex string? The iOS sources seems to point towards base64, while Android mentions Hex (and fails with base64, as @nowlkaszick already mentioned).

Also, I'm looping over a file and feed 64KB chunks with the same key/iv into Aes.encrypt, and the results are super weird: The lengths of the returned cyphers vary with every chunk, and they are super short. I end up with a total of around 13 KB worth of encrypted text for a 4 MB file.

@tectiv3 - I saw that you mentioned a few times that you don't have an Android phone. Has this library been tested on an emulator so far? Or do we have to assume that Android isn't production ready so far?

Thanks!

@tectiv3
Copy link
Owner

tectiv3 commented Feb 1, 2018

@hardcodet yes, android is definitely isn't production ready!

@chernandezq
Copy link

Hi, any solution, I tried in android, but dont work, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants