Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
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?
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,771
8,477
A sea of green
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.

Exactly what is the problem you want to solve that needs a SecretKeyFactory?
 

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
Exactly what is the problem you want to solve that needs a SecretKeyFactory?
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,771
8,477
A sea of green
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.
 

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
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.
Thanks. Figures it's something non-obvious. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.