List of usage examples for javax.crypto Cipher UNWRAP_MODE
int UNWRAP_MODE
To view the source code for javax.crypto Cipher UNWRAP_MODE.
Click Source Link
From source file:org.tolven.security.password.PasswordHolder.java
private void loadSecretKey(File encryptedSecretKeyFile, char[] password) { try {/*from w w w. ja v a2 s.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()); }