Package org.apache.fulcrum.jce.crypto
Class CryptoUtil
- java.lang.Object
-
- org.apache.fulcrum.jce.crypto.CryptoUtil
-
- Direct Known Subclasses:
CryptoUtilJ8
public class CryptoUtil extends Object
Helper class to provde generic functions to work with CryptoStreams. 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 CryptoStreamFactorycryptoStreamFactory
-
Constructor Summary
Constructors Modifier Constructor Description protectedCryptoUtil()protectedCryptoUtil(byte[] salt, int count)
-
Method Summary
Modifier and Type Method Description static longcopy(InputStream is, OutputStream os)Deprecated.use StreamUtil insteadvoiddecrypt(Object source, Object target, char[] password)Copies from a source to a target object using decryption.protected voiddecrypt(CryptoStreamFactory factory, Object source, Object target, char[] password)Copies from a source to a target object using decryption and a caller-suppier CryptoStreamFactory.StringdecryptString(String cipherText, char[] password)Decrypts an encrypted string into the plain text.StringdecryptString(String cipherText, char[] password, boolean withClearCode)Decrypts an encrypted string into the plain text.StringdecryptString(CryptoStreamFactory factory, String cipherText, char[] password)Decrypts an encrypted string into the plain text.StringdecryptStringWithClearCode(String cipherText, char[] password)Decrypts an encrypted string into the plain text.voidencrypt(Object source, Object target, char[] password)Copies from a source to a target object using encryptionvoidencrypt(CryptoStreamFactory factory, Object source, Object target, char[] password)Copies from a source to a target object using encryption and a caller supplied CryptoStreamFactory.StringencryptString(String plainText, char[] password)Encrypts a string into a hex string.StringencryptString(CryptoStreamFactory factory, String plainText, char[] password, boolean withClearCode)Encrypts a string into a hex string.StringencryptStringWithClearCode(String plainText, char[] password)Encrypts a string into a hex string usingCryptoParametersJ8.CLEAR_CODE_DEFAULTCryptoStreamFactorygetCryptoStreamFactory()static CryptoUtilgetInstance()Factory method to get a default instancestatic CryptoUtilgetInstance(byte[] salt, int count)Factory method to get a default instance
-
-
-
Field Detail
-
cryptoStreamFactory
protected CryptoStreamFactory cryptoStreamFactory
-
-
Method Detail
-
getInstance
public static CryptoUtil getInstance()
Factory method to get a default instance- Returns:
- an instance of the CryptoUtil
-
getInstance
public static CryptoUtil 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 CryptoUtil
-
encrypt
public void encrypt(Object source, Object target, char[] password) throws GeneralSecurityException, IOException
Copies from a source to a target object using encryption- Parameters:
source- the source objecttarget- the target objectpassword- the password to use for encryption- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
encrypt
public void encrypt(CryptoStreamFactory factory, Object source, Object target, char[] password) throws GeneralSecurityException, IOException
Copies from a source to a target object using encryption and a caller supplied CryptoStreamFactory.- Parameters:
factory- the factory to create the crypto streamssource- the source objecttarget- the target objectpassword- the password to use for encryption- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
encryptStringWithClearCode
public String encryptStringWithClearCode(String plainText, char[] password) throws GeneralSecurityException, IOException
Encrypts a string into a hex string usingCryptoParametersJ8.CLEAR_CODE_DEFAULT- Parameters:
plainText- the plain text to be encryptedpassword- the password for encryption- Returns:
- the encrypted string
- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
encryptString
public String encryptString(String plainText, char[] password) throws GeneralSecurityException, IOException
Encrypts a string into a hex string.- Parameters:
plainText- the plain text to be encryptedpassword- the password for encryption- Returns:
- the encrypted string
- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
encryptString
public String encryptString(CryptoStreamFactory factory, String plainText, char[] password, boolean withClearCode) throws GeneralSecurityException, IOException
Encrypts a string into a hex string.- Parameters:
factory- the factory to create the crypto streamsplainText- the plain text to be encryptedpassword- the password for encryptionwithClearCode- boolean to indicate, that a string containing how it was decoded is included- Returns:
- the encrypted string
- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
decrypt
public void decrypt(Object source, Object target, char[] password) throws GeneralSecurityException, IOException
Copies from a source to a target object using decryption.- Parameters:
source- the source objecttarget- the target objectpassword- the password to use for decryption- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
decryptStringWithClearCode
public String decryptStringWithClearCode(String cipherText, char[] password) throws GeneralSecurityException, IOException
Decrypts an encrypted string into the plain text. The encrypted string must be a hex string created by encryptString. Decrypts encrypted text afterCryptoParametersJ8.CLEAR_CODE_DEFAULT. Removes ClearCode length of 10 bit, before decrpyting expected as prefix.- Parameters:
cipherText- the encrypted text to be decryptedpassword- the password for decryption- Returns:
- the decrypted string
- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
decryptString
public String decryptString(String cipherText, char[] password) throws GeneralSecurityException, IOException
Decrypts an encrypted string into the plain text. The encrypted string must be a hex string created by encryptString.- Parameters:
cipherText- the encrypted text to be decryptedpassword- the password for decryption- Returns:
- the decrypted string
- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
decryptString
public String decryptString(String cipherText, char[] password, boolean withClearCode) throws GeneralSecurityException, IOException
Decrypts an encrypted string into the plain text. The encrypted string must be a hex string created by encryptString.- Parameters:
cipherText- the encrypted text to be decryptedpassword- the password for decryptionwithClearCode- boolean to indicate, that a string containing how it was decoded was included during encryption- Returns:
- the decrypted string
- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
decrypt
protected void decrypt(CryptoStreamFactory factory, Object source, Object target, char[] password) throws GeneralSecurityException, IOException
Copies from a source to a target object using decryption and a caller-suppier CryptoStreamFactory.- Parameters:
factory- the factory to create the crypto streamssource- the source objecttarget- the target objectpassword- the password to use for decryption- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
decryptString
public String decryptString(CryptoStreamFactory factory, String cipherText, char[] password) throws GeneralSecurityException, IOException
Decrypts an encrypted string into the plain text. The encrypted string must be a hex string created by encryptString.- Parameters:
factory- the factory to create the crypto streamscipherText- the encrypted text to be decryptedpassword- the password for decryption- Returns:
- the decrypted string
- Throws:
GeneralSecurityException- accessing JCE failedIOException- accessing the souce failed
-
copy
public static long copy(InputStream is, OutputStream os) throws IOException
Deprecated.use StreamUtil insteadPumps the input stream to the output stream.- Parameters:
is- the source input streamos- the target output stream- Returns:
- the number of bytes copied
- Throws:
IOException- the copying failed
-
getCryptoStreamFactory
public CryptoStreamFactory getCryptoStreamFactory()
- Returns:
- the CryptoStreamFactory to be used
-
-