Package org.apache.fulcrum.jce.crypto
Class CryptoStreamFactoryImpl
- java.lang.Object
-
- org.apache.fulcrum.jce.crypto.CryptoStreamFactoryTemplate
-
- org.apache.fulcrum.jce.crypto.CryptoStreamFactoryImpl
-
- All Implemented Interfaces:
CryptoStreamFactory
public class CryptoStreamFactoryImpl extends CryptoStreamFactoryTemplate implements CryptoStreamFactory
Concrete factory for creating encrypting/decrypting streams. The implementation uses the JCA (Java Crypto Extension) supplied by SUN (using SunJCE 1.42). The implementation uses as PBEWithMD5AndDES for encryption which should be sufficent for most applications. The implementation also supplies a default password in the case that the programmer don't want to have additional hassles. It is easy to reengineer the password being used but much better than a hard-coded password in the application. The code uses parts from Markus Hahn's Blowfish library found at http://blowfishj.sourceforge.net/- Author:
- Siegfried Goeschl , Markus Hahn
-
-
Field Summary
Fields Modifier and Type Field Description protected String
algorithm
the algorithm to useprotected int
count
the count paramter for the PBE algorithmprotected String
providerName
the name of the JCE providerprotected static String
PROVIDERNAME
The JCE provider name known to work.protected byte[]
salt
the salt for the PBE algorithm
-
Constructor Summary
Constructors Constructor Description CryptoStreamFactoryImpl()
ConstructorCryptoStreamFactoryImpl(byte[] salt, int count)
Constructor
-
Method Summary
Modifier and Type Method Description protected Cipher
createCipher(int mode, char[] password)
Create a Cipher.protected Key
createKey(char[] password)
Create a PBE key.String
getAlgorithm()
Info about used algorithm.protected int
getCount()
static CryptoStreamFactory
getInstance()
Factory method to get a default instancestatic CryptoStreamFactory
getInstance(byte[] salt, int count)
Factory method to get a default instanceprotected String
getProviderName()
protected byte[]
getSalt()
InputStream
getSmartInputStream(InputStream is, char[] password)
Creates a smart decrypting input stream using a given password.static void
setInstance(CryptoStreamFactory instance)
Set the default instance from an external application.-
Methods inherited from class org.apache.fulcrum.jce.crypto.CryptoStreamFactoryTemplate
getInputStream, getInputStream, getInputStream, getInputStream, getOutputStream, getOutputStream, getSmartInputStream
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.fulcrum.jce.crypto.CryptoStreamFactory
getInputStream, getInputStream, getInputStream, getInputStream, getOutputStream, getOutputStream, getSmartInputStream
-
-
-
-
Field Detail
-
salt
protected final byte[] salt
the salt for the PBE algorithm
-
count
protected int count
the count paramter for the PBE algorithm
-
providerName
protected String providerName
the name of the JCE provider
-
algorithm
protected String algorithm
the algorithm to use
-
PROVIDERNAME
protected static final String PROVIDERNAME
The JCE provider name known to work. If the value is set to null an appropriate provider will be used.
-
-
Method Detail
-
getInstance
public static CryptoStreamFactory getInstance()
Factory method to get a default instance- Returns:
- an instance of the CryptoStreamFactory
-
setInstance
public static void setInstance(CryptoStreamFactory instance)
Set the default instance from an external application.- Parameters:
instance
- the new default instance
-
getInstance
public static CryptoStreamFactory getInstance(byte[] salt, int count)
Factory method to get a default instance- Parameters:
salt
- the salt for the PBE algorithmcount
- the iteration for PBEParameterSpec- Returns:
- an instance of the CryptoStreamFactory
-
getSmartInputStream
public InputStream getSmartInputStream(InputStream is, char[] password) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactory
Creates a smart decrypting input stream using a given password. The implementation looks at the binary content to decide if it was encrypted or not thereby providing transparent access to encrypted/unencrypted files.- Specified by:
getSmartInputStream
in interfaceCryptoStreamFactory
- Specified by:
getSmartInputStream
in classCryptoStreamFactoryTemplate
- Parameters:
is
- the input stream to be wrappedpassword
- the password to be used- Returns:
- an decrypting input stream
- Throws:
GeneralSecurityException
- creating the input stream failedIOException
- creating the input stream failed- See Also:
CryptoStreamFactory.getSmartInputStream(java.io.InputStream,char[])
-
getAlgorithm
public String getAlgorithm()
Description copied from interface:CryptoStreamFactory
Info about used algorithm.- Specified by:
getAlgorithm
in interfaceCryptoStreamFactory
- Returns:
- Returns the algorithm.
-
getCount
protected int getCount()
- Returns:
- Returns the count.
-
getProviderName
protected String getProviderName()
- Returns:
- Returns the providerName.
-
getSalt
protected byte[] getSalt()
- Returns:
- Returns the salt.
-
createKey
protected Key createKey(char[] password) throws GeneralSecurityException
Create a PBE key.- Parameters:
password
- the password to use.- Returns:
- the key
- Throws:
GeneralSecurityException
- creating the key failed
-
createCipher
protected Cipher createCipher(int mode, char[] password) throws GeneralSecurityException, IOException
Create a Cipher.- Specified by:
createCipher
in classCryptoStreamFactoryTemplate
- Parameters:
mode
- the cipher modepassword
- the password- Returns:
- an instance of a cipher
- Throws:
GeneralSecurityException
- creating a cipher failedIOException
- creating a cipher failed
-
-