Game Development Community

Understanding and Using CipherLib with TGB

by Carpenter Software · in Torque Game Builder · 04/25/2007 (2:17 pm) · 9 replies

Well I have successfully compiled CipherLib with TGB 1.1.3 on the Mac. Yet before I begin testing, I have a question about the key used in the source code CipherLib/CipherBitmap.h.

As per the following DONE note from TDN: Be sure they got the extension you just registered and are encrypted with the same algorithm, key, and mode as specified at the top of CipherLib/CipherBitmap.h.

algorithm = "AES"
key = "KEY"
mode = "CBC"

What length or number of characters can I use for key? And does the password must match exactly the key? Is the key case sensitive?

Thanks
Carpenter Software

#1
04/25/2007 (2:59 pm)
The key can be any length and it is case-sensitive.

I have to say, I have never had the chance to test any of the code on a Mac.
#2
04/25/2007 (8:46 pm)
Hey Michael
Thanks for the response....

I may have a problem with your default test file..."OpenAL32.dll". Mac users do not usually carry these libraries. Any suggestions as to what would make a good test file?

// testBlockCipherStream( %cipher_name [,\"ECB\"|\"CBC\"], [%test_file], [%do_full_compare] );
//
// %cipher_name - The name that is registered in the factory
// optional ECB / CBC - The encryption/decryption mode
// optional %test_file - The file to use for the test. Default is "OpenAL32.dll".
// optional %do_full_compare - Not just CRC values but every single byte of the files will be compared
// e.g.

testBlockCipherStream( "Blowfish" );
testBlockCipherStream( "AES", "CBC" );
testBlockCipherStream( "XTEA", "ECB", "testfile.txt", true );

Carpenter Software
#3
04/25/2007 (9:04 pm)
Under the consolefunction in Cipher.cc source code, I located the following code:
if( argc >= 4 )
	{
		dSprintf(original_filename, 1024, "%s", argv[3]);
		dSprintf(encoded_filename, 1024, "encoded%s", argv[3]); 
	}
	else
	{
		dSprintf(original_filename, 1024, "OpenAL32.dll");
		dSprintf(encoded_filename, 1024, "encodedOpenAL32.dll"); 	
	}
It seems that the console function will allow me to enter a filename in the function arguments, if argc is greater or equal than 4. Will give it a try....

Carpenter Software.
#4
04/25/2007 (9:14 pm)
==>testBlockCipherStream( "AES", "CBC" , "CleanDSO.command");
BlockCipherStream Test Function:
Cipher = AES
Mode = CBC
test-file's name = CleanDSO.command
output-file's name = encodedCleanDSO.command
-> file encrypted in 1 ms
-> file decrypted via stream in 0 ms
CRC of decoded file = 273749016
CRC of original file = 273749016

I am not sure what the CRC is, but I cannot locate the output-file.

(edited)
// optional %do_full_compare - Not just CRC values but every single byte of the files will be compared
I'll try this next but maybe with a different file.

Carpenter Software
#5
04/26/2007 (12:08 am)
If the CRC value is the same for both files it means that the file that went through the encryption/decryption process is (most probably) the same as the original file. ( That means it worked :)

The files generated by the test are deleted automatically when the test has finished.
A tip on further testing: Try to use files of different sizes for your tests. Especially files where the size modulo blocksize of the stream is not equal to zero.
#6
04/26/2007 (10:30 am)
I tested "FishDemo/data/images/rocksfar.png" BUT does not work. The file has to be placed in the same folder as the app. Take a look at the two tests below:

==>testBlockCipherStream( "AES", "CBC", "FishDemo/data/images/rocksfar.png");
BlockCipherStream Test Function:
Cipher = AES
Mode = CBC
test-file's name = FishDemo/data/images/rocksfar.png
output-file's name = encodedFishDemo/data/images/rocksfar.png
Cipher::encryptFile() - Warning! Error open file for writing!
StreamUnknownError
-> file encrypted in 0 ms
Error opening file: StreamUnknownError

==>testBlockCipherStream( "AES", "CBC", "rocksfar.png");
BlockCipherStream Test Function:
Cipher = AES
Mode = CBC
test-file's name = rocksfar.png
output-file's name = encodedrocksfar.png
-> file encrypted in 33 ms
-> file decrypted via stream in 31 ms
CRC of decoded file = 1477151484
CRC of original file = 1477151484

Carpenter Software
#7
04/26/2007 (12:01 pm)
Yeah, that seems to be an error in the testing code. As you can see the outputfile just gets prepended the string "encoded". In the first case this results in an invalid file location (because there is no directory "encodedFishDemo").

I'll correct that when I find the time but there is nothing wrong with CipherStream classes. It's only error in the testing code.

edit: word missing...
#8
04/26/2007 (1:53 pm)
OK Michael Thanks

I believe I am ready for the next step..."encrypt your assets via a script written for the Cipher-tool".

Do I write the following as:
function testCipher()
{
    echo("Entered Function testCipher()");
    $CipherTool = new Cipher();
    
    echo("Cipher Name = " @ $CipherTool.getCipherName());
    echo("Buffer Size = " @ $CipherTool.getBufferSize());

    %filename = "FishDemo/data/images/rocksfar.png";
    // The default key
    %key = "KEY"; 
    %encrypted_filename = "FishDemo/data/images/rocksfar.xpng";
    $CipherTool.encryptFile(%filename, %key, %encrypted_filename);
}

(edited code)
Carpenter Software
#9
04/26/2007 (2:31 pm)
The results from the testCipher().

==>testCipher();
Entered Function testCipher()
Cipher Name = AES
Buffer Size = 102400

The encoded file was happily sitting there next to its cohort.

Next I will use the xpng in TGB....

Carpenter Software