Example usage for javax.crypto Cipher SECRET_KEY

List of usage examples for javax.crypto Cipher SECRET_KEY

Introduction

In this page you can find the example usage for javax.crypto Cipher SECRET_KEY.

Prototype

int SECRET_KEY

To view the source code for javax.crypto Cipher SECRET_KEY.

Click Source Link

Document

Constant used to indicate the to-be-unwrapped key is a "secret key".

Usage

From source file:org.auscope.portal.server.web.controllers.GridLoginController.java

/**
 * Extracts and decrypts the XML response received from the SLCS server
 *//*  w  w w  . j ava 2 s.c  o m*/
private String extractSlcsResponse(HttpServletRequest request) throws GeneralSecurityException, IOException {
    String responseXML = null;
    String certReqDataHex = request.getParameter("CertificateRequestData");
    String sessionKeyHex = request.getParameter("SessionKey");
    if (certReqDataHex == null || sessionKeyHex == null) {
        logger.error("CertificateRequestData or SessionKey empty!");
    } else {
        // load host key
        FileInputStream in = new FileInputStream(HOST_KEY_FILE);
        PKCS8Key pem = new PKCS8Key(in, null);
        Key privateKey = pem.getPrivateKey();
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.UNWRAP_MODE, privateKey);

        // unwrap session key and decrypt request data
        byte[] wrappedKey = unhexlify(sessionKeyHex);
        ByteArrayInputStream certReqDataEnc = new ByteArrayInputStream(unhexlify(certReqDataHex));
        Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        responseXML = decryptString(certReqDataEnc, cipher);
    }
    return responseXML;
}

From source file:org.cesecore.keys.token.BaseCryptoToken.java

private Key getKeyFromProperties(String alias) {
    Key key = null;/*from   w  w  w. j  a  v  a2  s  .c o m*/
    Properties prop = getProperties();
    String str = prop.getProperty(alias);
    if (StringUtils.isNotEmpty(str)) {
        // TODO: unwrapping with rsa key is also needed later on
        try {
            PrivateKey privK = getPrivateKey("symwrap");
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", getEncProviderName());
            cipher.init(Cipher.UNWRAP_MODE, privK);
            byte[] bytes = Hex.decode(str);
            // TODO: hardcoded AES for now
            key = cipher.unwrap(bytes, "AES", Cipher.SECRET_KEY);
        } catch (CryptoTokenOfflineException e) {
            log.debug(e);
        } catch (NoSuchAlgorithmException e) {
            log.debug(e);
        } catch (NoSuchProviderException e) {
            log.debug(e);
        } catch (NoSuchPaddingException e) {
            log.debug(e);
        } catch (InvalidKeyException e) {
            log.debug(e);
        }
    }
    return key;
}

From source file:org.soulwing.credo.service.crypto.jca.JcaEncryptedPrivateKeyWrapper.java

/**
 * {@inheritDoc}//  w  w w . jav  a  2  s .co m
 */
@Override
public PrivateKey derive() {
    int delimiter = transform.indexOf('/');
    if (delimiter == -1) {
        throw new IllegalArgumentException("illegal transform syntax: " + transform);
    }
    try {
        String algorithm = transform.substring(0, delimiter);
        Cipher cipher = Cipher.getInstance(transform);
        cipher.init(Cipher.UNWRAP_MODE, secretKey.derive(), new IvParameterSpec(iv));
        return (PrivateKey) cipher.unwrap(cipherText, algorithm, Cipher.SECRET_KEY);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchPaddingException ex) {
        throw new RuntimeException(ex);
    } catch (InvalidAlgorithmParameterException ex) {
        throw new RuntimeException(ex);
    } catch (InvalidKeyException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.tolven.security.password.PasswordHolder.java

private void loadSecretKey(File encryptedSecretKeyFile, char[] password) {
    try {/*from  ww  w .j av  a 2s. co m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileInputStream in = null;
        try {
            in = new FileInputStream(encryptedSecretKeyFile);
            byte[] bytes = new byte[1024];
            int n = 0;
            while ((n = in.read(bytes)) != -1) {
                baos.write(bytes, 0, n);
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
        byte[] encryptedSecretKey = Base64.decodeBase64(baos.toByteArray());
        String alias = getKeyStore().aliases().nextElement();
        Key key = getKeyStore().getKey(alias, password);
        Cipher cipher = Cipher.getInstance(key.getAlgorithm());
        cipher.init(Cipher.UNWRAP_MODE, key);
        secretKey = (SecretKey) cipher.unwrap(encryptedSecretKey, "DESede", Cipher.SECRET_KEY);
    } catch (Exception ex) {
        throw new RuntimeException("Could not load secret key from " + encryptedSecretKeyFile.getPath(), ex);
    }
}

From source file:test.integ.be.fedict.commons.eid.client.JCATest.java

@Test
public void testSoftwareRSAKeyWrapping() throws Exception {
    final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    final KeyPair keyPair = keyPairGenerator.generateKeyPair();

    final KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    final SecretKey secretKey = keyGenerator.generateKey();
    LOG.debug("secret key algo: " + secretKey.getAlgorithm());

    final Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.WRAP_MODE, keyPair.getPublic());
    LOG.debug("cipher security provider: " + cipher.getProvider().getName());
    LOG.debug("cipher type: " + cipher.getClass().getName());
    final byte[] wrappedKey = cipher.wrap(secretKey);

    cipher.init(Cipher.UNWRAP_MODE, keyPair.getPrivate());
    final Key resultKey = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);

    assertArrayEquals(secretKey.getEncoded(), resultKey.getEncoded());

}