Question

I'd like to write a Java method that mandates the use of AES-256. In order to do that I'd like to perform a check on the incoming SecretKey instance. In the case of RSA this is what I'm doing :-

public boolean checkKey(RSAKey key) {
    if ( key.getModulus().bitLength() == 1024 )
        return true;
    return false;
}

In the case of AES would the following do?

public boolean checkKey(SecretKey key) {
    if ( key.getAlgorithm() == "AES" && key.getEncoded().length == 256 )
        return true;
    return false;
}

But I get the feeling that this would be the wrong way to do it as I would be finding the length of the encoded key and not the key. How could I go about finding the length of my AES key?

Was it helpful?

Solution

For AES keys, the encoded form is just the raw bytes, so your check is fine in principle.

Just remember that the result will be a byte length, not a bit length, so check for == 32.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top