Package craterdog.security
Class MessageCryptex
java.lang.Object
craterdog.security.MessageCryptex
This abstract class defines constants and implements invariant methods that are needed
by all concrete classes that implement key and message encryption and decryption.
- Author:
- Derk Norton
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract booleanbytesAreValid(PublicKey certificate, byte[] bytes, byte[] signature) This method checks to see if the signature for a signed byte array is valid.abstract PrivateKeydecodePrivateKey(String pem, SecretKey passwordKey) This method decodes private key from a PEM string.abstract PublicKeydecodePublicKey(String pem) This method decodes public key from a PEM string.final byte[]decodeString(String base64String) This method decodes a base 64 string into its original bytes.abstract CipherInputStreamdecryptionInputStream(SecretKey sharedKey, InputStream input) This method generates an input stream that performs decryption on another input stream.abstract SecretKeydecryptSharedKey(PrivateKey privateKey, byte[] encryptedKey) This method decrypts a shared key using the private key that is paired with the public certificate that was used to encrypt it at the source.final voiddecryptStream(SecretKey sharedKey, InputStream input, OutputStream output) This method decrypts a byte stream from an encrypted byte stream.final StringdecryptString(SecretKey sharedKey, byte[] encryptedString) This method decrypts a string using a shared key.final StringencodeBytes(byte[] bytes) This method encodes a byte array into a base 64 string.final StringencodeBytes(byte[] bytes, String indentation) This method encodes a byte array into a base 64 string where each line is prepended with an indentation string.final StringencodePrivateKey(PrivateKey privateKey, SecretKey passwordKey) This method encodes a private key into a PEM string.abstract StringencodePrivateKey(PrivateKey privateKey, SecretKey passwordKey, String indentation) This method encodes a private key into a PEM string with a prepended indentation string.final StringencodePublicKey(PublicKey key) This method encodes a public key into a PEM string.abstract StringencodePublicKey(PublicKey key, String indentation) This method encodes a public key into a PEM string with a prepended indentation string.abstract CipherOutputStreamencryptionOutputStream(SecretKey sharedKey, OutputStream output) This method generates an output stream that performs encryption on another output stream.abstract byte[]encryptSharedKey(PublicKey certificate, SecretKey sharedKey) This method encrypts a shared key using the public certificate of the destination for a data stream that will be encrypted using the shared key.final voidencryptStream(SecretKey sharedKey, InputStream input, OutputStream output) This method encrypts a byte stream using a shared key.final byte[]encryptString(SecretKey sharedKey, String string) This method encrypts a string using a shared key.abstract KeyPairThis method generates a new public/private key pair.abstract SecretKeygeneratePasswordKey(char[] password) This method generates a password (secret) key to be used for encrypting a private key.abstract SecretKeyThis method generates a shared (secret) key to be used for encrypting large amounts of data.abstract StringThis method returns the asymmetric encryption algorithm used by this cryptex.abstract intThis method returns the asymmetric key size.abstract StringThis method returns the asymmetric key type string.abstract StringThis method returns the asymmetric signature algorithm used by this cryptex.abstract StringThis method returns the hash algorithm.abstract StringThis method returns the password encoding type used for password based encryption (PBE) used by this cryptex.abstract StringThis method returns the symmetric encryption algorithm used by this cryptex.abstract intThis method returns the symmetric key size used by this cryptex.abstract StringThis method returns the symmetric key type used by this cryptex.abstract StringhashString(String string) This method returns a base 64 encoded SHA256 one-way hash of the specified string.abstract byte[]signBytes(PrivateKey privateKey, byte[] bytes) This method signs a byte array.
-
Constructor Details
-
MessageCryptex
public MessageCryptex()
-
-
Method Details
-
encodeBytes
This method encodes a byte array into a base 64 string.- Parameters:
bytes- The byte array to be encoded.- Returns:
- The base 64 encoded string for those bytes.
-
encodeBytes
This method encodes a byte array into a base 64 string where each line is prepended with an indentation string.- Parameters:
bytes- The byte array to be encoded.indentation- The indentation string that should be prepended to each line of the output.- Returns:
- The base 64 encoded string for those bytes.
-
decodeString
This method decodes a base 64 string into its original bytes.- Parameters:
base64String- The base 64 encoded string.- Returns:
- The corresponding decoded bytes.
-
getHashAlgorithm
This method returns the hash algorithm.- Returns:
- The hash algorithm.
-
hashString
This method returns a base 64 encoded SHA256 one-way hash of the specified string.- Parameters:
string- The string to be hashed.- Returns:
- A base 64 encoded one-way hash of the string.
-
getSymmetricKeyType
This method returns the symmetric key type used by this cryptex.- Returns:
- The type of the symmetric keys.
-
getSymmetricKeySize
public abstract int getSymmetricKeySize()This method returns the symmetric key size used by this cryptex.- Returns:
- The size of the symmetric keys.
-
getSymmetricEncryptionAlgorithm
This method returns the symmetric encryption algorithm used by this cryptex.- Returns:
- The name of the algorithm.
-
encryptString
This method encrypts a string using a shared key.- Parameters:
sharedKey- The shared key used for the encryption.string- The string to be encrypted.- Returns:
- The encrypted string.
-
decryptString
This method decrypts a string using a shared key.- Parameters:
sharedKey- The shared key used for the encryption.encryptedString- The encrypted string.- Returns:
- The decrypted string.
-
encryptStream
public final void encryptStream(SecretKey sharedKey, InputStream input, OutputStream output) throws IOException This method encrypts a byte stream using a shared key.- Parameters:
sharedKey- The shared key used for the encryption.input- The byte stream to be encrypted.output- The encrypted output stream.- Throws:
IOException- Unable to encrypt the stream.
-
decryptStream
public final void decryptStream(SecretKey sharedKey, InputStream input, OutputStream output) throws IOException This method decrypts a byte stream from an encrypted byte stream.- Parameters:
sharedKey- The shared key used for the encryption.input- The encrypted byte stream.output- The decrypted byte stream.- Throws:
IOException- Unable to decrypt the stream.
-
encryptionOutputStream
public abstract CipherOutputStream encryptionOutputStream(SecretKey sharedKey, OutputStream output) throws IOException This method generates an output stream that performs encryption on another output stream.- Parameters:
sharedKey- The shared key used for the encryption.output- The output stream to be encrypted.- Returns:
- The encrypting output stream.
- Throws:
IOException- Unable to create an encryption output stream.
-
decryptionInputStream
public abstract CipherInputStream decryptionInputStream(SecretKey sharedKey, InputStream input) throws IOException This method generates an input stream that performs decryption on another input stream.- Parameters:
sharedKey- The shared key used for the encryption.input- The input stream to be decrypted.- Returns:
- The decrypting input stream.
- Throws:
IOException- Unable to create a decryption input stream.
-
getAsymmetricKeyType
This method returns the asymmetric key type string.- Returns:
- The asymmetric key type string.
-
getAsymmetricKeySize
public abstract int getAsymmetricKeySize()This method returns the asymmetric key size.- Returns:
- The asymmetric key size.
-
getAsymmetricSignatureAlgorithm
This method returns the asymmetric signature algorithm used by this cryptex.- Returns:
- The name of the algorithm.
-
getAsymmetricEncryptionAlgorithm
This method returns the asymmetric encryption algorithm used by this cryptex.- Returns:
- The name of the algorithm.
-
generateKeyPair
This method generates a new public/private key pair.- Returns:
- The new key pair.
-
encodePublicKey
This method encodes a public key into a PEM string.- Parameters:
key- The public key.- Returns:
- The corresponding PEM string.
-
encodePublicKey
This method encodes a public key into a PEM string with a prepended indentation string.- Parameters:
key- The public key.indentation- The indentation string that should be prepended to each line of the output.- Returns:
- The corresponding PEM string.
-
decodePublicKey
This method decodes public key from a PEM string.- Parameters:
pem- The PEM string for the public key.- Returns:
- The corresponding key.
-
getPasswordEncodingType
This method returns the password encoding type used for password based encryption (PBE) used by this cryptex.- Returns:
- The type of the password encoding.
-
generatePasswordKey
This method generates a password (secret) key to be used for encrypting a private key.- Parameters:
password- The password.- Returns:
- The password key.
-
encodePrivateKey
This method encodes a private key into a PEM string.- Parameters:
privateKey- The private key.passwordKey- The secret key, generated from a password, to be used to encrypt the private key.- Returns:
- The corresponding PEM string.
-
encodePrivateKey
public abstract String encodePrivateKey(PrivateKey privateKey, SecretKey passwordKey, String indentation) This method encodes a private key into a PEM string with a prepended indentation string.- Parameters:
privateKey- The private key.passwordKey- The secret key, generated from a password, to be used to encrypt the private key.indentation- The indentation string that should be prepended to each line of the output.- Returns:
- The corresponding PEM string.
-
decodePrivateKey
This method decodes private key from a PEM string.- Parameters:
pem- The PEM string for the private key.passwordKey- The secret key, generated from a password, to be used to decrypt the private key.- Returns:
- The corresponding key.
-
signBytes
This method signs a byte array.- Parameters:
privateKey- The private key used for signing.bytes- The byte array to be signed.- Returns:
- The resulting signature.
-
bytesAreValid
This method checks to see if the signature for a signed byte array is valid.- Parameters:
certificate- The certificate containing the matching public key for the private key that signed the bytes.bytes- The byte array to be signed.signature- The signature to be validated.- Returns:
- Whether or not the signature matches the byte array.
-