Interface Cipher
Cipher encapsulates a CipherStrategy and offers methods for:
- encryption/decryption
- key-generation
- key conversion
Its main purpose is to simplify the complexity of the javax.crypto and java.security packages. Their
many different free combinable pieces are put together in a few default CipherStrategys for typical use by
SAPERION and all methods are centralized in this Cipher. 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 all
CipherStrategys.
A Cipher is created using the CipherFactory and is based on a CipherStrategy and a
KeyDerivationAlgorithm.
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 a Key from a password and salt. This
method only works for symmetric encryption algorithms. First create a salt, than
get 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 use Key.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 a Key again, use
getKey(byte[], boolean).
To encrypt/decrypt binary data use encrypt(Key, byte[])/decrypt(Key, byte[]), or the streaming
overrides encrypt(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)). The String to encrypt is first converted to a byte[] using a
"UTF-8"-encoding, than encrypted and the result is converted 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:
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]Decrypts the specified encrypted data using the specifiedKeyand returns the clear data.Decrypts the specified encrypted data using the specifiedKeyand returns the clear data.voiddecrypt(Key key, InputStream inEncryptedData, OutputStream outClearData) Decrypts the encrypted data of the specifiedInputStreamusing the specifiedKeyand writes the clear data to the specifiedOutputStream.Decrypts the specified encrypted data using the specifiedKeyand returns the clear data.byte[]Encrypts the specified clear data using the specifiedKeyand returns the encrypted data.voidencrypt(Key key, InputStream inClearData, OutputStream outEncryptedData) Encrypts the clear data of the specifiedInputStreamusing the specifiedKeyand writes the encrypted data to the specifiedOutputStream.char[]Encrypts the specified clear data using the specifiedKeyand returns the encrypted data.Returns theCipherStrategyof thisCipher.getKey(byte[] encodedKey, boolean firstKey) Restores aKeyfrom the specified encoded representation (as retrieved byKey.getEncoded()).Creates aKeyfrom the specified password and salt implicitly using theKeyDerivationAlgorithmassociated with thisCipher.Returns theKeyDerivationAlgorithmof thisCipher.newKeys()Creates aPairof new keys.byte[]newSalt()Creates a new salt of the correct size initialized with secure random values.
-
Method Details
-
getCipherStrategy
CipherStrategy getCipherStrategy()Returns theCipherStrategyof thisCipher.- Returns:
- the
CipherStrategyof thisCipher
-
getKeyDerivationAlgorithm
KeyDerivationAlgorithm getKeyDerivationAlgorithm()Returns theKeyDerivationAlgorithmof thisCipher.- Returns:
- the
KeyDerivationAlgorithmof thisCipher
-
newKeys
Creates aPairof 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
Pairof new keys
-
getKey
Restores aKeyfrom the specified encoded representation (as retrieved byKey.getEncoded()). The specified encoded representation must represent a valid key of theCipherStrategyof thisCipher.- Parameters:
encodedKey- encoded representation of theKeyto getfirstKey- whether the first or secondKeyshould 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
Creates aKeyfrom the specified password and salt implicitly using theKeyDerivationAlgorithmassociated with thisCipher. The specified password and salt must not benull. -
newSalt
byte[] newSalt()Creates a new salt of the correct size initialized with secure random values.- Returns:
- new salt
- See Also:
-
encrypt
Encrypts the specified clear data using the specifiedKeyand returns the encrypted data. The specified key must not benulland must be valid for theCipherStrategyof thisCipher. The specified clear data must not benull.- Parameters:
key-KeyarClearData- clear data- Returns:
- encrypted data
- See Also:
-
decrypt
Decrypts the specified encrypted data using the specifiedKeyand returns the clear data. The specified key must not benulland must be valid for theCipherStrategyof thisCipher. The specified encrypted data must not benulland must be a decryptable.- Parameters:
key-KeyarEncryptedData- encrypted data- Returns:
- clear data
- See Also:
-
encrypt
Encrypts the clear data of the specifiedInputStreamusing the specifiedKeyand writes the encrypted data to the specifiedOutputStream. The specified key must not benulland must be valid for theCipherStrategyof thisCipher. Both specified streams must not benull.- Parameters:
key-KeyinClearData-InputStreamto read clear data fromoutEncryptedData-OutputStreamto write the encrypted data to- Throws:
IOException- on errors handling one of the streams- See Also:
-
decrypt
Decrypts the encrypted data of the specifiedInputStreamusing the specifiedKeyand writes the clear data to the specifiedOutputStream. The specified key must not benulland must be valid for theCipherStrategyof thisCipher. Both specified streams must not benull.- Parameters:
key-KeyinEncryptedData-InputStreamto read encrypted data fromoutClearData-OutputStreamto write the clear data to- Throws:
IOException- on errors handling one of the streams- See Also:
-
encrypt
Encrypts the specified clear data using the specifiedKeyand returns the encrypted data. Seethe class documentationfor a detailed description of the used algorithm. The specified key must not benulland must be valid for theCipherStrategyof thisCipher. The specified clear data must not benull.- Parameters:
key-KeyclearData- clear data- Returns:
- encrypted data
- See Also:
-
decrypt
Decrypts the specified encrypted data using the specifiedKeyand returns the clear data. Seethe class documentationfor a detailed description of the used algorithm. The specified key must not benulland must be valid for theCipherStrategyof thisCipher. The specified encrypted data must not benulland must be a decryptable.- Parameters:
key-KeyarEncryptedData- encrypted data- Returns:
- clear data
- See Also:
-
decrypt
Decrypts the specified encrypted data using the specifiedKeyand returns the clear data. Seethe class documentationfor a detailed description of the used algorithm. The specified key must not benulland must be valid for theCipherStrategyof thisCipher. The specified encrypted data must not benulland must be a decryptable.- Parameters:
key-KeyencryptedData- encrypted data- Returns:
- clear data
- See Also:
-