1. Brief description
SecretKeyFactory represents the factory of the private key.
Key factory is used to convert keys (opacity encryption keys of type Key) to key specifications (transparent representation of underlying key material)
vice versa. Secret key factory operates only on secret (symmetric) keys.
The key factory is in duplex mode, i.e. it allows the construction of an opaque key object according to a given key specification (key material), or obtain the underlying key material of the key object in an appropriate format.
Application developers should refer to their provider documentation to find out the key specifications supported by the generateSecret and getKeySpec methods. For example, the DES secret key factory provided by the "SunJCE" provider supports DESKeySpec as a transparent representation of the DES key, and the secret key factory of the Triple DES key for the provider supports DESedeKeySpec as a transparent representation of the Triple DES key.
2. Source code
2.1 Construction method
/** var1 delegation * var2 provider * var3 key algorithm */ protected SecretKeyFactory(SecretKeyFactorySpi var1, Provider var2, String var3) { = var1; = var2; = var3; } /** var1 key algorithm */ private SecretKeyFactory(String var1) throws NoSuchAlgorithmException { = var1; List var2 = ("SecretKeyFactory", var1); = (); if (((SecretKeyFactorySpi)null) == null) { throw new NoSuchAlgorithmException(var1 + " SecretKeyFactory not available"); } }
2.2 Detailed explanation of getInstance method
public static final SecretKeyFactory getInstance(String var0) throws NoSuchAlgorithmException { return new SecretKeyFactory(var0); }
1. Call the private SecretKeyFactory constructor to return the SecretKeyFactory object that converts the secret key of the specified algorithm.
2. This method starts with the preferred provider to traverse the registered security provider list. Returns a new SecretKeyFactory object encapsulated by SecretKeyFactorySpi implementation taken from the first Provider that supports the specified algorithm.
Note: You can obtain the registered provider list through the () method.
2.3 algorithm parameters
algorithm - The standard name of the requested secret key algorithm. For information on standard algorithm names, see Appendix A in the Java Cryptography Architecture Reference Guide
Throws: NoSuchAlgorithmException - If no Provider supports the SecretKeyFactorySpi implementation of the specified algorithm.
2.4 getInstance method two
/** var0 key algorithm * var1 provider */ public static final SecretKeyFactory getInstance(String var0, String var1) throws NoSuchAlgorithmException, NoSuchProviderException { Instance var2 = ("SecretKeyFactory", , var0, var1); return new SecretKeyFactory((SecretKeyFactorySpi), , var0); }
Returns the SecretKeyFactory object that converts the secret key of the specified algorithm.
Returns a new SecretKeyFactory object encapsulating the SecretKeyFactorySpi implementation, taken from the specified provider. The specified provider must be registered in the security provider list.
throw an exception
- NoSuchAlgorithmException - SecretKeyFactorySpi implementation of the specified algorithm cannot be obtained from the specified provider.
- NullPointerException - if the specified algorithm is null.
- NoSuchProviderException - If the specified provider is not registered in the security provider list.
- IllegalArgumentException - if provider is null or empty.
2.5 Detailed explanation of getInstance method three
/** var0 key algorithm * var1 provider */ public static final SecretKeyFactory getInstance(String var0, Provider var1) throws NoSuchAlgorithmException { Instance var2 = ("SecretKeyFactory", , var0, var1); return new SecretKeyFactory((SecretKeyFactorySpi), , var0); }
Returns a new SecretKeyFactory object encapsulating the SecretKeyFactorySpi implementation, taken from the specified Provider object. Note that specifying Provider objects does not need to be registered in the provider list.
throw an exception
- NullPointerException - if the specified algorithm is null.
- NoSuchAlgorithmException - If you cannot get the SecretKeyFactorySpi implementation of the specified algorithm from the specified Provider object.
- IllegalArgumentException - if provider is null.
2.6 getProvider method
Returns the provider of this SecretKeyFactory object.
public final Provider getProvider() { synchronized() { = null; return ; } }
2.7 getAlgorithm method
Returns the algorithm name of this SecretKeyFactory object. This name is the same as the name specified in a getInstance call that creates this SecretKeyFactory object.
public final String getAlgorithm() { return ; }
2.8 generateSecret method
Generate a SecretKey object based on the provided key specification (key material).
Return: Secret Key
Throws: InvalidKeySpecException - If the given key specification is not suitable for the secret key factory that generates the secret key.
/*keySpec - Specification for Secret Keys (key material) **/ public final SecretKey generateSecret(KeySpec var1) throws InvalidKeySpecException { if ( == null) { return (var1); } else { Exception var2 = null; SecretKeyFactorySpi var3 = ; while(true) { try { return (var1); } catch (Exception var5) { if (var2 == null) { var2 = var5; } var3 = (var3); if (var3 == null) { if (var2 instanceof InvalidKeySpecException) { throw (InvalidKeySpecException)var2; } throw new InvalidKeySpecException("Could not generate secret key", var2); } } } } }
2.9 getKeySpec method
Returns the specification (key material) of the given key object in the requested format.
Return: The underlying key specification (key material) of the requested format
Throws: InvalidKeySpecException - if the requested key specification does not fit the given key (e.g., the algorithm associated with key and keySpec does not match, or the key refers to a key on the encryption hardware device and keySpec is based on the software key specification), or the given key cannot be processed (e.g., the given key has an algorithm or format that this secret key factory does not support).
/*key - key *keySpec - The requested format, the key material will be returned in this format */ public final KeySpec getKeySpec(SecretKey var1, Class<?> var2) throws InvalidKeySpecException { if ( == null) { return (var1, var2); } else { Exception var3 = null; SecretKeyFactorySpi var4 = ; while(true) { try { return (var1, var2); } catch (Exception var6) { if (var3 == null) { var3 = var6; } var4 = (var4); if (var4 == null) { if (var3 instanceof InvalidKeySpecException) { throw (InvalidKeySpecException)var3; } throw new InvalidKeySpecException("Could not get key spec", var3); } } } } }
2.10 translateKey method
Convert a key object whose provider is unknown or may not be trusted to the corresponding key object of this secret key factory.
Return: Converted key
Throws: InvalidKeyException - if this secret key factory cannot handle the given key.
/*key - a key whose provider is unknown or untrusted */ public final SecretKey translateKey(SecretKey var1) throws InvalidKeyException { if ( == null) { return (var1); } else { Exception var2 = null; SecretKeyFactorySpi var3 = ; while(true) { try { return (var1); } catch (Exception var5) { if (var2 == null) { var2 = var5; } var3 = (var3); if (var3 == null) { if (var2 instanceof InvalidKeyException) { throw (InvalidKeyException)var2; } throw new InvalidKeyException("Could not translate key", var2); } } } } }
For example, the following example
try { // Get the key factory object SecretKeyFactory keyFactory = ("DES"); byte[] DESkey = "abcdefghijk".getBytes("UTF-8");// Set the key DESKeySpec keySpec = new DESKeySpec(DESkey);// Set key parameters Key key = (keySpec);// Get the key object } catch (Exception e) { (); }
This is the end of this article about the detailed explanation of the SecretKeyFactory class in Java. For more detailed explanation of the SecretKeyFactory class, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!