String obtained from file decryption not complete
I have made something that can successfully write an encrypted string to a
file, although unfortunately when the file is decrypted, the string is
incomplete and incorrectly formed.
The string I write to the file is:
"This should be written to the file and read perfectly. There should be no
limit to the length of a string."
The string I recieve from the file is:
"g.is should be written to the file and read perfectly. There should be no
limit to the length of a strin"
Note the "g." at the end of "string" is instead at the start, and the "th"
is missing from "this".
The methods I use to encrypt/decrypt files can be found below. I will be
very grateful for any help.
private Cipher getCipher(int nMode){
String sKey = "f94mg1hs8802tk5h";
try{
DESKeySpec oKeySpec = new DESKeySpec(sKey.getBytes());
SecretKeyFactory oKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey oSecretKey = oKeyFactory.generateSecret(oKeySpec);
Cipher oCipher = Cipher.getInstance("DES");
if(nMode == 1) oCipher.init(Cipher.ENCRYPT_MODE, oSecretKey);
else oCipher.init(Cipher.DECRYPT_MODE, oSecretKey);
return oCipher;
} catch(Exception e){
errorReport("Failed getting cipher",
"main.tools.Utils.getCipher()", "", e);
}
return null;
}
public boolean writeEncryptedFile(String sFile, String[] aContent){
boolean bSuccess = false;
if(aContent.length > 0){
String[] aContentToWrite = new String[aContent.length];
try{
Cipher oCipher = getCipher(1);
if(oCipher == null){
errorReport("Failed writing encrypted data to file: \"" +
sFile + "\"", "main.tools.Utils.writeEncodedFile()",
"Cipher was null.", null);
return false;
}
String sFileLocation = getClass().getResource(sFileDir +
sFile).toString();
sFileLocation = sFileLocation.replace("file:/", "");
FileOutputStream oOutputStream = new
FileOutputStream(sFileLocation);
CipherOutputStream oCipherStream = new
CipherOutputStream(oOutputStream, oCipher);
byte[] oContent = new byte[512];
for(int nLine = 0; nLine < aContent.length; nLine++){
System.out.println("Writing \"" + aContent[nLine] + "\" to
file.");
oContent = aContent[nLine].getBytes();
oCipherStream.write(oContent);
}
oCipherStream.flush();
oCipherStream.close();
bSuccess = true;
} catch(Exception e){
errorReport("Failed writing encrypted data to file: \"" +
sFile + "\"", "main.tools.Utils.writeEncodedFile()", "Check
file path.", e);
}
}
return bSuccess;
}
public String[] readEncryptedFile(String sFile){
Stack<String> oLines = new Stack<String>();
String[] aReadLines = null;
try{
Cipher oCipher = getCipher(0);
String sFileLocation = getClass().getResource(sFileDir +
sFile).toString();
sFileLocation = sFileLocation.replace("file:/", "");
FileInputStream oInputStream = new FileInputStream(sFileLocation);
CipherInputStream oCipherStream = new
CipherInputStream(oInputStream, oCipher);
int nBytes;
byte[] oBytes = new byte[512];
while((nBytes = oCipherStream.read(oBytes)) != -1){
oLines.push(new String(oBytes, "UTF8"));
}
if(oLines.size() > 0){
aReadLines = new String[oLines.size()];
//TESTING//
//System.out.println("====================");
//System.out.println("Writing to file: " + sFile);
//System.out.println("--------------------");
//TESTING//
for(int nLine = 0; nLine < oLines.size(); nLine++){
aReadLines[nLine] = oLines.elementAt(nLine);
}
//TESTING//
//System.out.println(aReadLines[0]);
//System.out.println("====================");
//TESTING//
}
if(aReadLines.length > 0) return aReadLines;
oCipherStream.close();
} catch(Exception e){
errorReport("Failed reading encrypted data in file: \"" + sFile +
"\"", "main.tools.Utils.readEncryptedFile()", "Check file path.",
e);
}
return null;
}
No comments:
Post a Comment