TeaEncode: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 38: Line 38:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function teaEncodeBinary( data, key )
function teaEncodeBinary( data, key )
     return teaEncode( base64encode( data ), key )
     return teaEncode( base64Encode( data ), key )
end
end
function teaDecodeBinary( data, key )
function teaDecodeBinary( data, key )
     return base64decode( teaDecode( data, key ) )
     return base64Decode( teaDecode( data, key ) )
end
end
</section>
</section>

Revision as of 17:47, 13 May 2015

This functions performs the Tiny Encryption Algorithm on the given string and returns the base64 representation of the encrypted string.

Syntax

string teaEncode ( string text , string key )

Required Arguments

  • text: The string you want to encrypt. (See second example if you want to encode binary data)
  • key: The key that should be used for encryption (Only first 16 characters are used)

Returns

Returns the base64 representation of the encrypted string if the encryption process was successfully completed, false otherwise.

Example

Click to collapse [-]
Client

This example creates a /teaencrypt command, which performs the TEA on a given string with the specified key, if any.

[lua]
function encryptString( cmd, theString, theKey )
    if ( theString ) then
        if ( not theKey ) then
            theKey = string.rep( string.char( math.random( string.byte( "a" ), string.byte( "Z" ) ) ), 10 ) -- Generate a random key of 10 characters if no key was passed in as an argument
        end
        local encodedString = teaEncode( theString, theKey ) -- Encode the string with the key
        outputChatBox( "The encoded string is: " .. tostring( encodedString ) .. " (used key: " .. theKey .. ")" )
    else
        outputChatBox( "Syntax: /" .. cmd .. " [string] <[key]>" )
    end
end
addCommandHandler( "teaencrypt", encryptString )

This shows how to use teaEncode/teaDecode with binary data: <syntaxhighlight lang="lua"> function teaEncodeBinary( data, key )

   return teaEncode( base64Encode( data ), key )

end function teaDecodeBinary( data, key )

   return base64Decode( teaDecode( data, key ) )

end

See Also