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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top