Interface Cipher
-
public interface Cipher
Cipher
encapsulates aCipherStrategy
and offers methods for:- encryption/decryption
- key-generation
- key conversion
Its main purpose is to simplify the complexity of the
javax.crypto
andjava.security
packages. Their many different free combinable pieces are put together in a few defaultCipherStrategy
s for typical use by SAPERION and all methods are centralized in thisCipher
. This results in a simple-to-use practical API, on the cost of some method-signatures looking a bit weird and some methods not working well with allCipherStrategy
s.A
Cipher
is created using theCipherFactory
and is based on aCipherStrategy
and aKeyDerivationAlgorithm
.The
CipherStrategy
defines the encryption algorithm, block mode, padding type and key size to use and enumerates the few strategies used by SAPERION.The
KeyDerivationAlgorithm
defines the algorithm of deriving aKey
from a password and salt. This method only works for symmetric encryption algorithms. Firstcreate a salt
, thanget a key
from a password and salt and remember the salt, f.e. together with the encrypted text or in a user-database together with the generated key. To decrypt the text or to test the password against the key use the same method again.Use
newKeys()
to create a new pair of keys. If the encryption algorithm is asymmetric the first is the public key and the second is the private key. If the encryption algorithm is symmetric both keys are the same instance.To export or store a
Key
useKey.getEncoded()
and for an asymmetric encryption algorithm remember whether it is the public or private key (well in many circumstances this should be implicitly clear out of the location WHERE the key is stored or exported to/imported from). To restore such aKey
again, usegetKey(byte[], boolean)
.To encrypt/decrypt binary data use
encrypt(Key, byte[])
/decrypt(Key, byte[])
, or the streaming overridesencrypt(Key, InputStream, OutputStream)
/decrypt(Key, InputStream, OutputStream)
.For ease of use there are convenient methods for
String
-encryption/decryption (encrypt(Key, String)
/decrypt(Key, String)
). TheString
to encrypt is first converted to abyte[]
using a "UTF-8"-encoding, than encrypted and the result isconverted to a hexadecimal representation
. Decryption works vice versa.The implementations are not synchronized. If used by different threads concurrently, external synchronization is necessary.
- Author:
- agz
- See Also:
CipherStrategy
,KeyDerivationAlgorithm
,CipherFactory
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description byte[]
decrypt(java.security.Key key, byte[] arEncryptedData)
Decrypts the specified encrypted data using the specifiedKey
and returns the clear data.java.lang.String
decrypt(java.security.Key key, char[] arEncryptedData)
Decrypts the specified encrypted data using the specifiedKey
and returns the clear data.void
decrypt(java.security.Key key, java.io.InputStream inEncryptedData, java.io.OutputStream outClearData)
Decrypts the encrypted data of the specifiedInputStream
using the specifiedKey
and writes the clear data to the specifiedOutputStream
.java.lang.String
decrypt(java.security.Key key, java.lang.String encryptedData)
Decrypts the specified encrypted data using the specifiedKey
and returns the clear data.byte[]
encrypt(java.security.Key key, byte[] arClearData)
Encrypts the specified clear data using the specifiedKey
and returns the encrypted data.void
encrypt(java.security.Key key, java.io.InputStream inClearData, java.io.OutputStream outEncryptedData)
Encrypts the clear data of the specifiedInputStream
using the specifiedKey
and writes the encrypted data to the specifiedOutputStream
.char[]
encrypt(java.security.Key key, java.lang.String clearData)
Encrypts the specified clear data using the specifiedKey
and returns the encrypted data.CipherStrategy
getCipherStrategy()
Returns theCipherStrategy
of thisCipher
.java.security.Key
getKey(byte[] encodedKey, boolean firstKey)
Restores aKey
from the specified encoded representation (as retrieved byKey.getEncoded()
).java.security.Key
getKey(java.lang.String password, byte[] salt)
Creates aKey
from the specified password and salt implicitly using theKeyDerivationAlgorithm
associated with thisCipher
.KeyDerivationAlgorithm
getKeyDerivationAlgorithm()
Returns theKeyDerivationAlgorithm
of thisCipher
.Pair<java.security.Key,java.security.Key>
newKeys()
Creates aPair
of new keys.byte[]
newSalt()
Creates a new salt of the correct size initialized with secure random values.
-
-
-
Method Detail
-
getCipherStrategy
CipherStrategy getCipherStrategy()
Returns theCipherStrategy
of thisCipher
.- Returns:
- the
CipherStrategy
of thisCipher
-
getKeyDerivationAlgorithm
KeyDerivationAlgorithm getKeyDerivationAlgorithm()
Returns theKeyDerivationAlgorithm
of thisCipher
.- Returns:
- the
KeyDerivationAlgorithm
of thisCipher
-
newKeys
Pair<java.security.Key,java.security.Key> newKeys()
Creates aPair
of new keys. If the encryption algorithm is asymmetric the first is the public key and the second is the private key. If the encryption algorithm is symmetric both keys are the same instance.- Returns:
- a
Pair
of new keys
-
getKey
java.security.Key getKey(byte[] encodedKey, boolean firstKey)
Restores aKey
from the specified encoded representation (as retrieved byKey.getEncoded()
). The specified encoded representation must represent a valid key of theCipherStrategy
of thisCipher
.- Parameters:
encodedKey
- encoded representation of theKey
to getfirstKey
- whether the first or secondKey
should be restored. If the encryption algorithm is asymmetric the first is the public key and the second is the private key. If the encryption algorithm is symmetric both keys are the same, and this parameter has no effect.- Returns:
- the
Key
-
getKey
java.security.Key getKey(java.lang.String password, byte[] salt)
Creates aKey
from the specified password and salt implicitly using theKeyDerivationAlgorithm
associated with thisCipher
. The specified password and salt must not benull
.- Parameters:
password
- passwordsalt
- salt- Returns:
- the
Key
created from the specified password and salt - See Also:
newSalt()
-
newSalt
byte[] newSalt()
Creates a new salt of the correct size initialized with secure random values.- Returns:
- new salt
- See Also:
getKey(String, byte[])
-
encrypt
byte[] encrypt(java.security.Key key, byte[] arClearData)
Encrypts the specified clear data using the specifiedKey
and returns the encrypted data. The specified key must not benull
and must be valid for theCipherStrategy
of thisCipher
. The specified clear data must not benull
.- Parameters:
key
-Key
arClearData
- clear data- Returns:
- encrypted data
- See Also:
decrypt(Key, byte[])
,encrypt(Key, InputStream, OutputStream)
,encrypt(Key, String)
-
decrypt
byte[] decrypt(java.security.Key key, byte[] arEncryptedData)
Decrypts the specified encrypted data using the specifiedKey
and returns the clear data. The specified key must not benull
and must be valid for theCipherStrategy
of thisCipher
. The specified encrypted data must not benull
and must be a decryptable.- Parameters:
key
-Key
arEncryptedData
- encrypted data- Returns:
- clear data
- See Also:
encrypt(Key, byte[])
,decrypt(Key, InputStream, OutputStream)
,decrypt(Key, char[])
,decrypt(Key, String)
-
encrypt
void encrypt(java.security.Key key, java.io.InputStream inClearData, java.io.OutputStream outEncryptedData) throws java.io.IOException
Encrypts the clear data of the specifiedInputStream
using the specifiedKey
and writes the encrypted data to the specifiedOutputStream
. The specified key must not benull
and must be valid for theCipherStrategy
of thisCipher
. Both specified streams must not benull
.- Parameters:
key
-Key
inClearData
-InputStream
to read clear data fromoutEncryptedData
-OutputStream
to write the encrypted data to- Throws:
java.io.IOException
- on errors handling one of the streams- See Also:
decrypt(Key, InputStream, OutputStream)
,encrypt(Key, byte[])
,encrypt(Key, String)
-
decrypt
void decrypt(java.security.Key key, java.io.InputStream inEncryptedData, java.io.OutputStream outClearData) throws java.io.IOException
Decrypts the encrypted data of the specifiedInputStream
using the specifiedKey
and writes the clear data to the specifiedOutputStream
. The specified key must not benull
and must be valid for theCipherStrategy
of thisCipher
. Both specified streams must not benull
.- Parameters:
key
-Key
inEncryptedData
-InputStream
to read encrypted data fromoutClearData
-OutputStream
to write the clear data to- Throws:
java.io.IOException
- on errors handling one of the streams- See Also:
encrypt(Key, InputStream, OutputStream)
,decrypt(Key, byte[])
,decrypt(Key, char[])
,decrypt(Key, String)
-
encrypt
char[] encrypt(java.security.Key key, java.lang.String clearData)
Encrypts the specified clear data using the specifiedKey
and returns the encrypted data. Seethe class documentation
for a detailed description of the used algorithm. The specified key must not benull
and must be valid for theCipherStrategy
of thisCipher
. The specified clear data must not benull
.- Parameters:
key
-Key
clearData
- clear data- Returns:
- encrypted data
- See Also:
decrypt(Key, char[])
,decrypt(Key, String)
,encrypt(Key, byte[])
,encrypt(Key, InputStream, OutputStream)
-
decrypt
java.lang.String decrypt(java.security.Key key, char[] arEncryptedData)
Decrypts the specified encrypted data using the specifiedKey
and returns the clear data. Seethe class documentation
for a detailed description of the used algorithm. The specified key must not benull
and must be valid for theCipherStrategy
of thisCipher
. The specified encrypted data must not benull
and must be a decryptable.- Parameters:
key
-Key
arEncryptedData
- encrypted data- Returns:
- clear data
- See Also:
encrypt(Key, String)
,decrypt(Key, String)
,decrypt(Key, byte[])
,decrypt(Key, InputStream, OutputStream)
-
decrypt
java.lang.String decrypt(java.security.Key key, java.lang.String encryptedData)
Decrypts the specified encrypted data using the specifiedKey
and returns the clear data. Seethe class documentation
for a detailed description of the used algorithm. The specified key must not benull
and must be valid for theCipherStrategy
of thisCipher
. The specified encrypted data must not benull
and must be a decryptable.- Parameters:
key
-Key
encryptedData
- encrypted data- Returns:
- clear data
- See Also:
encrypt(Key, String)
,decrypt(Key, char[])
,decrypt(Key, byte[])
,decrypt(Key, InputStream, OutputStream)
-
-