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

Illegal key size #1

Open
arivasvera opened this issue Aug 3, 2015 · 5 comments
Open

Illegal key size #1

arivasvera opened this issue Aug 3, 2015 · 5 comments

Comments

@arivasvera
Copy link

Exception in thread "main" java.lang.RuntimeException: java.security.InvalidKeyException: Illegal key size
    at AESUtil.encrypt(AESUtil.java:24)
    at AESMain.main(AESMain.java:10)
Caused by: java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)
    at AESUtil.encrypt(AESUtil.java:21)
    ... 1 more
@yutuoygy
Copy link

yutuoygy commented May 1, 2016

@andresrivas1506 Hi, I have the same issues as yours, have you fixed that?

@arivasvera
Copy link
Author

Hello @yutuoygy no, I haven't fixed that, sorry

@kounalem
Copy link

kounalem commented Aug 12, 2016

@arivasvera @yutuoygy Hi there.

You can use that fix:


private AlgorithmParameterSpec makeIv() {
        try {
            return new IvParameterSpec(sha256digest16(ENCRYPTION_IV));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }    
private byte[] sha256digest16(String ivString) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.reset();
        for(int i=0;i<ivString.length();i++)
        {
            digest.update((byte)ivString.charAt(i));
        }
        // so you have 32 bytes here
        byte[]  b = digest.digest();

        // you can return it directly or you can cut it to 16 bytes
        return Arrays.copyOf(b, 16);
    }

Implement that at the AESUtil.class, and obviously replace the makeIv.
You can put whatever value you wish to the ENCRYPTION_IV now.
We will get the first 16 bytes of the String always.

@yutuoygy , btw thanks for putting that code to open source. It helped me.

@julred
Copy link

julred commented Nov 5, 2017

@kounalem:

Your fix doesn't work for me.
I replaced the makeIv function, and made both function to public static.
But nevertheless, I get:

'Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.security.InvalidKeyException: Illegal key size'

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

5 participants
@arivasvera @julred @kounalem @yutuoygy and others