Java Utililty Methods Key Create

List of utility methods to do Key Create

Description

The list of methods to do Key Create are organized into topic(s).

Method

PrivateKeygetKey()
get Key
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(KEY);
return keyFactory.generatePrivate(privateKeySpec);
SecretKeygetKey()
Gets the key.
KeySpec keySpec = new PBEKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
return keyFactory.generateSecret(keySpec);
KeygetKey()
get Key
try {
    InputStream fis = ClassLoader.getSystemResourceAsStream("key.pk8");
    if (fis == null) {
        throw new RuntimeException("Cannot find keyfile");
    try {
        return KeyFactory.getInstance("RSA")
                .generatePrivate(new PKCS8EncodedKeySpec(getByteArrayFromStream(fis)));
...
KeygetKey(byte[] arrayBytesTemp)
get Key
byte[] bytesTemp = new byte[8];
int len = arrayBytesTemp.length < bytesTemp.length ? arrayBytesTemp.length : bytesTemp.length;
for (int i = 0; i < len; i++) {
    bytesTemp[i] = arrayBytesTemp[i];
Key key = new SecretKeySpec(bytesTemp, DEFAULT_ALGORITHM_NAME);
return key;
KeygetKey(byte[] keyData)
Instantiates an AES key.
return new SecretKeySpec(keyData, "AES");
KeygetKey(InputStream is)
get Key
try {
    ObjectInputStream ois = new ObjectInputStream(is);
    return (Key) ois.readObject();
} catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException(e);
byte[]getKey(int size)
Generate a random key.
if (size < 1)
    return null; 
byte[] k = new byte[size];
_rand.nextBytes(k);
return k;
KeygetKey(KeyStore keystore, String alias, String password)
Get a private key from the keystore by name and password.
return keystore.getKey(alias, password.toCharArray());
PrivateKeygetKey(KeyStore keyStore, String password, String orgName)
Loads a private key with the specified org name from a keystore.
try {
    PrivateKey key = (PrivateKey) keyStore.getKey(orgName, password.toCharArray());
    if (key == null) {
        throw new RuntimeException("Unable to get key for " + "name \"" + orgName + "\" using password \""
                + password + "\" from keystore");
    return key;
} catch (Exception e) {
...
TgetKey(KeyStore ks, String alias, String pass)
get Key
return (T) ks.getKey(alias, pass.toCharArray());