简体   繁体   中英

Android: Define which encryption to use: AES 128 or AES 256

In an android project I am using

import javax.crypto.Cipher;

ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 

I would like to ask: Whether AES128 or AES256 is used, is defined by the key that is used? For example key="012345678901234567890123456789012"; would have as a result AES256 to be used?

thanks
Thomas

Yes whether AES128 or AES256 is used is defined by the key that is in use.

However it's not the String length but the byte[] length that determines it. At some point in your code you should be converting String to byte[] . The size of resulting that byte[] is your key size. Without knowing how you convert "012345678901234567890123456789012" , it's not possible to know your encryption strength.

Alternatively you can use KeyGenerator :

    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128); // or 256

    // Generate the secret key specs
    SecretKey secretKey = keyGen.generateKey();
    byte[] byteArray = secretKey.getEncoded();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM