Class EncryptionUtils

java.lang.Object
rs.baselib.crypto.EncryptionUtils

public class EncryptionUtils
extends java.lang.Object
Basic function for helping in encryption.
Author:
ralph
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static int DEFAULT_ITERATIONS
    The default number of iterations to be executed when creating the encrypting algorithm.
    static java.lang.String DEFAULT_SECRET_KEY_TYPE
    The default secret key type to be generated from a password.
    static java.lang.String PASSWORD_CHARS
    The default number of iterations to be executed when creating the encrypting algorithm.
  • Constructor Summary

    Constructors 
    Constructor Description
    EncryptionUtils()  
  • Method Summary

    Modifier and Type Method Description
    static byte[] createMD5​(byte[] b)
    Creates an MD5 hash from the byte array.
    static byte[] createMD5​(java.lang.String s)
    Creates an MD5 hash from the string.
    static byte[] crypt​(byte[] bytes, javax.crypto.Cipher cipher)
    Transforms all bytes from the input array with a crypt alorithm and returns the transformed bytes.
    static byte[] crypt​(byte[] bytes, javax.crypto.Cipher cipher, int blockSize)
    Transforms all bytes from the input array with a crypt alorithm and returns the transformed bytes.
    static byte[] decodeBase64​(java.lang.String s)
    Decodes a BASE64 string into bytes.
    static java.security.PrivateKey decodeBase64PrivateKey​(java.lang.String s)
    Decodes a private DSA key from the BASE64 representation.
    static java.security.PrivateKey decodeBase64PrivateKey​(java.lang.String s, java.lang.String algorithm)
    Decodes a private key from the BASE64 representation.
    static java.security.PublicKey decodeBase64PublicKey​(java.lang.String s)
    Decodes a public key (DSA) from the BASE64 representation.
    static java.security.PublicKey decodeBase64PublicKey​(java.lang.String s, java.lang.String algorithm)
    Decodes a public key from the BASE64 representation.
    static java.lang.String decodeBase64ToString​(java.lang.String s)
    Decodes a BASE64 string and generates the original string from the result.
    static java.lang.String decodeBase64ToString​(java.lang.String s, java.nio.charset.Charset charset)
    Decodes a BASE64 string and generates the original string from the result.
    static java.lang.String encodeBase64​(byte[] b)
    Encodes a byte array into its BASE64 representation.
    static java.lang.String encodeBase64​(java.lang.String s)
    Encodes a string into its BASE64 representation.
    static java.lang.String encodeBase64​(java.lang.String s, java.nio.charset.Charset charset)
    Encodes a string into its BASE64 representation.
    static java.lang.String encodeBase64​(java.security.PrivateKey key)
    Encodes the private key into BASE64 representation.
    static java.lang.String encodeBase64​(java.security.PublicKey key)
    Encodes the public key into BASE64 representation.
    static java.lang.String encodeBase64Pkcs8​(java.security.PrivateKey key)
    Encodes the private key into BASE64 PKCS8 representation.
    static java.lang.String encodeBase64X509​(java.security.PublicKey key)
    Encodes the public key into BASE64 X.509 representation.
    static java.security.KeyPair generateKey()
    Generates a 512 byte RSA key pair.
    static java.security.KeyPair generateKey​(byte[] seed)
    Generates a public/private key pair.
    static java.security.KeyPair generateKey​(int keySize)
    Generates a RSA key pair of given size
    static java.security.KeyPair generateKey​(java.lang.String seed)
    Generates a public/private key pair.
    static java.security.KeyPair generateKey​(java.lang.String algorithm, int keySize)
    Generates a key pair.
    static java.security.KeyPair generateKey​(java.lang.String seed, java.nio.charset.Charset charset)
    Generates a public/private key pair.
    static javax.crypto.spec.PBEParameterSpec generateParamSpec()
    Creates a PBE Parameter specification randomly.
    static javax.crypto.spec.PBEParameterSpec generateParamSpec​(byte[] salt, int iterationCount)
    Creates a PBE Parameter Spec for encryption algorithm.
    static java.lang.String generatePassword()
    Creates a random password.
    static java.lang.String generatePassword​(int length)
    Creates a random password.
    static java.lang.String generatePassword​(java.lang.String allowedChars)
    Creates a random password.
    static java.lang.String generatePassword​(java.lang.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 java.security.Provider[] getKeyPairGenerators()
    Returns all security providers.
    static java.security.spec.KeySpec getKeySpec​(java.security.Key key)
    Creates a key specification.
    static java.security.KeyStore getKeyStore​(java.io.InputStream in, char[] password)
    Load the default keystore type.
    static java.security.KeyStore getKeyStore​(java.lang.String filename, char[] password)
    Load the default keystore type.
    static java.security.KeyStore getKeyStore​(java.lang.String type, java.io.InputStream in, char[] password)
    Load the given keystore.
    static java.security.KeyStore getKeyStore​(java.lang.String type, java.lang.String filename, char[] password)
    Load the default keystore type.
    static javax.crypto.SecretKey getSecretKey​(int iterationCount, java.lang.String passPhrase, byte[] salt)  
    static java.security.PrivateKey loadPrivateKey​(java.io.File file, java.lang.String algorithm)
    Instantiates a PrivateKey from given file.
    static java.security.PrivateKey loadPrivateKey​(java.io.InputStream in, java.lang.String algorithm)
    Instantiates a PrivateKey from given stream.
    static java.security.PrivateKey loadPrivateKey​(java.io.Reader in, java.lang.String algorithm)
    Instantiates a PrivateKey from given reader.
    static java.security.PrivateKey loadPrivateKey​(java.lang.String filename, java.lang.String algorithm)
    Instantiates a PrivateKey from given file.
    static java.security.PrivateKey loadPrivateKey​(java.net.URL url, java.lang.String algorithm)
    Instantiates a PrivateKey from given URL.
    static java.security.PublicKey loadPublicKey​(java.io.File file, java.lang.String algorithm)
    Instantiates a PublicKey from given file.
    static java.security.PublicKey loadPublicKey​(java.io.InputStream in, java.lang.String algorithm)
    Instantiates a PublicKey from given stream.
    static java.security.PublicKey loadPublicKey​(java.io.Reader in, java.lang.String algorithm)
    Instantiates a PublicKey from given reader.
    static java.security.PublicKey loadPublicKey​(java.lang.String filename, java.lang.String algorithm)
    Instantiates a PublicKey from given file.
    static java.security.PublicKey loadPublicKey​(java.net.URL url, java.lang.String algorithm)
    Instantiates a PublicKey from given URL.
    static void save​(java.io.File file, java.security.PrivateKey key)
    Saves a PrivateKey into given file.
    static void save​(java.io.File file, java.security.PublicKey key)
    Saves a PublicKey into given file.
    static void save​(java.io.OutputStream out, java.security.PrivateKey key)
    Saves a PrivateKey into given stream.
    static void save​(java.io.OutputStream out, java.security.PublicKey key)
    Saves a PublicKey into given stream.
    static void save​(java.io.PrintWriter out, java.security.PrivateKey key)
    Saves a PrivateKey into given writer.
    static void save​(java.io.PrintWriter out, java.security.PublicKey key)
    Saves a PublicKey into given writer.
    static void save​(java.lang.String filename, java.security.PrivateKey key)
    Saves a PrivateKey into given file.
    static void save​(java.lang.String filename, java.security.PublicKey key)
    Saves a PublicKey into given file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • getKeySpec

      public static java.security.spec.KeySpec getKeySpec​(java.security.Key key) throws java.security.spec.InvalidKeySpecException, java.security.NoSuchAlgorithmException
      Creates a key specification.
      Parameters:
      key - the key
      Returns:
      the PBE param spec
      Throws:
      java.security.spec.InvalidKeySpecException - when the key spec is invalid
      java.security.NoSuchAlgorithmException - when the algorithm is invalid
    • generateParamSpec

      public static javax.crypto.spec.PBEParameterSpec generateParamSpec()
      Creates a PBE Parameter specification randomly.
      Returns:
      the PBE param spec
      See Also:
      generateParamSpec(byte[], int)
    • generateParamSpec

      public static javax.crypto.spec.PBEParameterSpec generateParamSpec​(byte[] salt, int iterationCount)
      Creates a PBE Parameter Spec for encryption algorithm.
      Parameters:
      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)
      Returns:
      the generated PBE random spec
      See Also:
      DEFAULT_ITERATIONS, generateSalt(long)
    • generateSalt

      public static byte[] generateSalt()
      Creates a random salt array.
      Returns:
      the random salt
    • generateSalt

      public static byte[] generateSalt​(long randomInit)
      Creates a random salt array.
      Parameters:
      randomInit - initializer
      Returns:
      the random salt
    • generateRandomBytes

      public static byte[] generateRandomBytes​(long randomInit, int size)
      Generate a random array of bytes
      Parameters:
      randomInit - initializer
      size - size of returned array
      Returns:
      random byte array
    • generatePassword

      public static java.lang.String generatePassword()
      Creates a random password.
      Returns:
      the random password
    • generatePassword

      public static java.lang.String generatePassword​(int length)
      Creates a random password.
      Parameters:
      length - length of password
      Returns:
      the random password
    • generatePassword

      public static java.lang.String generatePassword​(java.lang.String allowedChars)
      Creates a random password.
      Parameters:
      allowedChars - all characters allowed
      Returns:
      the random password
    • generatePassword

      public static java.lang.String generatePassword​(java.lang.String allowedChars, long randomInit, int length)
      Creates a random password.
      Parameters:
      allowedChars - allowedCharacters
      randomInit - initializer
      length - length of password
      Returns:
      the random password
    • getSecretKey

      public static javax.crypto.SecretKey getSecretKey​(int iterationCount, java.lang.String passPhrase, byte[] salt) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException
      Throws:
      java.security.NoSuchAlgorithmException
      java.security.spec.InvalidKeySpecException
    • generateKey

      public static java.security.KeyPair generateKey​(java.lang.String seed) throws java.security.NoSuchProviderException, java.security.NoSuchAlgorithmException
      Generates a public/private key pair.
      Parameters:
      seed - seed to be used.
      Returns:
      key pair
      Throws:
      java.security.NoSuchProviderException - when the algorithm provider does not exist
      java.security.NoSuchAlgorithmException - when the algorithm does not exist
    • generateKey

      public static java.security.KeyPair generateKey​(java.lang.String seed, java.nio.charset.Charset charset) throws java.security.NoSuchProviderException, java.security.NoSuchAlgorithmException
      Generates a public/private key pair.
      Parameters:
      seed - seed to be used.
      charset - the charset to be used for string encoding (null for default charset)
      Returns:
      key pair
      Throws:
      java.security.NoSuchProviderException - when the algorithm provider does not exist
      java.security.NoSuchAlgorithmException - when the algorithm does not exist
      Since:
      1.2.5
    • generateKey

      public static java.security.KeyPair generateKey​(byte[] seed) throws java.security.NoSuchProviderException, java.security.NoSuchAlgorithmException
      Generates a public/private key pair.
      Parameters:
      seed - seed to be used.
      Returns:
      key pair
      Throws:
      java.security.NoSuchProviderException - when the algorithm provider does not exist
      java.security.NoSuchAlgorithmException - when the algorithm does not exist
    • generateKey

      public static java.security.KeyPair generateKey() throws java.security.NoSuchAlgorithmException
      Generates a 512 byte RSA key pair.
      Returns:
      the key pair
      Throws:
      java.security.NoSuchAlgorithmException - when the algorithm does not exist
    • generateKey

      public static java.security.KeyPair generateKey​(int keySize) throws java.security.NoSuchAlgorithmException
      Generates a RSA key pair of given size
      Parameters:
      keySize - the key size
      Returns:
      the key pair
      Throws:
      java.security.NoSuchAlgorithmException - when the algorithm does not exist
    • generateKey

      public static java.security.KeyPair generateKey​(java.lang.String algorithm, int keySize) throws java.security.NoSuchAlgorithmException
      Generates a key pair.
      Parameters:
      algorithm - algorithm, e.g. "RSA"
      keySize - teh key size
      Returns:
      the key pair
      Throws:
      java.security.NoSuchAlgorithmException - when the algorithm does not exist
    • getKeyPairGenerators

      public static java.security.Provider[] getKeyPairGenerators()
      Returns all security providers.
      Returns:
      all security providers
    • encodeBase64

      public static java.lang.String encodeBase64​(java.security.PublicKey key)
      Encodes the public key into BASE64 representation.
      Parameters:
      key - public key
      Returns:
      BASE64 representation of key
    • encodeBase64X509

      public static java.lang.String encodeBase64X509​(java.security.PublicKey key)
      Encodes the public key into BASE64 X.509 representation.
      Parameters:
      key - public key
      Returns:
      BASE64 representation of key
    • encodeBase64

      public static java.lang.String encodeBase64​(java.security.PrivateKey key)
      Encodes the private key into BASE64 representation.
      Parameters:
      key - private key
      Returns:
      BASE64 representation of key
    • encodeBase64Pkcs8

      public static java.lang.String encodeBase64Pkcs8​(java.security.PrivateKey key)
      Encodes the private key into BASE64 PKCS8 representation.
      Parameters:
      key - private key
      Returns:
      BASE64 representation of key
    • decodeBase64PublicKey

      public static java.security.PublicKey decodeBase64PublicKey​(java.lang.String s) throws java.security.spec.InvalidKeySpecException, java.security.NoSuchAlgorithmException
      Decodes a public key (DSA) from the BASE64 representation.
      Parameters:
      s - BASE64 representation
      Returns:
      public DSA key
      Throws:
      java.security.spec.InvalidKeySpecException - - when the key spec is invalid
      java.security.NoSuchAlgorithmException - - when the algorithm is invalid
    • decodeBase64PublicKey

      public static java.security.PublicKey decodeBase64PublicKey​(java.lang.String s, java.lang.String algorithm) throws java.security.spec.InvalidKeySpecException, java.security.NoSuchAlgorithmException
      Decodes a public key from the BASE64 representation.
      Parameters:
      s - BASE64 representation
      algorithm - name of algorithm
      Returns:
      public key
      Throws:
      java.security.spec.InvalidKeySpecException - - when the key spec is invalid
      java.security.NoSuchAlgorithmException - - when the algorithm is invalid
    • decodeBase64PrivateKey

      public static java.security.PrivateKey decodeBase64PrivateKey​(java.lang.String s) throws java.security.spec.InvalidKeySpecException, java.security.NoSuchAlgorithmException
      Decodes a private DSA key from the BASE64 representation.
      Parameters:
      s - BASE64 representation
      Returns:
      public key
      Throws:
      java.security.spec.InvalidKeySpecException - - when the key spec is invalid
      java.security.NoSuchAlgorithmException - - when the algorithm is invalid
    • decodeBase64PrivateKey

      public static java.security.PrivateKey decodeBase64PrivateKey​(java.lang.String s, java.lang.String algorithm) throws java.security.spec.InvalidKeySpecException, java.security.NoSuchAlgorithmException
      Decodes a private key from the BASE64 representation.
      Parameters:
      s - BASE64 representation
      algorithm - name of algorithm
      Returns:
      public key
      Throws:
      java.security.spec.InvalidKeySpecException - - when the key spec is invalid
      java.security.NoSuchAlgorithmException - - when the algorithm is invalid
    • decodeBase64ToString

      public static java.lang.String decodeBase64ToString​(java.lang.String s)
      Decodes a BASE64 string and generates the original string from the result.
      Parameters:
      s - BASE64 encoded string
      Returns:
      original string
      See Also:
      encodeBase64(String)
    • decodeBase64ToString

      public static java.lang.String decodeBase64ToString​(java.lang.String s, java.nio.charset.Charset charset)
      Decodes a BASE64 string and generates the original string from the result.
      Parameters:
      s - BASE64 encoded string
      charset - the character set to be used for constructing the string (can be null for default charset)
      Returns:
      original string
      Since:
      1.2.5
      See Also:
      encodeBase64(String)
    • decodeBase64

      public static byte[] decodeBase64​(java.lang.String s)
      Decodes a BASE64 string into bytes.
      Parameters:
      s - BASE64 encoded string
      Returns:
      bytes that were encoded.
    • encodeBase64

      public static java.lang.String encodeBase64​(java.lang.String s)
      Encodes a string into its BASE64 representation. The string is actually split into its bytes and then BASE64 encoded.
      Parameters:
      s - string to encode
      Returns:
      BASE64 representation
    • encodeBase64

      public static java.lang.String encodeBase64​(java.lang.String s, java.nio.charset.Charset charset)
      Encodes a string into its BASE64 representation. The string is actually split into its bytes and then BASE64 encoded.
      Parameters:
      s - string to encode
      charset - the charset to be used for string encoding (null for default charset)
      Returns:
      BASE64 representation
      Since:
      1.2.5
    • encodeBase64

      public static java.lang.String encodeBase64​(byte[] b)
      Encodes a byte array into its BASE64 representation.
      Parameters:
      b - bytes to encode
      Returns:
      BASE64 representation
    • getKeyStore

      public static java.security.KeyStore getKeyStore​(java.lang.String filename, char[] password) throws java.io.IOException
      Load the default keystore type.
      Parameters:
      filename - filename
      password - password
      Returns:
      the key store loaded
      Throws:
      java.io.IOException - when the key store cannot be opened
    • getKeyStore

      public static java.security.KeyStore getKeyStore​(java.lang.String type, java.lang.String filename, char[] password) throws java.io.IOException
      Load the default keystore type.
      Parameters:
      type - type of key store
      filename - filename
      password - password
      Returns:
      the key store loaded
      Throws:
      java.io.IOException - when the key store cannot be opened
    • getKeyStore

      public static java.security.KeyStore getKeyStore​(java.io.InputStream in, char[] password) throws java.io.IOException
      Load the default keystore type.
      Parameters:
      in - input stream
      password - password
      Returns:
      the key store loaded
      Throws:
      java.io.IOException - when the key store cannot be opened
    • getKeyStore

      public static java.security.KeyStore getKeyStore​(java.lang.String type, java.io.InputStream in, char[] password) throws java.io.IOException
      Load the given keystore.
      Parameters:
      type - of keystore
      in - input stream
      password - password
      Returns:
      the key store loaded
      Throws:
      java.io.IOException - when the key store cannot be opened
    • crypt

      public static byte[] crypt​(byte[] bytes, javax.crypto.Cipher cipher) throws java.io.IOException, java.security.GeneralSecurityException
      Transforms all bytes from the input array with a crypt alorithm and returns the transformed bytes.
      Parameters:
      bytes - bytes to crypt
      cipher - crypt alorithm used
      Returns:
      crypted bytes
      Throws:
      java.io.IOException - when crypting cannot be performed due to I/O problems
      java.security.GeneralSecurityException - when crypting cannot be performed due security problems
    • crypt

      public static byte[] crypt​(byte[] bytes, javax.crypto.Cipher cipher, int blockSize) throws java.io.IOException, java.security.GeneralSecurityException
      Transforms all bytes from the input array with a crypt alorithm and returns the transformed bytes.
      Parameters:
      bytes - bytes to crypt
      cipher - crypt alorithm used
      blockSize - block size to be applied (or 0 if none)
      Returns:
      crypted bytes
      Throws:
      java.io.IOException - when crypting cannot be performed due to I/O problems
      java.security.GeneralSecurityException - when crypting cannot be performed due security problems
    • createMD5

      public static byte[] createMD5​(java.lang.String s)
      Creates an MD5 hash from the string.
      Parameters:
      s - string to create the hash from
      Returns:
      the MD5 hash
    • createMD5

      public static byte[] createMD5​(byte[] b)
      Creates an MD5 hash from the byte array.
      Parameters:
      b - bytes to create the hash from
      Returns:
      the MD5 hash
    • loadPrivateKey

      public static java.security.PrivateKey loadPrivateKey​(java.io.File file, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PrivateKey from given file.
      Parameters:
      file - file where private key is stored in BASE64 PKCS8 encoding
      algorithm - algorithm that the key used
      Returns:
      the private key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPrivateKey

      public static java.security.PrivateKey loadPrivateKey​(java.lang.String filename, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PrivateKey from given file.
      Parameters:
      filename - file where private key is stored in BASE64 PKCS8 encoding
      algorithm - algorithm that the key used
      Returns:
      the private key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPrivateKey

      public static java.security.PrivateKey loadPrivateKey​(java.net.URL url, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PrivateKey from given URL.
      Parameters:
      url - URL where private key is stored in BASE64 PKCS8 encoding
      algorithm - algorithm that the key used
      Returns:
      the private key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPrivateKey

      public static java.security.PrivateKey loadPrivateKey​(java.io.InputStream in, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PrivateKey from given stream.
      Parameters:
      in - stream where private key is stored in BASE64 PKCS8 encoding
      algorithm - algorithm that the key used
      Returns:
      the private key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPrivateKey

      public static java.security.PrivateKey loadPrivateKey​(java.io.Reader in, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PrivateKey from given reader.
      Parameters:
      in - reader where private key is stored in BASE64 PKCS8 encoding
      algorithm - algorithm that the key used
      Returns:
      the private key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPublicKey

      public static java.security.PublicKey loadPublicKey​(java.io.File file, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PublicKey from given file.
      Parameters:
      file - file where private key is stored in BASE64 X.509 encoding
      algorithm - algorithm that the key used
      Returns:
      the public key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPublicKey

      public static java.security.PublicKey loadPublicKey​(java.lang.String filename, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PublicKey from given file.
      Parameters:
      filename - name of file where private key is stored in BASE64 X.509 encoding
      algorithm - algorithm that the key used
      Returns:
      the public key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPublicKey

      public static java.security.PublicKey loadPublicKey​(java.net.URL url, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PublicKey from given URL.
      Parameters:
      url - URL where private key is stored in BASE64 X.509 encoding
      algorithm - algorithm that the key used
      Returns:
      the public key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPublicKey

      public static java.security.PublicKey loadPublicKey​(java.io.InputStream in, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PublicKey from given stream.
      Parameters:
      in - stream where private key is stored in BASE64 X.509 encoding
      algorithm - algorithm that the key used
      Returns:
      the public key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • loadPublicKey

      public static java.security.PublicKey loadPublicKey​(java.io.Reader in, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, java.io.IOException
      Instantiates a PublicKey from given reader.
      Parameters:
      in - reader where private key is stored in BASE64 X.509 encoding
      algorithm - algorithm that the key used
      Returns:
      the public key
      Throws:
      java.security.NoSuchAlgorithmException - - when algorithm does not exist
      java.security.spec.InvalidKeySpecException - - when key spec cannot be created
      java.io.IOException - - when file cannot be opened
    • save

      public static void save​(java.io.File file, java.security.PrivateKey key) throws java.io.IOException
      Saves a PrivateKey into given file.
      Parameters:
      file - file where private key will be stored in BASE64 PKCS8 encoding
      key - key to be stored
      Throws:
      java.io.IOException - when key cannot be stored
    • save

      public static void save​(java.lang.String filename, java.security.PrivateKey key) throws java.io.IOException
      Saves a PrivateKey into given file.
      Parameters:
      filename - name of file where private key will be stored in BASE64 PKCS8 encoding
      key - key to be stored
      Throws:
      java.io.IOException - when key cannot be stored
    • save

      public static void save​(java.io.OutputStream out, java.security.PrivateKey key) throws java.io.IOException
      Saves a PrivateKey into given stream.
      Parameters:
      out - stream where private key will be stored in BASE64 PKCS8 encoding
      key - key to be stored
      Throws:
      java.io.IOException - when key cannot be stored
    • save

      public static void save​(java.io.PrintWriter out, java.security.PrivateKey key) throws java.io.IOException
      Saves a PrivateKey into given writer.
      Parameters:
      out - writer where private key will be stored in BASE64 PKCS8 encoding
      key - key to be stored
      Throws:
      java.io.IOException - when key cannot be stored
    • save

      public static void save​(java.io.File file, java.security.PublicKey key) throws java.io.IOException
      Saves a PublicKey into given file.
      Parameters:
      file - file where private key will be stored in BASE64 X.509 encoding
      key - key to be stored
      Throws:
      java.io.IOException - when key cannot be stored
    • save

      public static void save​(java.lang.String filename, java.security.PublicKey key) throws java.io.IOException
      Saves a PublicKey into given file.
      Parameters:
      filename - name of file where private key will be stored in BASE64 X.509 encoding
      key - key to be stored
      Throws:
      java.io.IOException - when key cannot be stored
    • save

      public static void save​(java.io.OutputStream out, java.security.PublicKey key) throws java.io.IOException
      Saves a PublicKey into given stream.
      Parameters:
      out - stream where private key will be stored in BASE64 X.509 encoding
      key - key to be stored
      Throws:
      java.io.IOException - when key cannot be stored
    • save

      public static void save​(java.io.PrintWriter out, java.security.PublicKey key) throws java.io.IOException
      Saves a PublicKey into given writer.
      Parameters:
      out - writer where private key will be stored in BASE64 X.509 encoding
      key - key to be stored
      Throws:
      java.io.IOException - when key cannot be stored