using AESCrypt.java to work with strings
Posted: Wed Feb 22, 2012 10:44 pm
Hi,
I am trying to adapt AESCrypt to work with strings instead of text files. My unit test is below. I'm getting "Message has been altered or password incorrect" when trying to return the encrypted string back to the original. Your suggestions are much appreciated!
I'm also wondering if there are suggestions for what "something else" might be as in:
"It only works in Java 6, but can be easily adapted to Java 5
by replacing the call to NetworkInterface.getHardwareAddress()
with something else."
try
{
AesEncryption crypt = new AesEncryption(true, "123456");
String text = "original text";
InputStream input = new ByteArrayInputStream(text.getBytes("UTF-8")); // getBytes("UTF-8"));
ByteArrayOutputStream output = new ByteArrayOutputStream();
crypt.encrypt(2, input, output);
String encrypted = output.toString();
System.out.println("original=" + text);
System.out.println("length from " + text.length() + " to " + encrypted.length());
System.out.println("encrypted=" + encrypted);
InputStream input2 = new ByteArrayInputStream(encrypted.getBytes("UTF-8")); // UTF-8 -16 -16LE
ByteArrayOutputStream output2 = new ByteArrayOutputStream();
crypt.decrypt(encrypted.length(), input2, output2);
String decrypted = output2.toString();
System.out.println(" decrypted=" + decrypted);
} catch (Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
Thanks!
Peter
I am trying to adapt AESCrypt to work with strings instead of text files. My unit test is below. I'm getting "Message has been altered or password incorrect" when trying to return the encrypted string back to the original. Your suggestions are much appreciated!
I'm also wondering if there are suggestions for what "something else" might be as in:
"It only works in Java 6, but can be easily adapted to Java 5
by replacing the call to NetworkInterface.getHardwareAddress()
with something else."
try
{
AesEncryption crypt = new AesEncryption(true, "123456");
String text = "original text";
InputStream input = new ByteArrayInputStream(text.getBytes("UTF-8")); // getBytes("UTF-8"));
ByteArrayOutputStream output = new ByteArrayOutputStream();
crypt.encrypt(2, input, output);
String encrypted = output.toString();
System.out.println("original=" + text);
System.out.println("length from " + text.length() + " to " + encrypted.length());
System.out.println("encrypted=" + encrypted);
InputStream input2 = new ByteArrayInputStream(encrypted.getBytes("UTF-8")); // UTF-8 -16 -16LE
ByteArrayOutputStream output2 = new ByteArrayOutputStream();
crypt.decrypt(encrypted.length(), input2, output2);
String decrypted = output2.toString();
System.out.println(" decrypted=" + decrypted);
} catch (Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
Thanks!
Peter