Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 1 of 1
  1. #1
    Join Date
    Feb 2001
    Location
    Indigo Rose Software
    Posts
    2,728

    Post Function: Blowfish Encrypt/Decrypt Strings

    The following pair of functions can be used with the Crypto plugin in AutoPlay Media Studio 5.0 Professional to blowfish encrypt and decrypt strings.

    Note: The Crypto.BlowfishEncrypt and Crypto.BlowfishDecrypt actions only operate on files because blowfish-encrypted data can't be stored in a string without (the encrypted data contains characters that aren't valid string characters). The following two functions get around this limitation by using temporary text files and base64-encoding the encrypted versions so they can be read back in as strings.

    Code:
    -- Function: BlowfishEncryptString
    -- Purpose: Blowfish encrypts a string by saving it to a
    -- temporary file and then base-64 encoding the encrypted
    -- version to get the encrypted data in string form.
    -- Returns: The source string blowfish encrypted 
    -- and base64 encoded, or "" if an error occurs.
    function BlowfishEncryptString(SourceText, Key)
    
    	-- I've tried to choose filenames that are unlikely to already exist ;)
    	local tempsrc = _TempFolder.."\\ILoveMyMommysChicken.tempsrc";
    	local tempenc = _TempFolder.."\\ILoveMyMommysChicken.tempenc";
    
    	-- Write the source string out to a temporary file
    	TextFile.WriteFromString(tempsrc, SourceText);
    
    	-- Make sure the TextFile.WriteFromString action was successful
    	-- (If an error occurred, display the error message in a dialog, clean up and abort)
    	local error = Application.GetLastError();
    	if (error ~= 0) then
    		Dialog.Message("Error", _tblErrorMessages[error] , MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    		File.Delete(tempsrc);
    		return "";
    	end
    
    	-- Now blowfish encrypt the file
    	Crypto.BlowfishEncrypt(tempsrc, tempenc, Key);
    
    	-- Make sure the Crypto.BlowfishEncrypt action was successful
    	local error = Application.GetLastError();
    	if (error ~=0) then
    		Dialog.Message("Error", _tblErrorMessages[error] , MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    		File.Delete(tempsrc);
    		File.Delete(tempenc);
    		return "";
    	end
    
    	-- Now base64 encode the encrypted file and store it in a string
    	local strEncoded = Crypto.Base64EncodeToString(tempenc);
    
    	-- Make sure the Crypto.Base64EncodeToString action was successful
    	error = Application.GetLastError();
    	if (error ~=0) then
    		Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    		File.Delete(tempsrc);
    		File.Delete(tempenc);
    		return "";
    	end	
    
    	-- remove the temp files
    	File.Delete(tempsrc);
    	File.Delete(tempenc);
    	
    	return strEncoded;
    end
    
    -- Function: BlowfishDecryptString
    -- Purpose: Blowfish decrypts a string that was encrypted
    -- using the BlowfishEncryptString function.
    -- Returns: The source string base64 decoded and blowfish 
    -- decrypted, or "" if an error occurs.
    function BlowfishDecryptString(SourceText, Key)
    
    	-- I've tried to choose filenames that are unlikely to already exist ;)
    	-- (If you're wondering, I have an Australian friend who says this constantly,
    	-- I can't seem to get this out of my head today)
    	local tempsrc = _TempFolder.."\\ILoveMyMommysChicken.tempsrc";
    	local tempdec = _TempFolder.."\\ILoveMyMommysChicken.tempdec";
    
    	-- base64 decode the string to a temp file
    	Crypto.Base64DecodeFromString(SourceText, tempsrc);
    	
    	-- Make sure the Crypto.Base64DecodeFromString action was successful
    	-- (If an error occurred, display the error message in a dialog, clean up and abort)
    	local error = Application.GetLastError();
    	if (error ~=0) then
    		Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    		File.Delete(tempsrc);
    		return "";
    	end	
    
    	-- Now blowfish decrypt the file
    	Crypto.BlowfishDecrypt(tempsrc, tempdec, Key);
    
    	-- Make sure the Crypto.BlowfishDecrypt action was successful
    	error = Application.GetLastError();
    	if (error ~=0) then
    		Dialog.Message("Error", _tblErrorMessages[error] , MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    		File.Delete(tempsrc);
    		File.Delete(tempdec);
    		return "";
    	end
    
    	-- Read the source string in from the temporary file
    	local strDecrypted = TextFile.ReadToString(tempdec);
    	
    	-- Check if any errors occurred from calling the TextFile.ReadToString action.
    	-- If an error occurred, display it's error message in a dialog message, clean up and abort.
    	error = Application.GetLastError();
    	if (error ~=0) then
    		Dialog.Message("Error", _tblErrorMessages[error] , MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    		File.Delete(tempsrc);
    		File.Delete(tempdec);
    		return "";
    	end
    
    	-- remove the temp files
    	File.Delete(tempsrc);
    	File.Delete(tempdec);
    
    	return strDecrypted;
    end
    I've attached a simple project that demonstrates how to use these two functions (and proves that they work ). The functions are contained in the Global Functions section and the code that uses them is in the On Click event for the "Test" button.

    Requirements:

    These functions and the example project require the Crypto plugin to be installed and enabled (go to Project > Plugins and make sure the "Crypto" item is checked).

    Note also that these functions are only for use with AutoPlay Media Studio 5.0 Professional Edition. The Standard Edition does not support action plugins.
    Attached Files

Similar Threads

  1. Function: Increase/Decrease Audio Playback Volume
    By Adam in forum AutoPlay Media Studio 5.0 Examples
    Replies: 0
    Last Post: 05-31-2004, 02:28 PM
  2. Function: Resize & Center an Image
    By kpsmith in forum AutoPlay Media Studio 5.0 Examples
    Replies: 0
    Last Post: 05-17-2004, 01:36 PM
  3. Function: Increase/Decrease Video Playback Volume
    By Adam in forum AutoPlay Media Studio 5.0 Examples
    Replies: 1
    Last Post: 05-14-2004, 12:18 PM
  4. Comparing Two Strings
    By Desmond in forum AutoPlay Media Studio 5.0 Examples
    Replies: 0
    Last Post: 10-03-2003, 01:23 PM
  5. Pass string's pointer in CallDLL function
    By sonysys in forum Setup Factory 6.0
    Replies: 1
    Last Post: 09-12-2003, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts