Package rs.baselib.crypto
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 intDEFAULT_ITERATIONSThe default number of iterations to be executed when creating the encrypting algorithm.static java.lang.StringDEFAULT_SECRET_KEY_TYPEThe default secret key type to be generated from a password.static java.lang.StringPASSWORD_CHARSThe 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.PrivateKeydecodeBase64PrivateKey(java.lang.String s)Decodes a private DSA key from the BASE64 representation.static java.security.PrivateKeydecodeBase64PrivateKey(java.lang.String s, java.lang.String algorithm)Decodes a private key from the BASE64 representation.static java.security.PublicKeydecodeBase64PublicKey(java.lang.String s)Decodes a public key (DSA) from the BASE64 representation.static java.security.PublicKeydecodeBase64PublicKey(java.lang.String s, java.lang.String algorithm)Decodes a public key from the BASE64 representation.static java.lang.StringdecodeBase64ToString(java.lang.String s)Decodes a BASE64 string and generates the original string from the result.static java.lang.StringdecodeBase64ToString(java.lang.String s, java.nio.charset.Charset charset)Decodes a BASE64 string and generates the original string from the result.static java.lang.StringencodeBase64(byte[] b)Encodes a byte array into its BASE64 representation.static java.lang.StringencodeBase64(java.lang.String s)Encodes a string into its BASE64 representation.static java.lang.StringencodeBase64(java.lang.String s, java.nio.charset.Charset charset)Encodes a string into its BASE64 representation.static java.lang.StringencodeBase64(java.security.PrivateKey key)Encodes the private key into BASE64 representation.static java.lang.StringencodeBase64(java.security.PublicKey key)Encodes the public key into BASE64 representation.static java.lang.StringencodeBase64Pkcs8(java.security.PrivateKey key)Encodes the private key into BASE64 PKCS8 representation.static java.lang.StringencodeBase64X509(java.security.PublicKey key)Encodes the public key into BASE64 X.509 representation.static java.security.KeyPairgenerateKey()Generates a 512 byte RSA key pair.static java.security.KeyPairgenerateKey(byte[] seed)Generates a public/private key pair.static java.security.KeyPairgenerateKey(int keySize)Generates a RSA key pair of given sizestatic java.security.KeyPairgenerateKey(java.lang.String seed)Generates a public/private key pair.static java.security.KeyPairgenerateKey(java.lang.String algorithm, int keySize)Generates a key pair.static java.security.KeyPairgenerateKey(java.lang.String seed, java.nio.charset.Charset charset)Generates a public/private key pair.static javax.crypto.spec.PBEParameterSpecgenerateParamSpec()Creates a PBE Parameter specification randomly.static javax.crypto.spec.PBEParameterSpecgenerateParamSpec(byte[] salt, int iterationCount)Creates a PBE Parameter Spec for encryption algorithm.static java.lang.StringgeneratePassword()Creates a random password.static java.lang.StringgeneratePassword(int length)Creates a random password.static java.lang.StringgeneratePassword(java.lang.String allowedChars)Creates a random password.static java.lang.StringgeneratePassword(java.lang.String allowedChars, long randomInit, int length)Creates a random password.static byte[]generateRandomBytes(long randomInit, int size)Generate a random array of bytesstatic 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.KeySpecgetKeySpec(java.security.Key key)Creates a key specification.static java.security.KeyStoregetKeyStore(java.io.InputStream in, char[] password)Load the default keystore type.static java.security.KeyStoregetKeyStore(java.lang.String filename, char[] password)Load the default keystore type.static java.security.KeyStoregetKeyStore(java.lang.String type, java.io.InputStream in, char[] password)Load the given keystore.static java.security.KeyStoregetKeyStore(java.lang.String type, java.lang.String filename, char[] password)Load the default keystore type.static javax.crypto.SecretKeygetSecretKey(int iterationCount, java.lang.String passPhrase, byte[] salt)static java.security.PrivateKeyloadPrivateKey(java.io.File file, java.lang.String algorithm)Instantiates aPrivateKeyfrom given file.static java.security.PrivateKeyloadPrivateKey(java.io.InputStream in, java.lang.String algorithm)Instantiates aPrivateKeyfrom given stream.static java.security.PrivateKeyloadPrivateKey(java.io.Reader in, java.lang.String algorithm)Instantiates aPrivateKeyfrom given reader.static java.security.PrivateKeyloadPrivateKey(java.lang.String filename, java.lang.String algorithm)Instantiates aPrivateKeyfrom given file.static java.security.PrivateKeyloadPrivateKey(java.net.URL url, java.lang.String algorithm)Instantiates aPrivateKeyfrom given URL.static java.security.PublicKeyloadPublicKey(java.io.File file, java.lang.String algorithm)Instantiates aPublicKeyfrom given file.static java.security.PublicKeyloadPublicKey(java.io.InputStream in, java.lang.String algorithm)Instantiates aPublicKeyfrom given stream.static java.security.PublicKeyloadPublicKey(java.io.Reader in, java.lang.String algorithm)Instantiates aPublicKeyfrom given reader.static java.security.PublicKeyloadPublicKey(java.lang.String filename, java.lang.String algorithm)Instantiates aPublicKeyfrom given file.static java.security.PublicKeyloadPublicKey(java.net.URL url, java.lang.String algorithm)Instantiates aPublicKeyfrom given URL.static voidsave(java.io.File file, java.security.PrivateKey key)Saves aPrivateKeyinto given file.static voidsave(java.io.File file, java.security.PublicKey key)Saves aPublicKeyinto given file.static voidsave(java.io.OutputStream out, java.security.PrivateKey key)Saves aPrivateKeyinto given stream.static voidsave(java.io.OutputStream out, java.security.PublicKey key)Saves aPublicKeyinto given stream.static voidsave(java.io.PrintWriter out, java.security.PrivateKey key)Saves aPrivateKeyinto given writer.static voidsave(java.io.PrintWriter out, java.security.PublicKey key)Saves aPublicKeyinto given writer.static voidsave(java.lang.String filename, java.security.PrivateKey key)Saves aPrivateKeyinto given file.static voidsave(java.lang.String filename, java.security.PublicKey key)Saves aPublicKeyinto given file.
-
Field Details
-
DEFAULT_SECRET_KEY_TYPE
public static final java.lang.String DEFAULT_SECRET_KEY_TYPEThe default secret key type to be generated from a password.- See Also:
- Constant Field Values
-
DEFAULT_ITERATIONS
public static final int DEFAULT_ITERATIONSThe default number of iterations to be executed when creating the encrypting algorithm.- See Also:
- Constant Field Values
-
PASSWORD_CHARS
public static final java.lang.String PASSWORD_CHARSThe default number of iterations to be executed when creating the encrypting algorithm.- See Also:
- Constant Field Values
-
-
Constructor Details
-
EncryptionUtils
public EncryptionUtils()
-
-
Method Details
-
getKeySpec
public static java.security.spec.KeySpec getKeySpec(java.security.Key key) throws java.security.spec.InvalidKeySpecException, java.security.NoSuchAlgorithmExceptionCreates a key specification.- Parameters:
key- the key- Returns:
- the PBE param spec
- Throws:
java.security.spec.InvalidKeySpecException- when the key spec is invalidjava.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- initializersize- 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- allowedCharactersrandomInit- initializerlength- 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.NoSuchAlgorithmExceptionjava.security.spec.InvalidKeySpecException
-
generateKey
public static java.security.KeyPair generateKey(java.lang.String seed) throws java.security.NoSuchProviderException, java.security.NoSuchAlgorithmExceptionGenerates a public/private key pair.- Parameters:
seed- seed to be used.- Returns:
- key pair
- Throws:
java.security.NoSuchProviderException- when the algorithm provider does not existjava.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.NoSuchAlgorithmExceptionGenerates a public/private key pair.- Parameters:
seed- seed to be used.charset- the charset to be used for string encoding (nullfordefault charset)- Returns:
- key pair
- Throws:
java.security.NoSuchProviderException- when the algorithm provider does not existjava.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.NoSuchAlgorithmExceptionGenerates a public/private key pair.- Parameters:
seed- seed to be used.- Returns:
- key pair
- Throws:
java.security.NoSuchProviderException- when the algorithm provider does not existjava.security.NoSuchAlgorithmException- when the algorithm does not exist
-
generateKey
public static java.security.KeyPair generateKey() throws java.security.NoSuchAlgorithmExceptionGenerates 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.NoSuchAlgorithmExceptionGenerates 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.NoSuchAlgorithmExceptionGenerates 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.NoSuchAlgorithmExceptionDecodes 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 invalidjava.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.NoSuchAlgorithmExceptionDecodes a public key from the BASE64 representation.- Parameters:
s- BASE64 representationalgorithm- name of algorithm- Returns:
- public key
- Throws:
java.security.spec.InvalidKeySpecException- - when the key spec is invalidjava.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.NoSuchAlgorithmExceptionDecodes 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 invalidjava.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.NoSuchAlgorithmExceptionDecodes a private key from the BASE64 representation.- Parameters:
s- BASE64 representationalgorithm- name of algorithm- Returns:
- public key
- Throws:
java.security.spec.InvalidKeySpecException- - when the key spec is invalidjava.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 stringcharset- the character set to be used for constructing the string (can benullfor 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 encodecharset- the charset to be used for string encoding (nullfordefault 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.IOExceptionLoad the default keystore type.- Parameters:
filename- filenamepassword- 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.IOExceptionLoad the default keystore type.- Parameters:
type- type of key storefilename- filenamepassword- 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.IOExceptionLoad the default keystore type.- Parameters:
in- input streampassword- 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.IOExceptionLoad the given keystore.- Parameters:
type- of keystorein- input streampassword- 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.GeneralSecurityExceptionTransforms all bytes from the input array with a crypt alorithm and returns the transformed bytes.- Parameters:
bytes- bytes to cryptcipher- crypt alorithm used- Returns:
- crypted bytes
- Throws:
java.io.IOException- when crypting cannot be performed due to I/O problemsjava.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.GeneralSecurityExceptionTransforms all bytes from the input array with a crypt alorithm and returns the transformed bytes.- Parameters:
bytes- bytes to cryptcipher- crypt alorithm usedblockSize- 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 problemsjava.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.IOExceptionInstantiates aPrivateKeyfrom given file.- Parameters:
file- file where private key is stored in BASE64 PKCS8 encodingalgorithm- algorithm that the key used- Returns:
- the private key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPrivateKeyfrom given file.- Parameters:
filename- file where private key is stored in BASE64 PKCS8 encodingalgorithm- algorithm that the key used- Returns:
- the private key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPrivateKeyfrom given URL.- Parameters:
url- URL where private key is stored in BASE64 PKCS8 encodingalgorithm- algorithm that the key used- Returns:
- the private key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPrivateKeyfrom given stream.- Parameters:
in- stream where private key is stored in BASE64 PKCS8 encodingalgorithm- algorithm that the key used- Returns:
- the private key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPrivateKeyfrom given reader.- Parameters:
in- reader where private key is stored in BASE64 PKCS8 encodingalgorithm- algorithm that the key used- Returns:
- the private key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPublicKeyfrom given file.- Parameters:
file- file where private key is stored in BASE64 X.509 encodingalgorithm- algorithm that the key used- Returns:
- the public key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPublicKeyfrom given file.- Parameters:
filename- name of file where private key is stored in BASE64 X.509 encodingalgorithm- algorithm that the key used- Returns:
- the public key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPublicKeyfrom given URL.- Parameters:
url- URL where private key is stored in BASE64 X.509 encodingalgorithm- algorithm that the key used- Returns:
- the public key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPublicKeyfrom given stream.- Parameters:
in- stream where private key is stored in BASE64 X.509 encodingalgorithm- algorithm that the key used- Returns:
- the public key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.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.IOExceptionInstantiates aPublicKeyfrom given reader.- Parameters:
in- reader where private key is stored in BASE64 X.509 encodingalgorithm- algorithm that the key used- Returns:
- the public key
- Throws:
java.security.NoSuchAlgorithmException- - when algorithm does not existjava.security.spec.InvalidKeySpecException- - when key spec cannot be createdjava.io.IOException- - when file cannot be opened
-
save
public static void save(java.io.File file, java.security.PrivateKey key) throws java.io.IOExceptionSaves aPrivateKeyinto given file.- Parameters:
file- file where private key will be stored in BASE64 PKCS8 encodingkey- 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.IOExceptionSaves aPrivateKeyinto given file.- Parameters:
filename- name of file where private key will be stored in BASE64 PKCS8 encodingkey- 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.IOExceptionSaves aPrivateKeyinto given stream.- Parameters:
out- stream where private key will be stored in BASE64 PKCS8 encodingkey- 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.IOExceptionSaves aPrivateKeyinto given writer.- Parameters:
out- writer where private key will be stored in BASE64 PKCS8 encodingkey- 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.IOExceptionSaves aPublicKeyinto given file.- Parameters:
file- file where private key will be stored in BASE64 X.509 encodingkey- 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.IOExceptionSaves aPublicKeyinto given file.- Parameters:
filename- name of file where private key will be stored in BASE64 X.509 encodingkey- 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.IOExceptionSaves aPublicKeyinto given stream.- Parameters:
out- stream where private key will be stored in BASE64 X.509 encodingkey- 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.IOExceptionSaves aPublicKeyinto given writer.- Parameters:
out- writer where private key will be stored in BASE64 X.509 encodingkey- key to be stored- Throws:
java.io.IOException- when key cannot be stored
-