Search in sources :

Example 96 with KeyGenerator

use of javax.crypto.KeyGenerator in project protools by SeanDragon.

the class ToolMAC_BCP method initHmacSHA224Key.

/**
 * 初始化HmacSHA224密钥
 *
 * @return byte[] 密钥
 *
 * @throws Exception
 */
public static byte[] initHmacSHA224Key() throws NoSuchAlgorithmException {
    // 加入BouncyCastleProvider支持
    Security.addProvider(new BouncyCastleProvider());
    // 初始化KeyGenerator
    KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA224");
    // 产生秘密密钥
    SecretKey secretKey = keyGenerator.generateKey();
    // 获得密钥
    return secretKey.getEncoded();
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 97 with KeyGenerator

use of javax.crypto.KeyGenerator in project rxlib by RockyLOMO.

the class AESUtil method genarateRandomKey.

public static byte[] genarateRandomKey() {
    KeyGenerator keygen;
    try {
        keygen = KeyGenerator.getInstance(AES_ALGORITHM);
    } catch (NoSuchAlgorithmException ex) {
        throw SystemException.wrap(ex);
    }
    SecureRandom random = new SecureRandom();
    keygen.init(random);
    Key key = keygen.generateKey();
    return key.getEncoded();
}
Also used : SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 98 with KeyGenerator

use of javax.crypto.KeyGenerator in project jeesuite-libs by vakinge.

the class AES method initKey.

/**
 * 生成密钥
 * @throws Exception
 */
public static byte[] initKey() throws Exception {
    // 密钥生成器
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    // 初始化密钥生成器
    // 默认128,获得无政策权限后可用192或256
    keyGen.init(128);
    // 生成密钥
    SecretKey secretKey = keyGen.generateKey();
    return secretKey.getEncoded();
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 99 with KeyGenerator

use of javax.crypto.KeyGenerator in project Bytecoder by mirkosertic.

the class Pair method doGenSecretKey.

/**
 * Creates a new secret key.
 */
private void doGenSecretKey(String alias, String keyAlgName, int keysize) throws Exception {
    if (alias == null) {
        alias = keyAlias;
    }
    if (keyStore.containsAlias(alias)) {
        MessageFormat form = new MessageFormat(rb.getString("Secret.key.not.generated.alias.alias.already.exists"));
        Object[] source = { alias };
        throw new Exception(form.format(source));
    }
    // Use the keystore's default PBE algorithm for entry protection
    boolean useDefaultPBEAlgorithm = true;
    SecretKey secKey = null;
    if (keyAlgName.toUpperCase(Locale.ENGLISH).startsWith("PBE")) {
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
        // User is prompted for PBE credential
        secKey = factory.generateSecret(new PBEKeySpec(promptForCredential()));
        // Check whether a specific PBE algorithm was specified
        if (!"PBE".equalsIgnoreCase(keyAlgName)) {
            useDefaultPBEAlgorithm = false;
        }
        if (verbose) {
            MessageFormat form = new MessageFormat(rb.getString("Generated.keyAlgName.secret.key"));
            Object[] source = { useDefaultPBEAlgorithm ? "PBE" : secKey.getAlgorithm() };
            System.err.println(form.format(source));
        }
    } else {
        KeyGenerator keygen = KeyGenerator.getInstance(keyAlgName);
        if (keysize == -1) {
            if ("DES".equalsIgnoreCase(keyAlgName)) {
                keysize = 56;
            } else if ("DESede".equalsIgnoreCase(keyAlgName)) {
                keysize = 168;
            } else {
                throw new Exception(rb.getString("Please.provide.keysize.for.secret.key.generation"));
            }
        }
        keygen.init(keysize);
        secKey = keygen.generateKey();
        if (verbose) {
            MessageFormat form = new MessageFormat(rb.getString("Generated.keysize.bit.keyAlgName.secret.key"));
            Object[] source = { keysize, secKey.getAlgorithm() };
            System.err.println(form.format(source));
        }
    }
    if (keyPass == null) {
        keyPass = promptForKeyPass(alias, null, storePass);
    }
    if (useDefaultPBEAlgorithm) {
        keyStore.setKeyEntry(alias, secKey, keyPass, null);
    } else {
        keyStore.setEntry(alias, new KeyStore.SecretKeyEntry(secKey), new KeyStore.PasswordProtection(keyPass, keyAlgName, null));
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) MessageFormat(java.text.MessageFormat) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyGenerator(javax.crypto.KeyGenerator) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) CertStoreException(java.security.cert.CertStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException)

Example 100 with KeyGenerator

use of javax.crypto.KeyGenerator in project openmrs-core by openmrs.

the class Security method generateNewSecretKey.

/**
 * generate a new secret key; should only be called once in order to not invalidate all
 * encrypted data
 *
 * @return generated secret key byte array
 * @since 1.9
 */
public static byte[] generateNewSecretKey() {
    // Get the KeyGenerator
    KeyGenerator kgen;
    try {
        kgen = KeyGenerator.getInstance(OpenmrsConstants.ENCRYPTION_KEY_SPEC);
    } catch (NoSuchAlgorithmException e) {
        throw new APIException("could.not.generate.cipher.key", null, e);
    }
    // 192 and 256 bits may not be available
    kgen.init(128);
    // Generate the secret key specs.
    SecretKey skey = kgen.generateKey();
    return skey.getEncoded();
}
Also used : SecretKey(javax.crypto.SecretKey) APIException(org.openmrs.api.APIException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

KeyGenerator (javax.crypto.KeyGenerator)464 SecretKey (javax.crypto.SecretKey)343 Test (org.junit.Test)106 ArrayList (java.util.ArrayList)104 SecureRandom (java.security.SecureRandom)99 Document (org.w3c.dom.Document)98 InputStream (java.io.InputStream)95 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)93 ByteArrayOutputStream (java.io.ByteArrayOutputStream)87 NodeList (org.w3c.dom.NodeList)82 Cipher (javax.crypto.Cipher)79 ByteArrayInputStream (java.io.ByteArrayInputStream)75 XMLStreamReader (javax.xml.stream.XMLStreamReader)68 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)68 DocumentBuilder (javax.xml.parsers.DocumentBuilder)62 Key (java.security.Key)58 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)45 SecurePart (org.apache.xml.security.stax.ext.SecurePart)40 SecretKeySpec (javax.crypto.spec.SecretKeySpec)39