List of usage examples for javax.crypto CipherInputStream CipherInputStream
public CipherInputStream(InputStream is, Cipher c)
From source file:de.serverfrog.pw.crypt.SerpentUtil.java
public static CipherInputStream getInputStream(InputStream os, Key key) throws IOException { try {// ww w .jav a 2 s .com Cipher instance = Cipher.getInstance("Serpent", "BC"); instance.init(Cipher.DECRYPT_MODE, key); return new CipherInputStream(os, instance); } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException ex) { if (ex.getClass().equals(InvalidKeyException.class)) { throw new IOException("Password Wrong"); } Logger.getLogger(SerpentUtil.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:com.intera.roostrap.util.EncryptionUtil.java
private static void encryptOrDecrypt(String encryptionKey, int mode, InputStream is, OutputStream os) throws InvalidKeyException, IOException { DESKeySpec keySpec = new DESKeySpec(toBytes(encryptionKey)); SecretKey key = null;// w ww . j av a2 s . c o m Cipher cipher = null; try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); key = secretKeyFactory.generateSecret(keySpec); cipher = Cipher.getInstance("DES"); } catch (Exception e) { throw new RuntimeException(e); } if (mode == Cipher.ENCRYPT_MODE) { cipher.init(Cipher.ENCRYPT_MODE, key); CipherInputStream cis = new CipherInputStream(is, cipher); doCopy(cis, os); } else if (mode == Cipher.DECRYPT_MODE) { cipher.init(Cipher.DECRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(os, cipher); doCopy(is, cos); } }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static String decryptStringImpl(Context context, final String encryptedText) { String plainText = null;//from w ww .ja v a 2 s . com try { final KeyStore keyStore = getKeyStore(context); PrivateKey privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS, null); String algorithm = ALGORITHM_OLD; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { algorithm = ALGORITHM; } Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, privateKey); CipherInputStream cipherInputStream = new CipherInputStream( new ByteArrayInputStream(Base64.decode(encryptedText, Base64.DEFAULT)), cipher); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int b; while ((b = cipherInputStream.read()) != -1) { outputStream.write(b); } outputStream.close(); plainText = outputStream.toString("UTF-8"); } catch (Exception e) { e.printStackTrace(); } return plainText; }
From source file:com.slimroms.themecore.AssetUtils.java
public static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath, Cipher cipher) { InputStream in;/* w w w. j a va 2 s . c o m*/ File parent = new File(toPath).getParentFile(); if (!parent.exists() && !parent.mkdirs()) { Log.d(TAG, "Unable to create " + parent.getAbsolutePath()); } try { in = assetManager.open(fromAssetPath); if (cipher != null && fromAssetPath.endsWith(".enc")) { in = new CipherInputStream(in, cipher); if (toPath.endsWith(".enc")) { toPath = toPath.substring(0, toPath.lastIndexOf(".")); } } String text = IOUtils.toString(in, Charset.defaultCharset()); Log.d("TEST", "text=" + text); copyInputStreamToFile(in, new File(toPath)); in.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:org.sakuli.services.cipher.AesCbcCipher.java
public static byte[] decryptBytes(SecretKey aesKey, byte[] ciphertext) throws SakuliCipherException { checkCipherParameters(aesKey, ciphertext); final byte[] decrypted; try {/* ww w . j a v a2 s .c o m*/ final ByteArrayInputStream bais = new ByteArrayInputStream(ciphertext); final Cipher aesCBC = Cipher.getInstance(CBC_ALGORITHM); final IvParameterSpec ivForCBC = readIV(aesCBC.getBlockSize(), bais); aesCBC.init(Cipher.DECRYPT_MODE, aesKey, ivForCBC); final byte[] buf = new byte[1_024]; try (final CipherInputStream cis = new CipherInputStream(bais, aesCBC); final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { int read; while ((read = cis.read(buf)) != -1) { baos.write(buf, 0, read); } decrypted = baos.toByteArray(); } return decrypted; } catch (Exception e) { throw new SakuliCipherException(e, "Error during decrypting secret!"); } }
From source file:com.scorpio4.util.io.IOStreamCrypto.java
public CipherInputStream decrypt(InputStream in) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { final SecretKey key = new SecretKeySpec(bytePassword, cipherSpec); final IvParameterSpec IV = new IvParameterSpec(ivBytes); final Cipher cipher = Cipher.getInstance(cipherTransformation); cipher.init(Cipher.DECRYPT_MODE, key, IV); return new CipherInputStream(new Base64InputStream(in), cipher); }
From source file:org.orbeon.oxf.main.SecureResource.java
private void view() { try {/*from w ww . j a va 2 s. c o m*/ FileInputStream archiveFile = new FileInputStream(archiveName); ZipInputStream zip = new ZipInputStream(new CipherInputStream(archiveFile, SecureUtils.getDecryptingCipher(SecureResourceManagerImpl.getPassword(), true))); ZipEntry ze; while ((ze = zip.getNextEntry()) != null) { System.out.println("entry: " + ze.getName()); } zip.close(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.hardisonbrewing.s3j.FileResponseHandler.java
@Override public File handleResponse(HttpResponse httpResponse) throws HttpResponseException, IOException { System.out.println(" Response Headers"); HttpUtil.printHeaders(httpResponse); HttpUtil.validateResponseCode(httpResponse); File file = FileUtils.createTempFile(); long contentLength = 0; HttpEntity entity = null;/*from w w w. j av a 2s .c o m*/ InputStream inputStream = null; CountingInputStream countingInputStream = null; OutputStream outputStream = null; try { entity = httpResponse.getEntity(); contentLength = entity.getContentLength(); validateContentLength(contentLength); inputStream = entity.getContent(); inputStream = new ProgressInputStream(inputStream, contentLength); // put this before the cipher so we get the encrypted length countingInputStream = new CountingInputStream(inputStream); inputStream = countingInputStream; if (cipher != null) { inputStream = new CipherInputStream(inputStream, cipher); } outputStream = new FileOutputStream(file); IOUtil.copy(inputStream, outputStream); } finally { IOUtil.close(inputStream); EntityUtils.consume(entity); IOUtil.close(outputStream); } long readLength = countingInputStream.getByteCount(); validateDownloadLength(contentLength, readLength); return file; }
From source file:CipherSocket.java
public InputStream getInputStream() throws IOException { InputStream is = delegate == null ? super.getInputStream() : delegate.getInputStream(); Cipher cipher = null;/*from w w w . j a v a 2 s . co m*/ try { cipher = Cipher.getInstance(algorithm); int size = cipher.getBlockSize(); byte[] tmp = new byte[size]; Arrays.fill(tmp, (byte) 15); IvParameterSpec iv = new IvParameterSpec(tmp); cipher.init(Cipher.DECRYPT_MODE, key, iv); } catch (Exception e) { e.printStackTrace(); throw new IOException("Failed to init cipher: " + e.getMessage()); } CipherInputStream cis = new CipherInputStream(is, cipher); return cis; }
From source file:jfs.sync.meta.AbstractMetaStorageAccess.java
protected Map<String, FileInfo> getMetaData(String rootPath, String relativePath) { if (directoryCache.containsKey(relativePath)) { return directoryCache.get(relativePath); } // if/*from w w w . j a v a 2s. com*/ Map<String, FileInfo> result = new HashMap<>(); ObjectInputStream ois = null; try { InputStream inputStream = getInputStream(rootPath, getMetaDataPath(relativePath)); byte[] credentials = getCredentials(relativePath); Cipher cipher = SecurityUtils.getCipher(getCipherSpec(), Cipher.DECRYPT_MODE, credentials); inputStream = new CipherInputStream(inputStream, cipher); if (LOG.isDebugEnabled()) { LOG.debug("getMetaData() reading infos for " + relativePath); } // if ois = new ObjectInputStream(inputStream); Object o; while ((o = ois.readObject()) != null) { if (o instanceof FileInfo) { FileInfo fi = (FileInfo) o; if (fi.isDirectory()) { String date; synchronized (FORMATTER) { date = FORMATTER.format(new Date(fi.getModificationDate())); } if (LOG.isDebugEnabled()) { LOG.debug( "getMetaData() " + relativePath + getSeparator() + fi.getName() + ": " + date); } // if } // if result.put(fi.getName(), fi); } // if } // while ois.close(); } catch (FileNotFoundException | EOFException e) { // empty directory or - who cares? } catch (Exception e) { if (LOG.isInfoEnabled()) { LOG.info("getMetaData() possible issue while reading infos " + e, e); } // if } finally { try { if (ois != null) { ois.close(); } // if } catch (Exception ex) { // who cares? } // try/catch } // try/catch directoryCache.put(relativePath, result); return result; }