Android Encryption with javax.crypto decryption with AES Cry

Discussion related to AES Crypt, the file encryption software for Windows, Linux, Mac, and Java.
Post Reply
dspence3
Posts: 1
Joined: Mon Oct 21, 2013 1:51 am

Android Encryption with javax.crypto decryption with AES Cry

Post by dspence3 »

Hello all and thanks for any suggestions in advance.

I am working a project to encrypt XML files on android devices that will be emailed and opened later on windows machines with AES Crypt.

Everything works find on the android environment. I can encrypt the XML File and then decrypt the cypher text to get back the original plain text XML. However when I download the encrypted file under windows 7 and try to decrypt it with AES Crypt I get the error message "Invalid Signature"

My crypto class is as follows:

Code: Select all

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class FileCrypto {

	private String _plainTextFile;
	private String _cypherTextFile;
	private String _password;
	
	// the getters and setters
	public String getPlainTextFile() {
		return _plainTextFile;
	}

	public void setPlainTextFile(String string) {
		this._plainTextFile = string;
	}
	
	public String getCypherTextFile() {
		return _cypherTextFile;
	}

	public void setCypherTextFile(String string) {
		this._cypherTextFile = string;
	}
	
	public String getPassword() {
		return _password;
	}

	public void setPassword(String string) {
		this._password = string;
	}
	
	public FileCrypto(){
		_plainTextFile = "";
		_cypherTextFile = "";
		_password = "";
	}
	
	public FileCrypto(String plainTextFile, String cypherTextFile, String password){
		_plainTextFile = plainTextFile;
		_cypherTextFile = cypherTextFile;
		_password = password;
	}
	
	public void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
	    // Here you read the cleartext.
	    FileInputStream fis = new FileInputStream(_plainTextFile);
	    // This stream write the encrypted text. This stream will be wrapped by another stream.
	    FileOutputStream fos = new FileOutputStream(_cypherTextFile);

	    // Length is 16 byte
	    SecretKeySpec sks = new SecretKeySpec(_password.getBytes(), "AES");
	    // Create cipher
	    Cipher cipher = Cipher.getInstance("AES");
	    cipher.init(Cipher.ENCRYPT_MODE, sks);
	    // Wrap the output stream
	    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
	    // Write bytes
	    int b;
	    byte[] d = new byte[8];
	    while((b = fis.read(d)) != -1) {
	        cos.write(d, 0, b);
	    }
	    // Flush and close streams.
	    cos.flush();
	    cos.close();
	    fis.close();
	}
	
	public void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
	    FileInputStream fis = new FileInputStream(_cypherTextFile);

	    FileOutputStream fos = new FileOutputStream(_plainTextFile);
	    SecretKeySpec sks = new SecretKeySpec(_password.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();
	}	
}
I have a feeling that the issues lay with either the encryption mode or the character set being used (or not being used) and what is compatible with AES Crypt.

I haven't worked in java in a long time and I am fairly noob with encryption so I apologize for any dumb mistakes.

Any insights are appreciated!

DS
User avatar
paulej
Posts: 595
Joined: Sun Aug 23, 2009 7:32 pm
Location: Research Triangle Park, NC, USA
Contact:

Re: Android Encryption with javax.crypto decryption with AES

Post by paulej »

To be compatible with AES Crypt, there are several things that must be done. First, the cipher text file must be properly constructed. This code does not appear to create a file following the file format.

As elements of creating a compatible file, the code must use AES using CBC mode. It must also produce an HMAC using SHA-256. I don't see that in there either.

There is Java code on aescrypt.com that will produce compatible files. You might want to look at that code. You could also port the Linux C code to Android.
Post Reply