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 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