Java Code Examples for org.apache.cxf.rt.security.crypto.CryptoUtils#getSecretKey()

The following examples show how to use org.apache.cxf.rt.security.crypto.CryptoUtils#getSecretKey() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: cxf   File: CryptoUtilsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBearerTokenCertAndSecretKey() throws Exception {
    AccessTokenRegistration atr = prepareTokenRegistration();
    BearerAccessToken token = p.createAccessTokenInternal(atr);

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    KeyPair keyPair = kpg.generateKeyPair();
    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();

    SecretKey secretKey = CryptoUtils.getSecretKey("AES");
    String encryptedSecretKey = CryptoUtils.encryptSecretKey(secretKey, publicKey);

    String encryptedToken = ModelEncryptionSupport.encryptAccessToken(token, secretKey);
    token.setTokenKey(encryptedToken);
    SecretKey decryptedSecretKey = CryptoUtils.decryptSecretKey(encryptedSecretKey, privateKey);
    ServerAccessToken token2 = ModelEncryptionSupport.decryptAccessToken(p, encryptedToken, decryptedSecretKey);
    // compare tokens
    compareAccessTokens(token, token2);
}
 
Example 2
public byte[] getContentEncryptionKey(JweHeaders headers) {
    byte[] theCek = null;
    if (cek == null) {
        String algoJava = getAlgorithm().getJavaName();
        SecretKey secretKey = CryptoUtils.getSecretKey(AlgorithmUtils.stripAlgoProperties(algoJava),
                      getContentEncryptionKeySize(headers));
        theCek = secretKey.getEncoded();
        if (generateCekOnce) {
            synchronized (this) {
                cek = theCek;
            }
        }
        // Clean the key after we're done with it
        try {
            secretKey.destroy();
        } catch (DestroyFailedException e) {
            // ignore
        }
    } else {
        theCek = cek;
    }
    return theCek;
}
 
Example 3
public DefaultEncryptingOAuthDataProvider(KeyProperties props) {
    this(CryptoUtils.getSecretKey(props));
}
 
Example 4
Source Project: cxf   File: EncryptingDataProvider.java    License: Apache License 2.0 4 votes vote down vote up
public EncryptingDataProvider() throws Exception {
    key = CryptoUtils.getSecretKey("AES");
    String encryptedClient = ModelEncryptionSupport.encryptClient(new Client("1", "2", true), key);
    clients = Collections.singletonMap("1", encryptedClient);
}
 
Example 5
Source Project: cxf   File: Crypto.java    License: Apache License 2.0 4 votes vote down vote up
public static SecretKey generateCek(ContentAlgorithm algo) {
    if (!AES_CEK_SIZE_MAP.containsKey(algo.getJwaName())) {
        throw new IllegalArgumentException("Content algorithm [" + algo.getJwaName() + "] not supported");
    }
    return CryptoUtils.getSecretKey(algo.getJavaAlgoName(), AES_CEK_SIZE_MAP.get(algo.getJwaName()) * 8);
}