Java Key Pair Create generateKey(String userKey, String masterKey)

Here you can find the source of generateKey(String userKey, String masterKey)

Description

generate Key

License

Open Source License

Declaration

private static Key generateKey(String userKey, String masterKey) 

Method Source Code

//package com.java2s;
/*// w w w.  java 2  s . com
 * Copyright (C) 2013 - 2018, Logical Clocks AB and RISE SICS AB. All rights reserved
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

import javax.crypto.spec.SecretKeySpec;

import java.security.Key;

public class Main {
    private static Key generateKey(String userKey, String masterKey) {
        // This is for backwards compatibility
        // sha256 of 'adminpw'
        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));
            } else {
                sb.append(userKey.charAt(Math.max(0, userKey.length() - i)));
            }
        }
        return new SecretKeySpec(sb.toString().getBytes(), "AES");
    }
}

Related

  1. generateKey(String PRIVATE_KEY_FILE, String PUBLIC_KEY_FILE)
  2. generateKey(String publicKeyFilename, String privateKeyFilename, String password)
  3. generateKey(String secretKey)
  4. generateKey(String seed)
  5. generateKey(String type)
  6. generateKeyAES()
  7. generateKeyDigest(int keyLength, String password)
  8. generateKeyFromString(final String secKey, String algorithm)
  9. generateKeyPair()