生成用于非对称加密的公钥和私钥<br>
密钥对生成算法见：https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator

@param algorithm 非对称加密算法
@param keySize 密钥模（modulus ）长度
@param seed 种子
@return {@link KeyPair}
359649
public static KeyPair generateKeyPair(String algorithm, int keySize, byte[] seed) {
		return KeyUtil.generateKeyPair(algorithm, keySize, seed);
	}
1287252
private Cipher createCipher() throws GeneralSecurityException {
		if(algorithmProvider != null) {
			return Cipher.getInstance(algorithm, algorithmProvider);
		} else {
			return Cipher.getInstance(algorithm);
		}
	}
1167984
public static SecretKey generateKey(String algorithm, KeySpec keySpec) {
		final SecretKeyFactory keyFactory = getSecretKeyFactory(algorithm);
		try {
			return keyFactory.generateSecret(keySpec);
		} catch (InvalidKeySpecException e) {
			throw new CryptoException(e);
		}
	}
193520
public static KeyPair generateKeys(int numBits) throws NoSuchAlgorithmException {
        int numBitsPow2 = 1;
        while (numBitsPow2 < numBits) {
            numBitsPow2 <<= 1;
        }

        KeyPairGenerator kpg = KeyPairGenerator.getInstance(CIPHER_ALGORITHM);
        kpg.initialize(numBitsPow2, SECURE_RNG);
        KeyPair keyPair = kpg.generateKeyPair();
        return keyPair;
    }
955369
public static KeyGenerator getKeyGenerator(String algorithm) {
		final Provider provider = GlobalBouncyCastleProvider.INSTANCE.getProvider();

		KeyGenerator generator;
		try {
			generator = (null == provider) //
					? KeyGenerator.getInstance(getMainAlgorithm(algorithm)) //
					: KeyGenerator.getInstance(getMainAlgorithm(algorithm), provider);
		} catch (NoSuchAlgorithmException e) {
			throw new CryptoException(e);
		}
		return generator;
	}
238810
public static CipherUtil buildInstance(Algorithms algorithms, String seed) {
        int keySize = 0;
        if (algorithms == Algorithms.AES) {
            keySize = 128;
        } else if (algorithms == Algorithms.DES) {
            keySize = 56;
        }
        return buildInstance(algorithms, seed, keySize);
    }
729703
public Key< byte[]> start( final String algorithm) throws NoSuchAlgorithmException {
		final MessageDigest digest = MessageDigest.getInstance( algorithm);
		final Key< byte[]> object = new Key< byte[]>();
		consumers.put( object, new Consumer< byte[]>() {
			public void consume( final ByteBuffer buffer) {
				try {
					digest.update( buffer);
				} catch ( Exception e) {
					throw new RuntimeException( e);
				}
			}
			public byte[] finish() {
				try {
					return digest.digest();
				} catch ( Exception e) {
					throw new RuntimeException( e);
				}
			}
		});
		return object;
	}
1133147
public static SecretKey generateKey(String algorithm, KeySpec keySpec) {
		return KeyUtil.generateKey(algorithm, keySpec);
	}
941636
public static SshKeyPair generateKeyPair(String algorithm, int bits)
			throws IOException, SshException {

		if (!SSH2_RSA.equalsIgnoreCase(algorithm)
				&& !SSH2_DSA.equalsIgnoreCase(algorithm)) {
			throw new IOException(algorithm
					+ " is not a supported key algorithm!");
		}

		SshKeyPair pair = new SshKeyPair();

		if (SSH2_RSA.equalsIgnoreCase(algorithm)) {
			pair = ComponentManager.getInstance().generateRsaKeyPair(bits);
		} else {
			pair = ComponentManager.getInstance().generateDsaKeyPair(bits);
		}

		return pair;
	}
1081207
public static byte[] generateAesKey(final int keysize) {
		try {
			KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_NAME);
			keyGenerator.init(keysize);
			SecretKey secretKey = keyGenerator.generateKey();
			return secretKey.getEncoded();
		} catch (NoSuchAlgorithmException e) {
			throw new ImpossibleException(e);
		}
	}
528900
public static PrivateKey generatePrivateKey(String algorithm, KeySpec keySpec) {
		return KeyUtil.generatePrivateKey(algorithm, keySpec);
	}
509139
public static SecretKey deserializeSecretKey(byte[] keyData, String algorithm) {
        LOGGER.trace("deserialize secret key from data using algorithm \"{}\"", algorithm);
        return new SecretKeySpec(keyData, algorithm);
    }
920181
@Override
    public Builder signWith(String algorithm, String key) throws KeyException {

        if (algorithm == null || algorithm.isEmpty() || !algorithm.equals(Constants.SIGNATURE_ALG_HS256)) {
            String err = Tr.formatMessage(tc, "JWT_INVALID_ALGORITHM_ERR", new Object[] { algorithm, Constants.SIGNATURE_ALG_HS256 });
            throw new KeyException(err);
        }
        if (key == null || key.isEmpty()) {
            String err = Tr.formatMessage(tc, "JWT_INVALID_KEY_ERR", new Object[] { algorithm, key });
            throw new KeyException(err);
        }
        alg = algorithm;
        sharedKey = key;
        return this;
    }
525796
public SymmetricCrypto init(String algorithm, SecretKey key) {
		this.secretKey = key;
		if (algorithm.startsWith("PBE")) {
			// 对于PBE算法使用随机数加盐
			this.params = new PBEParameterSpec(RandomUtil.randomBytes(8), 100);
		}
		this.cipher = SecureUtil.createCipher(algorithm);
		return this;
	}
120821
public static KeyPair generateKeyPair(String algorithm, int keySize, byte[] seed) {
		// SM2算法需要单独定义其曲线生成
		if ("SM2".equalsIgnoreCase(algorithm)) {
			final ECGenParameterSpec sm2p256v1 = new ECGenParameterSpec(SM2_DEFAULT_CURVE);
			return generateKeyPair(algorithm, keySize, seed, sm2p256v1);
		}

		return generateKeyPair(algorithm, keySize, seed, (AlgorithmParameterSpec[]) null);
	}
402646
public KeyPair getRandomKeyPair(int keySize){  
        KeyPairGenerator pairgen;
		try {
			pairgen = KeyPairGenerator.getInstance(algorithm);
			SecureRandom random = new SecureRandom();  
	        pairgen.initialize(keySize, random);  
	        return pairgen.generateKeyPair();
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}  
        return null;  
    }
170131
public static HMac hmac(HmacAlgorithm algorithm, String key) {
		return new HMac(algorithm, StrUtil.utf8Bytes(key));
	}