0

I've been looking at using Conceal to encrypt some files. The code snippet provided states that the input is plain text. Can it be used to encrypt file binary though? Why is it specifically plain text and not general binary?

Here's the snippet provided:

// Creates a new Crypto object with default implementations of 
// a key chain as well as native library.
Crypto crypto = new Crypto(
  new SharedPrefsBackedKeyChain(context),
  new SystemNativeCryptoLibrary());

// Check for whether the crypto functionality is available
// This might fail if Android does not load libaries correctly.
if (!crypto.isAvailable()) {
  return;
}

OutputStream fileStream = new BufferedOutputStream(
  new FileOutputStream(file));

// Creates an output stream which encrypts the data as
// it is written to it and writes it out to the file.
OutputStream outputStream = crypto.getCipherOutputStream(
  fileStream,
  entity);

// Write plaintext to it.
outputStream.write(plainText);
outputStream.close();
Kar
  • 6,063
  • 7
  • 53
  • 82

1 Answers1

0

From the library, the method says:

  /**
   * A convenience method to encrypt data if the data to be processed is small and can
   * be held in memory.
   * @param plainTextBytes Bytes of the plain text.
   * @param entity Entity to process.
   * @return cipherText.
   * @throws KeyChainException
   * @throws CryptoInitializationException
   * @throws IOException
   * @throws CryptoInitializationException Thrown if the crypto libraries could not be initialized.
   * @throws KeyChainException Thrown if there is trouble managing keys.
   */
  public byte[] encrypt(byte[] plainTextBytes, Entity entity)
    throws KeyChainException, CryptoInitializationException, IOException {
    int cipheredBytesLength = plainTextBytes.length + mCipherHelper.getCipherMetaDataLength();
    FixedSizeByteArrayOutputStream outputStream = new FixedSizeByteArrayOutputStream(cipheredBytesLength);
    OutputStream cipherStream = mCipherHelper.getCipherOutputStream(outputStream, entity);
    cipherStream.write(plainTextBytes);
    cipherStream.close();
    return outputStream.getBytes();
  }

You will need to convert your plain text into byte[] in order to use the library. If you can also convert the binary file to byte[] then you can use conceal for your purposes.

Try this method:

byte[] bytes = File.ReadAllBytes("C:\\Mybinaryfile");
Simon
  • 19,658
  • 27
  • 149
  • 217