List of usage examples for javax.crypto CipherInputStream close
public void close() throws IOException
From source file:org.yes.cart.web.support.util.cookie.impl.CookieTuplizerImpl.java
/** * {@inheritDoc}/* w w w . j a va2 s . co m*/ */ @SuppressWarnings("unchecked") public <T extends Serializable> T toObject(Cookie[] cookies, final T object) throws UnableToObjectizeCookieException { final String input = assembleStringRespresentationOfObjectFromCookies(cookies, object); if (input.length() == 0) { return object; } final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input.getBytes()); final BASE64DecoderStream base64DecoderStream = new BASE64DecoderStream(byteArrayInputStream); final CipherInputStream cipherInputStream = new CipherInputStream(base64DecoderStream, desUnCipher); ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(cipherInputStream); return (T) objectInputStream.readObject(); } catch (Exception exception) { try { desUnCipher.init(Cipher.DECRYPT_MODE, secretKey); //reinit } catch (InvalidKeyException e) { ShopCodeContext.getLog(this).error("Cant reinit desUnCipher", exception); } final String errMsg = "Unable to convert bytes assembled from cookies into object"; ShopCodeContext.getLog(this).error(errMsg, exception); throw new UnableToObjectizeCookieException(errMsg, exception); } finally { try { if (objectInputStream != null) { objectInputStream.close(); } cipherInputStream.close(); base64DecoderStream.close(); byteArrayInputStream.close(); } catch (IOException ioe) { // leave this one silent as we have the object on hands. ShopCodeContext.getLog(this).error("Unable to close object stream", ioe); } } }
From source file:com.microsoft.azure.storage.blob.CloudBlobClientEncryptionTests.java
@Test public void testBlockBlobValidateEncryption() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, StorageException, IOException, InvalidAlgorithmParameterException, URISyntaxException, InterruptedException, ExecutionException { int size = 5 * 1024 * 1024; byte[] buffer = TestHelper.getRandomBuffer(size); CloudBlockBlob blob = container.getBlockBlobReference("blob1"); // Create the Key to be used for wrapping. SymmetricKey aesKey = TestHelper.getSymmetricKey(); // Create the encryption policy to be used for upload. BlobEncryptionPolicy uploadPolicy = new BlobEncryptionPolicy(aesKey, null); // Set the encryption policy on the request options. BlobRequestOptions uploadOptions = new BlobRequestOptions(); uploadOptions.setEncryptionPolicy(uploadPolicy); // Upload the encrypted contents to the blob. ByteArrayInputStream stream = new ByteArrayInputStream(buffer); blob.upload(stream, size, null, uploadOptions, null); // Encrypt locally. String metadata = blob.getMetadata().get(Constants.EncryptionConstants.BLOB_ENCRYPTION_DATA); BlobEncryptionData encryptionData = BlobEncryptionData.deserialize(metadata); Cipher myAes = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivParameterSpec = new IvParameterSpec(encryptionData.getContentEncryptionIV()); byte[] contentEncryptionKey = aesKey.unwrapKeyAsync(encryptionData.getWrappedContentKey().getEncryptedKey(), encryptionData.getWrappedContentKey().getAlgorithm()).get(); SecretKey keySpec = new SecretKeySpec(contentEncryptionKey, 0, contentEncryptionKey.length, "AES"); myAes.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec); CipherInputStream encryptedStream = new CipherInputStream(new ByteArrayInputStream(buffer), myAes); // Download the encrypted contents from the blob. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); blob.download(outputStream);//from w ww. j a v a 2s . c om ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); for (int i = 0; i < outputStream.size(); i++) { assertEquals(encryptedStream.read(), inputStream.read()); } encryptedStream.close(); }
From source file:com.pari.nm.utils.backup.BackupRestore.java
public boolean decrypt(File backupZipFile, File encrZipFile) throws Exception { try {/*from w w w.j a v a 2s. com*/ FileOutputStream fout = new FileOutputStream(backupZipFile); FileInputStream fin = new FileInputStream(encrZipFile); Cipher cipher = Cipher.getInstance("DESEDE"); cipher.init(Cipher.DECRYPT_MODE, getDefaultKey()); CipherInputStream cin = new CipherInputStream(fin, cipher); byte[] buffer = new byte[8192]; try { int read = cin.read(buffer); while (read != -1) { fout.write(buffer, 0, read); read = cin.read(buffer); } } finally { try { fout.close(); } catch (Exception ignore) { } try { cin.close(); } catch (Exception ignore) { } try { fin.close(); } catch (Exception ignore) { } } return true; } catch (Exception ex) { ex.printStackTrace(); } return false; }
From source file:uk.ac.ox.webauth.Token.java
/** * Initialise a token with a base64 encoded Webauth token. * @param tokenData The data to be decrypted. * @param sessionKey The session key to use for the AES and Hmac. * @throws GeneralSecurityException if there was a problem with the security code used. *//* w ww . j ava 2 s .com*/ public Token(byte[] tokenData, Key sessionKey) throws GeneralSecurityException { // a token is: // {key-hint}{nonce }{hmac }{token-attributes }{padding } // {4 bytes }{16 bytes}{20 bytes}{make the data into multiple of 16 bytes} // everything after the key hint is aes encrypted try { // set up some streams ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tokenData); DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream); // read the key hint int keyHint = dataInputStream.readInt(); // prepare to AES decrypt the rest Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(DECRYPT_MODE, sessionKey, IV); CipherInputStream decrypt = new CipherInputStream(byteArrayInputStream, cipher); // throw away the nonce if (decrypt.read(new byte[16]) != 16) { throw new GeneralSecurityException("Failed to read nonce from token."); } // read the HMACSHA1 checksum byte[] checksum = new byte[20]; if (decrypt.read(checksum) != 20) { throw new GeneralSecurityException("Failed to read HMAC SHA1 checksum from token."); } // read in the rest of the data ByteArrayOutputStream tokenByteArrayOutputStream = new ByteArrayOutputStream(); for (int b = decrypt.read(); b != -1; b = decrypt.read()) { tokenByteArrayOutputStream.write(b); } byte[] data = tokenByteArrayOutputStream.toByteArray(); decrypt.close(); // check the hmacsha1 Mac hmacSHA1 = Mac.getInstance("HmacSHA1"); hmacSHA1.init(sessionKey); if (!Arrays.equals(checksum, hmacSHA1.doFinal(data))) { throw new GeneralSecurityException("Invalid token, checksum mismatch."); } // create all the key-value pairs for (int i = 0, start = 0; (i = indexOf(SEMI_COLON, data, i)) != -1;) { i++; if (i < data.length && data[i] == SEMI_COLON) { i++; continue; } byte[] keyValuePairArray = new byte[i - start]; System.arraycopy(data, start, keyValuePairArray, 0, keyValuePairArray.length); KeyValuePair kvp = new KeyValuePair(keyValuePairArray); kv.put(new String(kvp.key(), "US-ASCII"), kvp); start = i; } } catch (IOException ioe) { /* should never happen as it's a ByteArrayInputStream */ ioe.printStackTrace(); } valid = true; // create the Stringifier to use stringifier = new WebauthTokenStringifier(); }