Java Utililty Methods Key Pair Create

List of utility methods to do Key Pair Create

Description

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

Method

SecretKeygenerateKey(String algorithm, int size)
Generates a secret symmetric key
KeyGenerator keyGen = null;
try {
    keyGen = KeyGenerator.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
    throw new KeyException("Cannot initialize key generator", e);
SecureRandom random = new SecureRandom();
keyGen.init(size, random);
...
SecretKeygenerateKey(String cipherAlgorithm)
Generates a random encryption key.
KeyGenerator keyGenerator = KeyGenerator.getInstance(cipherAlgorithm);
keyGenerator.init(secureRandom);
return keyGenerator.generateKey();
KeyPairgenerateKey(String keyPhrase)
Generate key which contains a pair of privae and public key using 1024 bytes
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
SecureRandom randomAlg = SecureRandom.getInstance("SHA1PRNG", "SUN");
randomAlg.setSeed(keyPhrase.getBytes());
keyGen.initialize(1024, randomAlg);
KeyPair key = keyGen.generateKeyPair();
return key;
byte[]generateKey(String password)
generate Key
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128); 
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
return f.generateSecret(spec).getEncoded();
voidgenerateKey(String PRIVATE_KEY_FILE, String PUBLIC_KEY_FILE)
Generate key which contains a pair of private and public key using 1024 bytes.
try {
    final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
    keyGen.initialize(1024);
    final KeyPair key = keyGen.generateKeyPair();
    File privateKeyFile = new File(PRIVATE_KEY_FILE);
    File publicKeyFile = new File(PUBLIC_KEY_FILE);
    if (privateKeyFile.getParentFile() != null) {
        privateKeyFile.getParentFile().mkdirs();
...
voidgenerateKey(String publicKeyFilename, String privateKeyFilename, String password)
generate Key
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom secureRandom = new SecureRandom(password.getBytes());
keyPairGenerator.initialize(1024, secureRandom);
KeyPair keyPair = keyPairGenerator.genKeyPair();
byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
FileOutputStream fos = new FileOutputStream(publicKeyFilename);
fos.write(publicKeyBytes);
fos.close();
...
SecretKeygenerateKey(String secretKey)
generate Key
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secretKey.getBytes());
KeyGenerator kg = null;
try {
    kg = KeyGenerator.getInstance("DES");
} catch (NoSuchAlgorithmException nosuchalgorithmexception) {
kg.init(secureRandom);
...
StringgenerateKey(String seed)
Create a DES key and return a string version of the raw bits.
byte[] seedBytes = seed.getBytes();
SecureRandom random = new SecureRandom(seedBytes);
KeyGenerator keygen = null;
try {
    keygen = KeyGenerator.getInstance("DES");
} catch (NoSuchAlgorithmException e) {
    throw e;
keygen.init(random);
Key key = keygen.generateKey();
byte[] keyBytes = key.getEncoded();
return bytesToText(keyBytes);
KeygenerateKey(String type)
generate Key
KeyGenerator keygen = KeyGenerator.getInstance(type);
keygen.init(new SecureRandom());
return keygen.generateKey();
KeygenerateKey(String userKey, String masterKey)
generate Key
if (masterKey.equals("5fcf82bc15aef42cd3ec93e6d4b51c04df110cf77ee715f62f3f172ff8ed9de9")) {
    return new SecretKeySpec(userKey.substring(0, 16).getBytes(), "AES");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 8; i++) {
    sb.append(userKey.charAt(i));
    if (masterKey.length() > i + 1) {
        sb.append(masterKey.charAt(i + 1));
...