/** * Returns the input stream for reading the previously written encrypted data * * @return the inputstream * @throws IOException if the reading of the underlying file fails */ public InputStream getInputStream() throws IOException { Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, PADDING); return new CipherInputStream(new FileInputStream(tempFile), ciDec); }
static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { FileInputStream fis = new FileInputStream("data/encrypted"); FileOutputStream fos = new FileOutputStream("data/decrypted"); SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, sks); CipherInputStream cis = new CipherInputStream(fis, cipher); int b; byte[] d = new byte[8]; while((b = cis.read(d)) != -1) { fos.write(d, 0, b); } fos.flush(); fos.close(); cis.close(); }
private void crypt(InputStream in, OutputStream out, byte[] keyBytes, byte[] iv, int cryptMode) throws CryptoException { if (in == null) { throw new NullPointerException("InputStream argument cannot be null."); } if (out == null) { throw new NullPointerException("OutputStream argument cannot be null."); } javax.crypto.Cipher cipher = initNewCipher(cryptMode, keyBytes, iv, true); CipherInputStream cis = new CipherInputStream(in, cipher); int bufSize = getStreamingBufferSize(); byte[] buffer = new byte[bufSize]; int bytesRead; try { while ((bytesRead = cis.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } catch (IOException e) { throw new CryptoException(e); } }
mEcipher.init (Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = mEcipher.getParameters (); mDecipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(mInitVec)); byte [] inbuf = new byte [MAX_FILE_BUF]; fout = new FileOutputStream (output); fin = new FileInputStream (input); while ((nread = fin.read (inbuf)) > 0 ) fout.write (tmp); byte [] finalbuf = mEcipher.doFinal (); if (finalbuf != null) fout.write (finalbuf); while ((nread = cin.read (inbuf)) > 0 ) fout.flush(); cin.close(); fin.close (); fout.close();
FileInputStream fis = new FileInputStream(new File("D:/Shashank/inputVideo.avi")); File outfile = new File("D:/Shashank/encVideo.avi"); int read; if(!decfile.exists()) decfile.createNewFile(); FileOutputStream fos = new FileOutputStream(outfile); FileInputStream encfis = new FileInputStream(outfile); FileOutputStream decfos = new FileOutputStream(decfile); Cipher encipher = Cipher.getInstance("AES"); Cipher decipher = Cipher.getInstance("AES"); encipher.init(Cipher.ENCRYPT_MODE, skey); CipherInputStream cis = new CipherInputStream(fis, encipher); decipher.init(Cipher.DECRYPT_MODE, skey); CipherOutputStream cos = new CipherOutputStream(decfos,decipher); while((read = cis.read())!=-1) fos.write((char)read); fos.flush(); fos.close(); while((read=encfis.read())!=-1)
public static void decryptFile(File src, File dst, String passwd, String iv) throws Exception { if (!dst.getParentFile().exists()) dst.getParentFile().mkdirs(); byte[] _iv = StringUtils.getHexBytes(iv); IvParameterSpec _spec = new IvParameterSpec(_iv); byte[] _keyBytes = HashFunctions.getSHAHashBytes(passwd.getBytes()); SecretKeySpec _key = new SecretKeySpec(_keyBytes, "AES"); Cipher encrypt = Cipher.getInstance("AES/CBC/PKCS5Padding"); encrypt.init(Cipher.DECRYPT_MODE, _key, _spec); // opening streams FileOutputStream fos = new FileOutputStream(dst); FileInputStream fis = new FileInputStream(src); CipherInputStream cis = new CipherInputStream(fis, encrypt); IOUtils.copy(cis, fos); fos.flush(); fos.close(); cis.close(); }
protected void transferFileData(File inputFile, File outputFile, Cipher cipher) throws IOException { InputStream is = null; OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(outputFile)); is = new CipherInputStream(new BufferedInputStream(new FileInputStream(inputFile)), cipher); byte[] buffer = new byte[16384]; int read = -1; while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } } finally { if (os != null) os.close(); if (is != null) is.close(); } }
FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream fileOutputStream = new FileOutputStream(outputFile); int read; CipherInputStream cis = new CipherInputStream(inputStream, cipher); while ((read = cis.read()) != -1) { fileOutputStream.write(read); } fileOutputStream.close(); cis.close();
cipher.init(Cipher.ENCRYPT_MODE, secretKey); FileInputStream fis = new FileInputStream(System.getProperty("user.home") + java.io.File.separatorChar + "plain.pdf"); FileOutputStream fos = new FileOutputStream(System.getProperty("user.home") + java.io.File.separatorChar + "test.enc"); final CipherOutputStream output = new CipherOutputStream(fos, cipher); while ((bytesRead = fis.read(plainText)) >= 0) { output.write(plainText, 0, bytesRead); fos.close(); fis.close(); final byte[] iv = cipher.getIV(); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); fis = new FileInputStream(System.getProperty("user.home") + java.io.File.separatorChar + "test.enc"); fos = new FileOutputStream(System.getProperty("user.home") + java.io.File.separatorChar + "test.pdf"); final CipherInputStream input = new CipherInputStream(fis, cipher); while ((decryptedRead = input.read(decryptedData)) >= 0) { fos.write(decryptedData, 0, decryptedRead); fos.flush(); fos.close(); input.close(); fis.close();
byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; fileinputstrm = new FileInputStream(path); PBEKeySpec pbeKeySpec = new PBEKeySpec("pass".toCharArray()); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20); SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); FileInputStream fis=new FileInputStream(path); CipherInputStream cis=new CipherInputStream(fis, pbeCipher); BufferedInputStream bfi=new BufferedInputStream(cis); bfi.read(); cis.close(); FileOutputStream output1 = new FileOutputStream(path+".jpeg"); ByteArrayOutputStream baos=new ByteArrayOutputStream(); BufferedOutputStream bfo=new BufferedOutputStream(output1); output1.write(baos.toByteArray());
SecretKey sk = kg.generateKey(); Cipher cipher = Cipher.getInstance(TEST_CIPHER); cipher.init(Cipher.ENCRYPT_MODE, sk); System.out.println("IV: " + new String(Base64.encodeBase64(cipher.getIV()), "US-ASCII")); CipherOutputStream out = new CipherOutputStream(new FileOutputStream(new File(outfile)), cipher); InputStream in = new FileInputStream(new File(infile)); String outfile = args[4]; SecretKeySpec sk = new SecretKeySpec(Base64.decodeBase64(key.getBytes("US-ASCII")), "AES"); IvParameterSpec ivspec = new IvParameterSpec(Base64.decodeBase64(iv.getBytes("US-ASCII"))); Cipher cipher = Cipher.getInstance(TEST_CIPHER); cipher.init(Cipher.DECRYPT_MODE, sk, ivspec); CipherInputStream in = new CipherInputStream(new FileInputStream(new File(infile)), cipher); OutputStream out = new FileOutputStream(new File(outfile)); out.close(); in.close();
// get cipher object for password-based encryption Cipher cipher1 = Cipher.getInstance("PBEWithMD5AndDES");//You have to pass here algorithm name which PGP uses. May be you have to find and init provider for it. // initialize cipher for decryption, using one of the // init() methods that takes an AlgorithmParameters // object, and pass it the algParams object from above cipher1.init(Cipher.DECRYPT_MODE, myKey, algParams); FileInputStream fis; FileOutputStream fos; CipherInputStream cis; fis = new FileInputStream("/tmp/a.txt"); cis = new CipherInputStream(fis, cipher1); fos = new FileOutputStream("/tmp/b.txt"); byte[] b = new byte[8]; int i = cis.read(b); while (i != -1) { fos.write(b, 0, i); i = cis.read(b); } fos.close();
inCipher.init(Cipher.ENCRYPT_MODE, publicKey); outCipher.init(Cipher.DECRYPT_MODE, privateKey); new FileOutputStream(encryptedDataFilePath), inCipher); cipherOutputStream.write(plainText.getBytes("UTF-8")); cipherOutputStream.close(); new CipherInputStream(new FileInputStream(encryptedDataFilePath), outCipher); byte [] roundTrippedBytes = new byte[1000]; // TODO: dynamically resize as we get more data while ((nextByte = cipherInputStream.read()) != -1) { roundTrippedBytes[index] = (byte)nextByte; index++;
InputStream is = new FileInputStream(in); if (is.read(bufferKey) != bufferKey.length) { is.close(); cipher.init(Cipher.UNWRAP_MODE, priv); Key aesKey = cipher.unwrap(bufferKey, "AES", Cipher.SECRET_KEY); cipher.init(Cipher.DECRYPT_MODE, aesKey); FileOutputStream os = new FileOutputStream(out); ix.close(); os.close();
public void decrypt(File in, File out) throws IOException, InvalidKeyException { aesCipher.init(Cipher.DECRYPT_MODE, aeskeySpec); CipherInputStream is = new CipherInputStream(new FileInputStream(in), aesCipher); FileOutputStream os = new FileOutputStream(out); int i; byte[] b = new byte[1024]; while((i=is.read(b))!=-1) { os.write(b, 0, i); }
/** * @param keyLength * Block size of the asymmetric cipher, in bits. I thought I can get it from {@code asym.getBlockSize()} * but that doesn't work with Sun's implementation. */ public CombinedCipherInputStream(InputStream in, Cipher asym, String algorithm, int keyLength) throws IOException, GeneralSecurityException { super(in); String keyAlgorithm = getKeyAlgorithm(algorithm); // first read the symmetric key cipher byte[] symKeyBytes = new byte[keyLength/8]; new DataInputStream(in).readFully(symKeyBytes); SecretKey symKey = new SecretKeySpec(asym.doFinal(symKeyBytes),keyAlgorithm); // the rest of the data will be decrypted by this symmetric cipher Cipher sym = Secret.getCipher(algorithm); sym.init(Cipher.DECRYPT_MODE,symKey, keyAlgorithm.equals(algorithm) ? null : new IvParameterSpec(symKey.getEncoded())); super.in = new CipherInputStream(in,sym); }
/** * 解密文件 * * @param in */ public void doDecryptFile(InputStream in, String path) { if (in == null) { System.out.println("inputstream is null"); return; } try { CipherInputStream cin = new CipherInputStream(in, mDecryptCipher); OutputStream outputStream = new FileOutputStream(path); byte[] bytes = new byte[1024]; int length = -1; while ((length = cin.read(bytes)) > 0) { outputStream.write(bytes, 0, length); outputStream.flush(); } cin.close(); in.close(); System.out.println("解密成功"); } catch (Exception e) { System.out.println("解密失败"); e.printStackTrace(); } }
CipherInputStream c_decf = new CipherInputStream(decf, decipher); FileOutputStream destf = new FileOutputStream(destfile); //CipherOutputStream cout = new CipherOutputStream(destf, decipher); while ((read = c_decf.read()) != -1) { destf.write(read); destf.flush(); } c_decf.close(); destf.close(); // cout.close(); decf.close();
byte[] wholeFileByte = null; Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.DECRYPT_MODE, key, DownloadBookAsyncTask.ivspec); File file = new File(context.getFilesDir().getParentFile().getPath() + "/download/" + id + "/xmldata.xml"); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); CipherInputStream cis = new CipherInputStream(fis, cipher); byte data[] = new byte[4096]; int count; while ((count = cis.read(data)) != -1) { bos.write(data, 0, count); } if(cis != null) cis.close(); if(bos != null) bos.close(); if(fis != null) fis.close(); wholeFileByte = bos.toByteArray(); String kk = new String(wholeFileByte, "UTF-8");
public void decrypt(String filename) throws Exception { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("filename")); Base64InputStream base64Stream = new Base64InputStream(bis, 0); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); CipherInputStream cis = new CipherInputStream(base64Stream, c); byte[] plainBuf = new byte[2048]; int nRead; while ((nRead = cis.read(plainBuf)) > 0) { // send plainBuf[0] through plainBuf[nRead-1] to the video codec } cis.close(); }