Example usage for javax.crypto SecretKey getFormat

List of usage examples for javax.crypto SecretKey getFormat

Introduction

In this page you can find the example usage for javax.crypto SecretKey getFormat.

Prototype

public String getFormat();

Source Link

Document

Returns the name of the primary encoding format of this key, or null if this key does not support encoding.

Usage

From source file:energy.usef.core.service.business.MessageEncryptionServiceIntegrationTest.java

@Before
public void initTest() throws UnsupportedEncodingException {
    Whitebox.setInternalState(keystoreHelperService, "config", config);
    service = new MessageEncryptionService();
    Whitebox.setInternalState(service, "keystoreHelperService", keystoreHelperService);

    energy.usef.core.util.encryption.NaCl.sodium().crypto_sign_ed25519_seed_keypair(publicKey, privateKey,
            SEED.getBytes(UTF_8));/*  ww w .java2s .  c om*/
    SecretKey secretKey = new SecretKeySpec(privateKey, ALGORITHM);

    LOGGER.info("Public Key: [{}]", new String(publicKey, StandardCharsets.UTF_8));
    LOGGER.info("Private Key: [{}]", new String(privateKey, StandardCharsets.UTF_8));
    LOGGER.info("Secret Key Algorithm: [{}]", secretKey.getAlgorithm());
    LOGGER.info("Secret Key Format: [{}]", secretKey.getFormat());
    LOGGER.info("Secret Key Encoded: [{}]", new String(secretKey.getEncoded(), StandardCharsets.UTF_8));

    LOGGER.info("### Executing test: {}", name.getMethodName());

    Mockito.when(keystoreHelperService.loadSecretKey())
            .thenReturn(Arrays.copyOf(privateKey, privateKey.length));
}

From source file:com.wandrell.util.ksgen.BouncyCastleKeyStoreFactory.java

@Override
protected final void addSecretKey(final KeyStore kstore, final String alias, final String password)
        throws KeyStoreException {
    final SecretKeyEntry secretKeyEntry; // Secret key entry
    final PasswordProtection keyPassword; // Secret key password protection
    final SecretKey secretKey; // Secret key password
    final byte[] key; // Secret key as array

    key = getPasswordArray(password);/*from www .  j a v  a2  s  .c  o m*/
    secretKey = new SecretKeySpec(key, getSecretKeyAlgorithm());

    LOGGER.debug("Created secret key {} with format {}", Arrays.asList(secretKey.getEncoded()),
            secretKey.getFormat());

    secretKeyEntry = new SecretKeyEntry(secretKey);
    keyPassword = new PasswordProtection(password.toCharArray());
    kstore.setEntry(alias, secretKeyEntry, keyPassword);

    LOGGER.debug("Added secret key with alias {} and password {}", alias, password);
}

From source file:test.unit.org.owasp.webscarab.plugin.saml.SamlTest.java

@Test
public void testEncryptionAES() throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);//from w w  w  .j av a  2s.  co m
    SecretKey secretKey = keygen.generateKey();

    LOG.debug("secret key algo: " + secretKey.getAlgorithm());
    LOG.debug("secret key format: " + secretKey.getFormat());

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    LOG.debug("cipher provider: " + cipher.getProvider().getName());
    byte[] result = cipher.doFinal("hello world".getBytes());
    assertNotNull(result);

    byte[] encodedSecretKey = secretKey.getEncoded();
    LOG.debug("encoded secret key size: " + encodedSecretKey.length * 8);

    // decrypt
    cipher = Cipher.getInstance("AES");
    SecretKeySpec secretKeySpec = new SecretKeySpec(encodedSecretKey, "AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    byte[] decryptedResult = cipher.doFinal(result);
    assertEquals("hello world", new String(decryptedResult));
}

From source file:tests.unit.configuration.ConfigurationTestCase.java

/**
 * @see junit.framework.TestCase#setUp()
 *//*from w w w. java2 s  .co m*/
@Override
protected void setUp() throws Exception {
    super.setUp();

    try {
        FileUtils.moveFile(new File(CryptoHelper.getKeystorePath() + File.separator + KEYSTORE_NAME),
                new File(CryptoHelper.getKeystorePath() + File.separator + KEYSTORE_NAME + ".orig"));
    } catch (Exception exc) {
        exc.printStackTrace();
        fail();
    }

    try {
        KeyStoreID keySid = new KeyStoreID("TEMP", KeyStoreUtils.DEFAULT_KEYSTORE_TYPE, "", "",
                KeyStoreUtils.DEFAULT_KEYSTORE_PROVIDER);
        KeyID keyid = new KeyID("TEMP", keySid, "");
        keySid.setKeyStoreName(KEYSTORE_NAME);
        keySid.setKeyStorePwd(KEY_STORE_PWD);
        keyid.setKeyAlias(ALIAS_KEY_NAME);
        keyid.setKeyPwd(ALIAS_KEY_PWD);
        SecretKey secretKey = CryptoUtils.generateSecretKey(CryptoUtils.TRIPLE_DES_TYPE,
                KEY_STORE_PWD.getBytes());
        System.out.println("***************************************");
        System.out.println("Registering SecretKey: " + secretKey.getAlgorithm() + " " + secretKey.getFormat()
                + " " + secretKey.toString());
        System.out.println("In: " + keyid);

        KeyStoreUtils.writeKey(CryptoHelper.getKeystorePath(), keyid, secretKey, null);

        CryptoHelper.resetCache();
    } catch (Exception exc) {
        exc.printStackTrace();
        throw exc;
    }
}