Package gov.usgs.util
Class CryptoUtils
- java.lang.Object
-
- gov.usgs.util.CryptoUtils
-
public class CryptoUtils extends java.lang.ObjectEncryption and signing utilities.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCryptoUtils.VersionSignature versions.
-
Field Summary
Fields Modifier and Type Field Description static intAES_128Number of bits for AES 128 bit key.static intAES_256Number of bits for AES 256 bit key.static java.lang.StringAES_ALGORITHMAlgorithm used by AES keys and ciphers.static intDSA_1024Number of bits for DSA 1024 bit key.static java.lang.StringDSA_ALGORITHMAlgorithm used by DSA keys.static java.lang.StringDSA_SIGNATURE_ALGORITHMAlgorithm used for signature with DSA key.static intRSA_2048Number of bits for RSA 2048 bit key.static intRSA_4096Number of bits for RSA 4096 bit key.static java.lang.StringRSA_ALGORITHMAlgorithm used by RSA keys and ciphers.static java.lang.StringRSA_SIGNATURE_ALGORITHMAlgorithm used for signature with RSA key.static java.lang.StringSIGNATURE_V2_DSA_ALGORITHMv2 Algorithm for DSA signaturestatic java.lang.StringSIGNATURE_V2_RSA_ALGORITHMv2 Algorithm for RSA signature
-
Constructor Summary
Constructors Constructor Description CryptoUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidconfigureSignature(java.security.Key key, CryptoUtils.Version version, java.security.Signature signature)static byte[]convertPEMToDER(java.lang.String string)Read a PEM format.static byte[]decrypt(java.security.Key key, byte[] toDecrypt)A convenience method to decrypt a byte array.static byte[]encrypt(java.security.Key key, byte[] toEncrypt)A convenience method to encrypt a byte array.static java.security.KeygenerateAESKey(int bits)Generate a new symmetric encryption key.static java.security.KeyPairgenerateDSAKeyPair(int bits)Generate a new asymmetric signature key pair.static java.security.KeyPairgenerateRSAKeyPair(int bits)Generate a new asymmetric encryption key pair.static javax.crypto.CiphergetDecryptCipher(java.security.Key key)Create and initialize a decrypting cipher using key.getAlgorithm as transformation.static javax.crypto.CiphergetEncryptCipher(java.security.Key key)Create and initialize an encrypting cipher using key.getAlgorithm() as transformation.static java.security.SignaturegetSignature(java.security.Key key, CryptoUtils.Version version)Create and configure a signature object based on key type.static voidprocessCipherStream(javax.crypto.Cipher cipher, java.io.InputStream in, java.io.OutputStream out)Process a data stream using a cipher.static java.security.cert.CertificatereadCertificate(byte[] bytes)Read a X509 encoded certificate.static byte[]readDERString(java.nio.ByteBuffer buf)This method reads a DER encoded byte string from a ByteBuffer.static java.security.PrivateKeyreadOpenSSHPrivateKey(byte[] bytes, java.lang.String password)Read an OpenSSH private key from a stream.static java.security.PublicKeyreadOpenSSHPublicKey(byte[] bytes)Read an OpenSSH PublicKey from a stream.static java.security.PrivateKeyreadPrivateKey(byte[] bytes)Read a PKCS#8 encoded private key.static java.security.PublicKeyreadPublicKey(byte[] bytes)Read a X509 encoded public key.static java.lang.Stringsign(java.security.PrivateKey privateKey, byte[] data)A convenience method that chooses a signature algorithm based on the key type.static java.lang.Stringsign(java.security.PrivateKey privateKey, byte[] data, CryptoUtils.Version version)Generate a signature.static booleanverify(java.security.PublicKey publicKey, byte[] data, java.lang.String allegedSignature)A convenience method that chooses a signature algorithm based on the key type.static booleanverify(java.security.PublicKey publicKey, byte[] data, java.lang.String allegedSignature, CryptoUtils.Version version)Verify a signature.
-
-
-
Field Detail
-
AES_ALGORITHM
public static final java.lang.String AES_ALGORITHM
Algorithm used by AES keys and ciphers.- See Also:
- Constant Field Values
-
AES_128
public static final int AES_128
Number of bits for AES 128 bit key.- See Also:
- Constant Field Values
-
AES_256
public static final int AES_256
Number of bits for AES 256 bit key.- See Also:
- Constant Field Values
-
DSA_ALGORITHM
public static final java.lang.String DSA_ALGORITHM
Algorithm used by DSA keys.- See Also:
- Constant Field Values
-
DSA_SIGNATURE_ALGORITHM
public static final java.lang.String DSA_SIGNATURE_ALGORITHM
Algorithm used for signature with DSA key.- See Also:
- Constant Field Values
-
DSA_1024
public static final int DSA_1024
Number of bits for DSA 1024 bit key.- See Also:
- Constant Field Values
-
RSA_ALGORITHM
public static final java.lang.String RSA_ALGORITHM
Algorithm used by RSA keys and ciphers.- See Also:
- Constant Field Values
-
RSA_SIGNATURE_ALGORITHM
public static final java.lang.String RSA_SIGNATURE_ALGORITHM
Algorithm used for signature with RSA key.- See Also:
- Constant Field Values
-
RSA_2048
public static final int RSA_2048
Number of bits for RSA 2048 bit key.- See Also:
- Constant Field Values
-
RSA_4096
public static final int RSA_4096
Number of bits for RSA 4096 bit key.- See Also:
- Constant Field Values
-
SIGNATURE_V2_DSA_ALGORITHM
public static final java.lang.String SIGNATURE_V2_DSA_ALGORITHM
v2 Algorithm for DSA signature- See Also:
- Constant Field Values
-
SIGNATURE_V2_RSA_ALGORITHM
public static final java.lang.String SIGNATURE_V2_RSA_ALGORITHM
v2 Algorithm for RSA signature- See Also:
- Constant Field Values
-
-
Method Detail
-
processCipherStream
public static void processCipherStream(javax.crypto.Cipher cipher, java.io.InputStream in, java.io.OutputStream out) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, java.io.IOExceptionProcess a data stream using a cipher. If cipher is initialized to ENCRYPT_MODE, the input stream will be encrypted. If cipher is initialized to DECRYPT_MODE, the input stream will be decrypted.- Parameters:
cipher- an initialized cipher.in- the data to encrypt.out- where encrypted data is written.- Throws:
java.security.NoSuchAlgorithmException- if invalid encrypt/decrypt algorithmjavax.crypto.NoSuchPaddingException- on padding errorjava.security.InvalidKeyException- if key is not RSA or DSA.java.io.IOException- if IO error occurs
-
getEncryptCipher
public static javax.crypto.Cipher getEncryptCipher(java.security.Key key) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyExceptionCreate and initialize an encrypting cipher using key.getAlgorithm() as transformation.- Parameters:
key- the key used to encrypt.- Returns:
- a cipher used to encrypt.
- Throws:
java.security.NoSuchAlgorithmException- on invalid algorithmjavax.crypto.NoSuchPaddingException- on invalid paddingjava.security.InvalidKeyException- if key is not RSA or DSA.
-
getDecryptCipher
public static javax.crypto.Cipher getDecryptCipher(java.security.Key key) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyExceptionCreate and initialize a decrypting cipher using key.getAlgorithm as transformation.- Parameters:
key- the key used to decrypt.- Returns:
- a cipher used to decrypt.
- Throws:
java.security.NoSuchAlgorithmException- on invalid algorithmjavax.crypto.NoSuchPaddingException- on invalid paddingjava.security.InvalidKeyException- if key is not RSA or DSA.
-
getSignature
public static java.security.Signature getSignature(java.security.Key key, CryptoUtils.Version version) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, java.security.SignatureExceptionCreate and configure a signature object based on key type.- Parameters:
key- Key used to sign/verify.version- SIGNATURE_V1 or SIGNATURE_V2- Returns:
- Configured Signature object
- Throws:
java.security.InvalidKeyException- if key is not RSA or DSA.java.security.NoSuchAlgorithmException- on invalid algorithmjava.security.SignatureException- on signature error
-
configureSignature
public static void configureSignature(java.security.Key key, CryptoUtils.Version version, java.security.Signature signature) throws java.security.InvalidAlgorithmParameterException- Parameters:
key- Key used to sign/verify.version- SIGNATURE_V1 or SIGNATURE_V2signature- A signature- Throws:
java.security.InvalidAlgorithmParameterException- on invalid or inappropriate algorithm parameters
-
sign
public static java.lang.String sign(java.security.PrivateKey privateKey, byte[] data) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, java.security.SignatureExceptionA convenience method that chooses a signature algorithm based on the key type. Works with DSA and RSA keys.- Parameters:
privateKey- a private keydata- data to sign- Returns:
- signature as hex encoded string
- Throws:
java.security.InvalidAlgorithmParameterException- on invalid or inappropriate algorithm parametersjava.security.InvalidKeyException- if key is not RSA or DSA.java.security.NoSuchAlgorithmException- on invalid algorithmjava.security.SignatureException- on signature error
-
sign
public static java.lang.String sign(java.security.PrivateKey privateKey, byte[] data, CryptoUtils.Version version) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, java.security.SignatureExceptionGenerate a signature.- Parameters:
privateKey- private key to use, should be acceptable by signature instance.data- data/hash to sign.version- the signature version.- Returns:
- signature as hex encoded string.
- Throws:
java.security.InvalidAlgorithmParameterException- on invalid or inappropriate algorithm parametersjava.security.NoSuchAlgorithmException- on invalid algorithmjava.security.InvalidKeyException- if key is not RSA or DSA.java.security.SignatureException- on signature error
-
verify
public static boolean verify(java.security.PublicKey publicKey, byte[] data, java.lang.String allegedSignature) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, java.security.SignatureExceptionA convenience method that chooses a signature algorithm based on the key type. Works with DSA and RSA keys.- Parameters:
publicKey- public key corresponding to private key that generated signature.data- data/hash to verifyallegedSignature- to try and verify with- Returns:
- boolean
- Throws:
java.security.InvalidAlgorithmParameterException- on invalid or inappropriate algorithm parametersjava.security.InvalidKeyException- if key is not RSA or DSA.java.security.NoSuchAlgorithmException- on invalid algorithmjava.security.SignatureException- on signature error
-
verify
public static boolean verify(java.security.PublicKey publicKey, byte[] data, java.lang.String allegedSignature, CryptoUtils.Version version) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, java.security.SignatureExceptionVerify a signature.- Parameters:
publicKey- public key corresponding to private key that generated signature.data- the data/hash that was signed.allegedSignature- the signature being verified.version- signature version.- Returns:
- true if computed signature matches allegedSignature.
- Throws:
java.security.InvalidAlgorithmParameterException- on invalid or inappropriate algorithm parametersjava.security.NoSuchAlgorithmException- on invalid algorithmjava.security.InvalidKeyException- if key is not RSA or DSA.java.security.SignatureException- on signature error
-
encrypt
public static byte[] encrypt(java.security.Key key, byte[] toEncrypt) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.lang.IllegalArgumentException, java.io.IOExceptionA convenience method to encrypt a byte array.- Parameters:
key- a key that can be used to encrypt.toEncrypt- the data to encrypt.- Returns:
- encrypted byte array.
- Throws:
java.security.InvalidKeyException- if key is not RSA or DSA.java.security.NoSuchAlgorithmException- on invalid algorithmjavax.crypto.NoSuchPaddingException- on invalid paddingjava.lang.IllegalArgumentException- on illegal args passed to functionjava.io.IOException- on IO error
-
decrypt
public static byte[] decrypt(java.security.Key key, byte[] toDecrypt) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.lang.IllegalArgumentException, java.io.IOExceptionA convenience method to decrypt a byte array.- Parameters:
key- a key that can be used to decrypt.toDecrypt- the data to decrypt.- Returns:
- decrypted byte array.
- Throws:
java.security.InvalidKeyException- if key is not RSA or DSA.java.security.NoSuchAlgorithmException- on invalid algorithmjavax.crypto.NoSuchPaddingException- on invalid paddingjava.lang.IllegalArgumentException- on illegal args passed to functionjava.io.IOException- on IO error
-
generateAESKey
public static java.security.Key generateAESKey(int bits) throws java.security.NoSuchAlgorithmExceptionGenerate a new symmetric encryption key.- Parameters:
bits- how many bits. This should be AES_128 or AES256.- Returns:
- generated AES key.
- Throws:
java.security.NoSuchAlgorithmException- on invalid algorithm
-
generateRSAKeyPair
public static java.security.KeyPair generateRSAKeyPair(int bits) throws java.security.NoSuchAlgorithmExceptionGenerate a new asymmetric encryption key pair.- Parameters:
bits- how many bits. Must be a valid RSA size.- Returns:
- generated RSA key pair.
- Throws:
java.security.NoSuchAlgorithmException- on invalid algorithm
-
generateDSAKeyPair
public static java.security.KeyPair generateDSAKeyPair(int bits) throws java.security.NoSuchAlgorithmExceptionGenerate a new asymmetric signature key pair.- Parameters:
bits- how many bits. Must be a valid DSA size.- Returns:
- generated DSA key pair.
- Throws:
java.security.NoSuchAlgorithmException- on invalid algorithm
-
readCertificate
public static java.security.cert.Certificate readCertificate(byte[] bytes) throws java.security.cert.CertificateException, java.io.IOExceptionRead a X509 encoded certificate. May be DER or PEM encoded.- Parameters:
bytes- the certificate data as a byte array.- Returns:
- parsed certificate.
- Throws:
java.security.cert.CertificateException- on certificate issuejava.io.IOException- on IO error
-
readPublicKey
public static java.security.PublicKey readPublicKey(byte[] bytes) throws java.io.IOException, java.security.NoSuchAlgorithmExceptionRead a X509 encoded public key. May be DER or PEM encoded.- Parameters:
bytes- the key data as a byte array.- Returns:
- parsed public key.
- Throws:
java.io.IOException- on IO errorjava.security.NoSuchAlgorithmException- on invalid algorithm
-
readPrivateKey
public static java.security.PrivateKey readPrivateKey(byte[] bytes) throws java.io.IOException, java.security.NoSuchAlgorithmExceptionRead a PKCS#8 encoded private key. May be DER or PEM encoded.- Parameters:
bytes- the key data as a byte array.- Returns:
- parsed private key.
- Throws:
java.io.IOException- on IO errorjava.security.NoSuchAlgorithmException- on invalid algorithm
-
readOpenSSHPrivateKey
public static java.security.PrivateKey readOpenSSHPrivateKey(byte[] bytes, java.lang.String password) throws java.io.IOException, java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecExceptionRead an OpenSSH private key from a stream.- Parameters:
bytes- the byte array containing an OpenSSH private key.password- password if the key is encrypted.- Returns:
- decoded PrivateKey.
- Throws:
java.io.IOException- on IO errorjava.security.spec.InvalidKeySpecException- when key has invalid specificationsjava.security.NoSuchAlgorithmException- on invalid algorithm
-
readOpenSSHPublicKey
public static java.security.PublicKey readOpenSSHPublicKey(byte[] bytes) throws java.io.IOException, java.security.spec.InvalidKeySpecException, java.security.NoSuchAlgorithmExceptionRead an OpenSSH PublicKey from a stream.- Parameters:
bytes- bytes to read.- Returns:
- a publicKey
- Throws:
java.io.IOException- on IO errorjava.security.NoSuchAlgorithmException- on invalid algorithmjava.security.spec.InvalidKeySpecException- when key has invalid specifications
-
readDERString
public static byte[] readDERString(java.nio.ByteBuffer buf)
This method reads a DER encoded byte string from a ByteBuffer. A DER encoded string has length = 4 bytes big-endian integer
string = length bytes- Parameters:
buf- buffer containing DER encoded bytes.- Returns:
- bytes the decoded bytes.
-
convertPEMToDER
public static byte[] convertPEMToDER(java.lang.String string) throws java.io.IOExceptionRead a PEM format. This does not currently support encrypted PEM formats.- Parameters:
string- string containing PEM formatted data.- Returns:
- DER formatted data.
- Throws:
java.io.IOException- on IO error
-
-