public InputDecryptor getValueDecryptor(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CRMFException { Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey); final Cipher dataCipher = helper.createContentCipher(secretKey, contentEncryptionAlgorithm); return new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return contentEncryptionAlgorithm; } public InputStream getInputStream(InputStream dataIn) { return new CipherInputStream(dataIn, dataCipher); } }; }
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey) throws CMSException { Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, derivedKey, encryptedContentEncryptionKey); final Cipher dataCipher = helper.createContentCipher(secretKey, contentEncryptionAlgorithm); return new RecipientOperator(new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return contentEncryptionAlgorithm; } public InputStream getInputStream(InputStream dataOut) { return new CipherInputStream(dataOut, dataCipher); } }); }
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, SubjectPublicKeyInfo senderPublicKey, ASN1OctetString userKeyingMaterial, byte[] encryptedContentKey) throws CMSException { Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, senderPublicKey, userKeyingMaterial, encryptedContentKey); final Cipher dataCipher = contentHelper.createContentCipher(secretKey, contentEncryptionAlgorithm); return new RecipientOperator(new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return contentEncryptionAlgorithm; } public InputStream getInputStream(InputStream dataOut) { return new CipherInputStream(dataOut, dataCipher); } }); }
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException { Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey); final Cipher dataCipher = contentHelper.createContentCipher(secretKey, contentEncryptionAlgorithm); return new RecipientOperator(new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return contentEncryptionAlgorithm; } public InputStream getInputStream(InputStream dataOut) { return new CipherInputStream(dataOut, dataCipher); } }); }
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException { Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey); final Cipher dataCipher = contentHelper.createContentCipher(secretKey, contentEncryptionAlgorithm); return new RecipientOperator(new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return contentEncryptionAlgorithm; } public InputStream getInputStream(InputStream dataIn) { return new CipherInputStream(dataIn, dataCipher); } }); }
/** * {@inheritDoc} */ @Override public InputStream decrypt(String keyAlias, AlgorithmParameters params, InputStream input) { Cipher cipher = getCipher(keyAlias, params, Cipher.DECRYPT_MODE); if (cipher == null) { return input; } try { return new CipherInputStream(input, cipher); } catch (Throwable e) { throw new AlfrescoRuntimeException("Decryption failed for key alias: " + keyAlias, e); } }
/** * load data to the holder, if data exists and is compatible. */ public void load() { if (!new File(Properties.DATA_FILE).exists()) return; Listeners.onAction(OnDataLoadStart.class); try (ObjectInputStream in = new ObjectInputStream(new CipherInputStream(new FileInputStream(Properties.DATA_FILE), fileDecryptCipher))) { holder = (DataHolder) in.readObject(); Listeners.onAction(OnDataLoaded.class); } catch (IOException | ClassNotFoundException e) { Main.showError("Cannot load data! File are corrupted or incompatible with this hardware. " + "Old data will be saved as backup then overwritten with a new file."); makeBackup("OnLoadFail"); e.printStackTrace(); } }
@Override int readByte(CipherInputStream ciIn2, byte[] outputText, int save, int index) throws IOException { int len1 = ciIn2.read(outputText, index, save); out.println("Init: index=" + index + ",len=" + len1); // read more until save bytes index += len1; int len2 = 0; while (len1 != save && len2 != -1) { len2 = ciIn2.read(outputText, index, save - len1); out.println("Cont: index=" + index + ",len=" + len2); len1 += len2; index += len2; } return index; }
/** * Implements byte array buffering type test case of the CICO SKIP test. * * @param blockNum block number to read. */ private void proceedSkipTestUsingByteArrayBufferingType( CipherInputStream ciIn2, int blockNum) throws IOException { int index = blockNum * SAVE; int len1 = ciIn2.read(outputText, index, SAVE); // read more until SAVE bytes index += len1; int len2 = 0; int totalRead = len1; while (len1 != SAVE && len2 != -1) { len2 = ciIn2.read(outputText, index, SAVE - len1); len1 += len2; index += len2; totalRead += len2; } if (totalRead != SAVE) { throw new RuntimeException("Read bytes number " + totalRead + " does not equal to given number " + SAVE); } }
/** * Implements int buffering type test case of the CICO SKIP test. * * @param blockNum block number to read. */ private void proceedSkipTestUsingIntBufferingType(CipherInputStream ciIn2, int blockNum) throws IOException { int index = blockNum * SAVE; int totalRead = 0; for (int j = 0; j < SAVE; j++, index++) { int buffer0 = ciIn2.read(); if (buffer0 != -1) { outputText[index] = (byte) buffer0; totalRead++; } else { break; } } if (totalRead != SAVE) { throw new RuntimeException("Read bytes number " + totalRead + " does not equal to given number " + SAVE); } }
static void gcm_suppressUnreadCorrupt() throws Exception { Cipher c; byte[] read = new byte[200]; System.out.println("Running supressUnreadCorrupt test"); // Encrypt 100 bytes with AES/GCM/PKCS5Padding byte[] ct = encryptedText("GCM", 100); // Corrupt the encrypted message ct = corruptGCM(ct); // Create stream for decryption CipherInputStream in = getStream("GCM", ct); try { in.close(); System.out.println(" Pass."); } catch (IOException e) { System.out.println(" Fail: " + e.getMessage()); throw new RuntimeException(e.getCause()); } }
static void gcm_oneReadByte() throws Exception { System.out.println("Running gcm_oneReadByte test"); // Encrypt 100 bytes with AES/GCM/PKCS5Padding byte[] ct = encryptedText("GCM", 100); // Create stream for decryption CipherInputStream in = getStream("GCM", ct); try { in.read(); System.out.println(" Pass."); } catch (Exception e) { System.out.println(" Fail: " + e.getMessage()); throw new RuntimeException(e.getCause()); } }
static void gcm_oneReadByteCorrupt() throws Exception { System.out.println("Running gcm_oneReadByteCorrupt test"); // Encrypt 100 bytes with AES/GCM/PKCS5Padding byte[] ct = encryptedText("GCM", 100); // Corrupt the encrypted message ct = corruptGCM(ct); // Create stream for decryption CipherInputStream in = getStream("GCM", ct); try { in.read(); System.out.println(" Fail. No exception thrown."); } catch (IOException e) { Throwable ec = e.getCause(); if (ec instanceof AEADBadTagException) { System.out.println(" Pass."); } else { System.out.println(" Fail: " + ec.getMessage()); throw new RuntimeException(ec); } } }
static void cbc_shortStream() throws Exception { Cipher c; AlgorithmParameters params; byte[] read = new byte[200]; System.out.println("Running cbc_shortStream"); // Encrypt 97 byte with AES/CBC/PKCS5Padding byte[] ct = encryptedText("CBC", 97); // Create stream with only 96 bytes of encrypted data CipherInputStream in = getStream("CBC", ct, 96); try { int size = in.read(read); in.close(); if (size != 80) { throw new RuntimeException("Fail: CipherInputStream.read() " + "returned " + size + ". Should have been 80"); } System.out.println(" Pass."); } catch (IOException e) { System.out.println(" Fail: " + e.getMessage()); throw new RuntimeException(e.getCause()); } }
static void cbc_shortRead400() throws Exception { System.out.println("Running cbc_shortRead400"); // Encrypt 400 byte with AES/CBC/PKCS5Padding byte[] ct = encryptedText("CBC", 400); // Create stream with encrypted data CipherInputStream in = getStream("CBC", ct); try { in.read(); in.close(); System.out.println(" Pass."); } catch (IOException e) { System.out.println(" Fail: " + e.getMessage()); throw new RuntimeException(e.getCause()); } }
static void cbc_shortRead600() throws Exception { System.out.println("Running cbc_shortRead600"); // Encrypt 600 byte with AES/CBC/PKCS5Padding byte[] ct = encryptedText("CBC", 600); // Create stream with encrypted data CipherInputStream in = getStream("CBC", ct); try { in.read(); in.close(); System.out.println(" Pass."); } catch (IOException e) { System.out.println(" Fail: " + e.getMessage()); throw new RuntimeException(e.getCause()); } }
static CipherInputStream getStream(String mode, byte[] ct, int length) throws Exception { Cipher c; if (mode.compareTo("GCM") == 0) { c = Cipher.getInstance("AES/GCM/PKCS5Padding", "SunJCE"); c.init(Cipher.DECRYPT_MODE, key, gcmspec); } else if (mode.compareTo("CBC") == 0) { c = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE"); c.init(Cipher.DECRYPT_MODE, key, iv); } else { return null; } return new CipherInputStream(new ByteArrayInputStream(ct, 0, length), c); }
public void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Exception { DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey desKey = skf.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for // SunJCE if (mode == Cipher.ENCRYPT_MODE) { cipher.init(Cipher.ENCRYPT_MODE, desKey); CipherInputStream cis = new CipherInputStream(is, cipher); doCopy(cis, os); } else if (mode == Cipher.DECRYPT_MODE) { cipher.init(Cipher.DECRYPT_MODE, desKey); CipherOutputStream cos = new CipherOutputStream(os, cipher); doCopy(is, cos); } }
public static String enfile_read(String file_name, byte[] key) throws Exception { file = new File(file_name); bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); secretKey = new SecretKeySpec(key, "AES"); cipher = Cipher.getInstance(CIPHER_ALOGORTHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); cipherInputStream = new CipherInputStream(bufferedInputStream, cipher); String str = ""; while((length = cipherInputStream.read(cache)) > 0) { str += new String(cache, 0, length); } return str; }
public static void encryptLarge(String password, File in, File out) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(password), "AES"); Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); FileInputStream inputStream = new FileInputStream(in); FileOutputStream fileOutputStream = new FileOutputStream(out); int read; byte[] buffer = new byte[4096]; CipherInputStream cis = new CipherInputStream(inputStream, cipher); while ((read = cis.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, read); } fileOutputStream.close(); cis.close(); in.delete(); }
public static void decryptLarge(String password, File in, File out) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(password), "AES"); Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); FileInputStream inputStream = new FileInputStream(in); FileOutputStream fileOutputStream = new FileOutputStream(out); int read; byte[] buffer = new byte[4096]; CipherInputStream cis = new CipherInputStream(inputStream, cipher); while ((read = cis.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, read); } fileOutputStream.close(); cis.close(); in.delete(); }