Java Crypto: AES not available?

Discussion in 'Mac Programming' started by wrldwzrd89, Sep 25, 2011.

  1. wrldwzrd89, Sep 25, 2011
    Last edited: Sep 25, 2011

    wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #1
    Code:
    public class ByteDumper {
    
    import javax.crypto.SecretKeyFactory;
    
             public static void main(String[] args) {
    		try {
    			SecretKeyFactory skf = SecretKeyFactory.getInstance("AES");
    			System.out.println(skf.toString());
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    This is the output it produces on my Mac OS X 10.7.1 system with the latest Java 6:
    Code:
    java.security.NoSuchAlgorithmException: AES SecretKeyFactory not available
    	at javax.crypto.SecretKeyFactory.<init>(DashoA13*..)
    	at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)
    	at com.puttysoftware.bytedumper.ByteDumper.main(ByteDumper.java:6)
    
    Huh. Strange. Why doesn't that work?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  4. wrldwzrd89 thread starter macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #4
    Thanks, I was looking at that earlier... only problem is, the KeyGenerator approach won't work as I've already generated a suitable key to use for the encryption / decryption process.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Exactly what is the problem you want to solve that needs a SecretKeyFactory?
     
  6. wrldwzrd89 thread starter macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #6
    As far as I can tell, the only way to get a SecretKey, which is in turn needed for a Cipher, from an array of bytes that is known to me, is to use SecretKeyFactory to create a SecretKeySpec, then use that to get the needed SecretKey. Is there something I am overlooking?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    The example Lee linked to shows this code:
    Code:
           KeyGenerator kgen = KeyGenerator.getInstance("AES");
           kgen.init(128); // 192 and 256 bits may not be available
    
           // Generate the secret key specs.
           SecretKey skey = kgen.generateKey();
           byte[] raw = skey.getEncoded();
    
           SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    
           // Instantiate the cipher
           Cipher cipher = Cipher.getInstance("AES");
    
           cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    
    Note the absence of SecretKeyFactory.


    I also googled: java aes cipher example

    One of the top hits:
    http://stackoverflow.com/questions/992019/java-256bit-aes-encryption

    Again, note the absence of SecretKeyFactory (in the first post).

    A later post uses SecretKeyFactory, but of type "PBKDF2WithHmacSHA1".


    So it doesn't seem difficult to find examples that use AES ciphers, yet don't require an AES SecretKeyFactory.

    In fact, given raw key bytes in byte[], the SecretKeySpec class seems to do what you want. Check the class reference docs, and look for examples.
     
  8. wrldwzrd89 thread starter macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #8
    Thanks. Figures it's something non-obvious. :)
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Java Crypto is like that. Get used to it.
     

Share This Page