List of usage examples for javax.crypto CipherInputStream read
public int read() throws IOException
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };/* w w w . j a v a2s . c om*/ SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); System.out.println("input : " + new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); ByteArrayInputStream bIn = new ByteArrayInputStream(input); CipherInputStream cIn = new CipherInputStream(bIn, cipher); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); int ch; while ((ch = cIn.read()) >= 0) { bOut.write(ch); } byte[] cipherText = bOut.toByteArray(); System.out.println("cipher: " + new String(cipherText)); // decryption pass cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); bOut = new ByteArrayOutputStream(); CipherOutputStream cOut = new CipherOutputStream(bOut, cipher); cOut.write(cipherText); cOut.close(); System.out.println("plain : " + new String(bOut.toByteArray())); }
From source file:MainClass.java
public static void read(byte[] bytes, InputStream in) throws Exception { CipherInputStream cis = new CipherInputStream(in, m_decrypter); int pos = 0, intValue; while ((intValue = cis.read()) != -1) { bytes[pos] = (byte) intValue; pos++;//ww w . ja va2 s . com } }
From source file:MainClass.java
public static void blowfishEncrypt(String f1, String f2) throws Exception { SecretKey key = null;//from ww w. ja v a 2 s. c o m ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("BlowfishKey.ser")); key = (SecretKey) keyFile.readObject(); keyFile.close(); Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2)); int i; do { i = in.read(); if (i != -1) out.write(i); } while (i > 0); in.close(); out.close(); }
From source file:MainClass.java
public static void desEncrypt(String f1, String f2) throws Exception { SecretKey key = null;//from w w w . j a va 2s . c om ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser")); key = (SecretKey) keyFile.readObject(); keyFile.close(); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2)); int i; do { i = in.read(); if (i != -1) out.write(i); } while (i > 0); in.close(); out.close(); }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static String decryptStringImpl(Context context, final String encryptedText) { String plainText = null;/*w w w .jav a 2s .c om*/ 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:de.schildbach.wallet.util.FingerprintHelper.java
private String decipher(Cipher cipher) throws IOException { String retVal = null;/*from w ww . j a v a 2 s. c o m*/ String savedEncryptedPassword = getSavedEncryptedPassword(); if (savedEncryptedPassword != null) { byte[] decodedPassword = decodeBytes(savedEncryptedPassword); CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(decodedPassword), cipher); ArrayList<Byte> values = new ArrayList<>(); int nextByte; while ((nextByte = cipherInputStream.read()) != -1) { values.add((byte) nextByte); } cipherInputStream.close(); byte[] bytes = new byte[values.size()]; for (int i = 0; i < values.size(); i++) { bytes[i] = values.get(i).byteValue(); } retVal = new String(bytes, Charset.defaultCharset()); } return retVal; }
From source file:nl.afas.cordova.plugin.secureLocalStorage.SecureLocalStorage.java
@SuppressWarnings("unchecked") private HashMap<String, String> readAndDecryptStorage(KeyStore keyStore) throws SecureLocalStorageException { try {/* ww w.j a v a 2 s . c om*/ // obtain encrypted key SecretKey key = getSecretKey(keyStore); FileInputStream fis = _cordova.getActivity().openFileInput(SECURELOCALSTORAGEFILE); ArrayList<Byte> values = new ArrayList<Byte>(); try { Cipher output = Cipher.getInstance("DES"); output.init(Cipher.DECRYPT_MODE, key); CipherInputStream cipherInputStream = new CipherInputStream(fis, output); try { int nextByte; while ((nextByte = cipherInputStream.read()) != -1) { values.add((byte) nextByte); } } finally { cipherInputStream.close(); } } finally { fis.close(); } byte[] bytes = new byte[values.size()]; for (int i = 0; i < bytes.length; i++) { bytes[i] = values.get(i); } HashMap<String, String> hashMap; ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); try { hashMap = (HashMap<String, String>) ois.readObject(); } finally { ois.close(); } return hashMap; } catch (Exception e) { Log.e("SecureStorage", "Write", e); throw new SecureLocalStorageException("Error decrypting storage", e); } }
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 ww w . j a v a 2 s. c om ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); for (int i = 0; i < outputStream.size(); i++) { assertEquals(encryptedStream.read(), inputStream.read()); } encryptedStream.close(); }