Skip to main content

Encrypting Files

The TEAgentFile class of the Pi Soft Java TEAgent package represents a file. It is used to encrypt, decrypt a file given the file path. The following sequence describes file encryption and decryption using this class.

  1. Create a TEAgentFile object
  2. Set the path of the file to be encrypted or decrypted, also set the path of the output file
  3. Attach a connection
  4. Encrypt or decrypt the file
  5. Detach the connection

Sample_Bonus_Crypt_Image.java

The following code sample illustrates how to evaluate a file and then encrypting and decrypting it.

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import com.eruces.teagent.TEAgentConnection;
import com.eruces.teagent.TEAgentException;
import com.eruces.teagent.TECipher;
import com.eruces.teagent.TEKey;

public class Sample_Bonus_Crypt_Image {

private static TEAgentConnection conn;
private static byte[] ttag;

private static byte[] readBMP(String strFile) {

FileInputStream fis = null;
BufferedInputStream bis = null;
ByteArrayOutputStream aos = null;

byte[] buffer = new byte[64];

try {
fis = new FileInputStream(strFile);
bis = new BufferedInputStream(fis);
aos = new ByteArrayOutputStream();

while (bis.available() != 0) {
aos.write(buffer, 0, bis.read(buffer));
}
}
catch (IOException e) {
System.out.println("File \"" + strFile + "\" could not be read.");
}
finally {
try {
aos.close();
bis.close();
fis.close();
}
catch (Exception e) {
e.printStackTrace();
}
}

byte[] data = aos.toByteArray();

if (data.length < 54) {
System.out.println("File \"" + strFile + "\" is too small to be a valid BMP file.");
}
if (data[0] != 'B' || data[1] != 'M')
{
System.out.println("File \"" + strFile + "\" has an invalid header for a BMP file.");
}
if ((data[29] << 8 | data[28]) != 24)
{
System.out.println("File \"" + strFile + "\" must have a color depth of 24bpp.");
}
if ((data[33] << 8 | data[32] << 8 | data[31] << 8 | data[30]) != 0)
{
System.out.println("File \"" + strFile + "\" must not use compression.");
}

return data;
}

private static void writeBMP(String strFile, byte[] header, byte[] data) {

ByteArrayInputStream ais = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;

byte[] buffer = new byte[64];

byte[] both = new byte[header.length + data.length];
System.arraycopy(header, 0, both, 0, header.length);
System.arraycopy(data, 0, both, header.length, data.length);

try {
ais = new ByteArrayInputStream(both);
bis = new BufferedInputStream(ais);
fos = new FileOutputStream(strFile);

while (bis.available() != 0) {
fos.write(buffer, 0, bis.read(buffer));
}
}
catch (IOException e) {
System.out.println("File \"" + strFile + "\" could not be written.");
}
finally {
try {
fos.close();
bis.close();
ais.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

private static byte[] encryptBytes(byte[] data) {

byte[] result = null;

try {
TEKey teKey = new TEKey();
teKey.attachConnection(conn);
TECipher teCipher = teKey.exportKey(new byte[0]);

ttag = teCipher.getRawHiddenLink();

result = teCipher.encrypt(data);

teKey.detachConnection();

} catch (TEAgentException e) {
e.printStackTrace();
}

return result;
}

private static byte[] decryptBytes(byte[] data) {

byte[] result = null;

try {
TEKey teKey = new TEKey();
teKey.attachConnection(conn);
TECipher teCipher = teKey.exportKey(ttag);

result = teCipher.decrypt(data);

teKey.detachConnection();

} catch (TEAgentException e) {
e.printStackTrace();
}

return result;
}

public static void cryptImage(TEAgentConnection conn_, String file_path) {

System.out.println("Attempting to perform image encryption tests on \"" + file_path + "\"...");

conn = conn_;

// typical bitmap file: 54 byte header, 3 bytes per pixel

try {

byte[] header;
byte[] data;
byte[] raw;

raw = readBMP(file_path);
header = Arrays.copyOfRange(raw, 0, 54);
data = Arrays.copyOfRange(raw, 54, raw.length);

data = encryptBytes(data);
writeBMP("encrypted.bmp", header, data);


raw = readBMP("encrypted.bmp");
header = Arrays.copyOfRange(raw, 0, 54);
data = Arrays.copyOfRange(raw, 54, raw.length);

data = decryptBytes(data);
writeBMP("decrypted.bmp", header, data);

}
catch (Exception e) {
e.printStackTrace();
}


}
}