Open GoogleCodeExporter opened 8 years ago
Ive double checked tht hesToBytes works, by dumping the resultant byte buffer
as decimal integers, and manually checking each.
Original comment by hob...@gmail.com
on 9 Dec 2010 at 9:20
Interestingly, if you ask javax crypto to encrpt the same string using the same
key, it comes up with an ecrpyted string which is much longer.
Encryption on the java side, and decryption on the flash side works.
Original comment by hob...@gmail.com
on 9 Dec 2010 at 9:22
OK, I have reversed engineered the example program, and realised I have been
using the library completely wrongly for a long time. I was manually trying to
add padding, to avoid null pointer errors, but there is a level above AES which
does this for you.
Also, to use this library, you have to become something of an expert in the
undocumented paramers, such as ebc/cbc. Note: cbc needs a second key, called
an initialisation vector (IV). So ebc is much simipler (but presumaly less
secure).
However, I am not 100% sure this will work all the time - as Im not sure yet
how to account for the fact that one side uses 16 byte padding, and the other 8
byte.
This is how you are supposed to do it (flash side):
public static var KEY:ByteArray =
Hex.toArray("faf579668b4bb03be0732eb0a42a7ebe");
public static function encrypt2(data:String):ByteArray {
var dataByteArray:ByteArray =new ByteArray();
dataByteArray.writeUTFBytes(data);
trace("before encryption [" + Hex.fromArray(dataByteArray, true) + "]");
trace("before encryption [" + Hex.fromArray(dataByteArray, false) + "]");
var pad:IPad = new PKCS5;
var name:String = "aes-ecb";
var mode:ICipher = Crypto.getCipher(name, KEY, pad);
pad.setBlockSize(mode.getBlockSize());
mode.encrypt(dataByteArray);
trace("after encryption [" + Hex.fromArray(dataByteArray, true) + "]");
trace("after encryption [" + Hex.fromArray(dataByteArray, false) + "]");
return dataByteArray;
} // encrypt()
java side (not sure if this is how you are supposed to do it, but works):
static String decryptBytes(String message) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException{
byte[] messageBytes = CryptUtils.hexToBytes(message);
for (int i=0; i<messageBytes.length; i++) {
System.out.println("Byte[" + i + "]" + (0x000000FF & (int) messageBytes[i]) );
}
String result;
String strKeyHex="faf579668b4bb03be0732eb0a42a7ebe";
byte[] raw = CryptUtils.hexToBytes(strKeyHex);
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(messageBytes);
result = new String(decrypted);
return result;
}
Original comment by hob...@gmail.com
on 10 Dec 2010 at 11:38
Original issue reported on code.google.com by
hob...@gmail.com
on 9 Dec 2010 at 8:11