public class EncryptionUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_ITERATIONS
The default number of iterations to be executed when creating the encrypting algorithm.
|
static String |
DEFAULT_SECRET_KEY_TYPE
The default secret key type to be generated from a password.
|
static String |
PASSWORD_CHARS
The default number of iterations to be executed when creating the encrypting algorithm.
|
| Constructor and Description |
|---|
EncryptionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
createMD5(byte[] b)
Creates an MD5 hash from the byte array.
|
static byte[] |
createMD5(String s)
Creates an MD5 hash from the string.
|
static byte[] |
crypt(byte[] bytes,
Cipher cipher)
Transforms all bytes from the input array with a crypt alorithm
and returns the transformed bytes.
|
static byte[] |
crypt(byte[] bytes,
Cipher cipher,
int blockSize)
Transforms all bytes from the input array with a crypt alorithm
and returns the transformed bytes.
|
static byte[] |
decodeBase64(String s)
Decodes a BASE64 string into bytes.
|
static PrivateKey |
decodeBase64PrivateKey(String s)
Decodes a private DSA key from the BASE64 representation.
|
static PrivateKey |
decodeBase64PrivateKey(String s,
String algorithm)
Decodes a private key from the BASE64 representation.
|
static PublicKey |
decodeBase64PublicKey(String s)
Decodes a public key (DSA) from the BASE64 representation.
|
static PublicKey |
decodeBase64PublicKey(String s,
String algorithm)
Decodes a public key from the BASE64 representation.
|
static String |
decodeBase64ToString(String s)
Decodes a BASE64 string and generates the original
string from the result.
|
static String |
decodeBase64ToString(String s,
Charset charset)
Decodes a BASE64 string and generates the original
string from the result.
|
static String |
encodeBase64(byte[] b)
Encodes a byte array into its BASE64 representation.
|
static String |
encodeBase64(PrivateKey key)
Encodes the private key into BASE64 representation.
|
static String |
encodeBase64(PublicKey key)
Encodes the public key into BASE64 representation.
|
static String |
encodeBase64(String s)
Encodes a string into its BASE64 representation.
|
static String |
encodeBase64(String s,
Charset charset)
Encodes a string into its BASE64 representation.
|
static String |
encodeBase64Pkcs8(PrivateKey key)
Encodes the private key into BASE64 PKCS8 representation.
|
static String |
encodeBase64X509(PublicKey key)
Encodes the public key into BASE64 X.509 representation.
|
static KeyPair |
generateKey()
Generates a 512 byte RSA key pair.
|
static KeyPair |
generateKey(byte[] seed)
Generates a public/private key pair.
|
static KeyPair |
generateKey(int keySize)
Generates a RSA key pair of given size
|
static KeyPair |
generateKey(String seed)
Generates a public/private key pair.
|
static KeyPair |
generateKey(String seed,
Charset charset)
Generates a public/private key pair.
|
static KeyPair |
generateKey(String algorithm,
int keySize)
Generates a key pair.
|
static PBEParameterSpec |
generateParamSpec()
Creates a PBE Parameter specification randomly.
|
static PBEParameterSpec |
generateParamSpec(byte[] salt,
int iterationCount)
Creates a PBE Parameter Spec for encryption algorithm.
|
static String |
generatePassword()
Creates a random password.
|
static String |
generatePassword(int length)
Creates a random password.
|
static String |
generatePassword(String allowedChars)
Creates a random password.
|
static String |
generatePassword(String allowedChars,
long randomInit,
int length)
Creates a random password.
|
static byte[] |
generateRandomBytes(long randomInit,
int size)
Generate a random array of bytes
|
static byte[] |
generateSalt()
Creates a random salt array.
|
static byte[] |
generateSalt(long randomInit)
Creates a random salt array.
|
static Provider[] |
getKeyPairGenerators()
Returns all security providers.
|
static KeySpec |
getKeySpec(Key key)
Creates a key specification.
|
static KeyStore |
getKeyStore(InputStream in,
char[] password)
Load the default keystore type.
|
static KeyStore |
getKeyStore(String filename,
char[] password)
Load the default keystore type.
|
static KeyStore |
getKeyStore(String type,
InputStream in,
char[] password)
Load the given keystore.
|
static KeyStore |
getKeyStore(String type,
String filename,
char[] password)
Load the default keystore type.
|
static SecretKey |
getSecretKey(int iterationCount,
String passPhrase,
byte[] salt) |
static PrivateKey |
loadPrivateKey(File file,
String algorithm)
Instantiates a
PrivateKey from given file. |
static PrivateKey |
loadPrivateKey(InputStream in,
String algorithm)
Instantiates a
PrivateKey from given stream. |
static PrivateKey |
loadPrivateKey(Reader in,
String algorithm)
Instantiates a
PrivateKey from given reader. |
static PrivateKey |
loadPrivateKey(String filename,
String algorithm)
Instantiates a
PrivateKey from given file. |
static PrivateKey |
loadPrivateKey(URL url,
String algorithm)
Instantiates a
PrivateKey from given URL. |
static PublicKey |
loadPublicKey(File file,
String algorithm)
Instantiates a
PublicKey from given file. |
static PublicKey |
loadPublicKey(InputStream in,
String algorithm)
Instantiates a
PublicKey from given stream. |
static PublicKey |
loadPublicKey(Reader in,
String algorithm)
Instantiates a
PublicKey from given reader. |
static PublicKey |
loadPublicKey(String filename,
String algorithm)
Instantiates a
PublicKey from given file. |
static PublicKey |
loadPublicKey(URL url,
String algorithm)
Instantiates a
PublicKey from given URL. |
static void |
save(File file,
PrivateKey key)
Saves a
PrivateKey into given file. |
static void |
save(File file,
PublicKey key)
Saves a
PublicKey into given file. |
static void |
save(OutputStream out,
PrivateKey key)
Saves a
PrivateKey into given stream. |
static void |
save(OutputStream out,
PublicKey key)
Saves a
PublicKey into given stream. |
static void |
save(PrintWriter out,
PrivateKey key)
Saves a
PrivateKey into given writer. |
static void |
save(PrintWriter out,
PublicKey key)
Saves a
PublicKey into given writer. |
static void |
save(String filename,
PrivateKey key)
Saves a
PrivateKey into given file. |
static void |
save(String filename,
PublicKey key)
Saves a
PublicKey into given file. |
public static final String DEFAULT_SECRET_KEY_TYPE
public static final int DEFAULT_ITERATIONS
public static final String PASSWORD_CHARS
public static KeySpec getKeySpec(Key key) throws InvalidKeySpecException, NoSuchAlgorithmException
InvalidKeySpecExceptionNoSuchAlgorithmExceptionpublic static PBEParameterSpec generateParamSpec()
generateParamSpec(byte[], int)public static PBEParameterSpec generateParamSpec(byte[] salt, int iterationCount)
salt - salt to be used (if NULL, a random one will be generated)iterationCount - number of iterations for encryption (if 0 then default number will be used)DEFAULT_ITERATIONS,
generateSalt(long)public static byte[] generateSalt()
public static byte[] generateSalt(long randomInit)
randomInit - initializerpublic static byte[] generateRandomBytes(long randomInit,
int size)
randomInit - initializersize - size of returned arraypublic static String generatePassword()
public static String generatePassword(int length)
length - length of passwordpublic static String generatePassword(String allowedChars)
allowedChars - all characters allowedpublic static String generatePassword(String allowedChars, long randomInit, int length)
allowedChars - allowedCharactersrandomInit - initializerlength - length of passwordpublic static SecretKey getSecretKey(int iterationCount, String passPhrase, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException
public static KeyPair generateKey(String seed) throws NoSuchProviderException, NoSuchAlgorithmException
seed - seed to be used.NoSuchProviderExceptionNoSuchAlgorithmExceptionpublic static KeyPair generateKey(String seed, Charset charset) throws NoSuchProviderException, NoSuchAlgorithmException
seed - seed to be used.charset - the charset to be used for string encoding (null for default charset)NoSuchProviderExceptionNoSuchAlgorithmExceptionpublic static KeyPair generateKey(byte[] seed) throws NoSuchProviderException, NoSuchAlgorithmException
seed - seed to be used.NoSuchProviderExceptionNoSuchAlgorithmExceptionpublic static KeyPair generateKey() throws NoSuchAlgorithmException
NoSuchAlgorithmExceptionpublic static KeyPair generateKey(int keySize) throws NoSuchAlgorithmException
keySize - the key sizeNoSuchAlgorithmExceptionpublic static KeyPair generateKey(String algorithm, int keySize) throws NoSuchAlgorithmException
algorithm - algorithm, e.g. "RSA"keySize - teh key sizeNoSuchAlgorithmExceptionpublic static Provider[] getKeyPairGenerators()
public static String encodeBase64(PublicKey key)
key - public keypublic static String encodeBase64X509(PublicKey key)
key - public keypublic static String encodeBase64(PrivateKey key)
key - private keypublic static String encodeBase64Pkcs8(PrivateKey key)
key - private keypublic static PublicKey decodeBase64PublicKey(String s) throws InvalidKeySpecException, NoSuchAlgorithmException
s - BASE64 representationInvalidKeySpecExceptionNoSuchAlgorithmExceptionpublic static PublicKey decodeBase64PublicKey(String s, String algorithm) throws InvalidKeySpecException, NoSuchAlgorithmException
s - BASE64 representationInvalidKeySpecExceptionNoSuchAlgorithmExceptionpublic static PrivateKey decodeBase64PrivateKey(String s) throws InvalidKeySpecException, NoSuchAlgorithmException
s - BASE64 representationInvalidKeySpecExceptionNoSuchAlgorithmExceptionpublic static PrivateKey decodeBase64PrivateKey(String s, String algorithm) throws InvalidKeySpecException, NoSuchAlgorithmException
s - BASE64 representationInvalidKeySpecExceptionNoSuchAlgorithmExceptionpublic static String decodeBase64ToString(String s)
s - BASE64 encoded stringencodeBase64(String)public static String decodeBase64ToString(String s, Charset charset)
s - BASE64 encoded stringcharset - the chracter set to be used for constructing the string (can be null for default charset)encodeBase64(String)public static byte[] decodeBase64(String s)
s - BASE64 encoded stringpublic static String encodeBase64(String s)
s - string to encodepublic static String encodeBase64(String s, Charset charset)
s - string to encodecharset - the charset to be used for string encoding (null for default charset)public static String encodeBase64(byte[] b)
b - bytes to encodepublic static KeyStore getKeyStore(String filename, char[] password) throws IOException
filename - filenamepassword - passwordIOExceptionpublic static KeyStore getKeyStore(String type, String filename, char[] password) throws IOException
filename - filenamepassword - passwordIOExceptionpublic static KeyStore getKeyStore(InputStream in, char[] password) throws IOException
in - input streampassword - passwordIOExceptionpublic static KeyStore getKeyStore(String type, InputStream in, char[] password) throws IOException
type - of keystorein - input streampassword - passwordIOExceptionpublic static byte[] crypt(byte[] bytes,
Cipher cipher)
throws IOException,
GeneralSecurityException
bytes - bytes to cryptcipher - crypt alorithm usedIOExceptionGeneralSecurityExceptionpublic static byte[] crypt(byte[] bytes,
Cipher cipher,
int blockSize)
throws IOException,
GeneralSecurityException
bytes - bytes to cryptcipher - crypt alorithm usedblockSize - block size to be applied (or 0 if none)IOExceptionGeneralSecurityExceptionpublic static byte[] createMD5(String s)
s - string to create the hash frompublic static byte[] createMD5(byte[] b)
b - bytes to create the hash frompublic static PrivateKey loadPrivateKey(File file, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PrivateKey from given file.file - file where private key is stored in BASE64 PKCS8 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PrivateKey loadPrivateKey(String filename, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PrivateKey from given file.filename - file where private key is stored in BASE64 PKCS8 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PrivateKey loadPrivateKey(URL url, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PrivateKey from given URL.url - URL where private key is stored in BASE64 PKCS8 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PrivateKey loadPrivateKey(InputStream in, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PrivateKey from given stream.in - stream where private key is stored in BASE64 PKCS8 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PrivateKey loadPrivateKey(Reader in, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PrivateKey from given reader.in - reader where private key is stored in BASE64 PKCS8 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PublicKey loadPublicKey(File file, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PublicKey from given file.file - file where private key is stored in BASE64 X.509 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PublicKey loadPublicKey(String filename, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PublicKey from given file.filename - name of file where private key is stored in BASE64 X.509 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PublicKey loadPublicKey(URL url, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PublicKey from given URL.url - URL where private key is stored in BASE64 X.509 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PublicKey loadPublicKey(InputStream in, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PublicKey from given stream.in - stream where private key is stored in BASE64 X.509 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static PublicKey loadPublicKey(Reader in, String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
PublicKey from given reader.in - reader where private key is stored in BASE64 X.509 encodingalgorithm - algorithm that the key usedNoSuchAlgorithmExceptionInvalidKeySpecExceptionIOExceptionpublic static void save(File file, PrivateKey key) throws IOException
PrivateKey into given file.file - file where private key will be stored in BASE64 PKCS8 encodingkey - key to be storedIOException - when key cannot be storedpublic static void save(String filename, PrivateKey key) throws IOException
PrivateKey into given file.filename - name of file where private key will be stored in BASE64 PKCS8 encodingkey - key to be storedIOException - when key cannot be storedpublic static void save(OutputStream out, PrivateKey key) throws IOException
PrivateKey into given stream.out - stream where private key will be stored in BASE64 PKCS8 encodingkey - key to be storedIOException - when key cannot be storedpublic static void save(PrintWriter out, PrivateKey key) throws IOException
PrivateKey into given writer.out - writer where private key will be stored in BASE64 PKCS8 encodingkey - key to be storedIOException - when key cannot be storedpublic static void save(File file, PublicKey key) throws IOException
PublicKey into given file.file - file where private key will be stored in BASE64 X.509 encodingkey - key to be storedIOException - when key cannot be storedpublic static void save(String filename, PublicKey key) throws IOException
PublicKey into given file.filename - name of file where private key will be stored in BASE64 X.509 encodingkey - key to be storedIOException - when key cannot be storedpublic static void save(OutputStream out, PublicKey key) throws IOException
PublicKey into given stream.out - stream where private key will be stored in BASE64 X.509 encodingkey - key to be storedIOException - when key cannot be storedpublic static void save(PrintWriter out, PublicKey key) throws IOException
PublicKey into given writer.out - writer where private key will be stored in BASE64 X.509 encodingkey - key to be storedIOException - when key cannot be storedCopyright © 2014. All rights reserved.