TeaEncode: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(12 intermediate revisions by 9 users not shown)
Line 2: Line 2:
{{Server_client function}}
{{Server_client function}}


{{New feature/item|9.06056|1.3.5|6056|
{{New feature/item|3.0135|1.3.5|6056|
This functions performs the [https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm Tiny Encryption Algorithm] on the given string and returns the [https://en.wikipedia.org/wiki/Base64 base64] representation of the encrypted block of data.
This functions performs the [https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm Tiny Encryption Algorithm] on the given string and returns the [https://en.wikipedia.org/wiki/Base64 base64] representation of the encrypted string.
}}
}}
{{Important Note|Use [[encodeString]] instead, because teaEncode inflates the file size by as high as ~77% (or by ~33% if the input isn't binary converted to base64 string).}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">string teaEncode ( string data, string key )</syntaxhighlight>
<syntaxhighlight lang="lua">string teaEncode ( string text , string key )</syntaxhighlight>


===Required Arguments===
===Required Arguments===
*'''data:''' The block of data you want to encrypt
*'''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)
*'''key:''' The key that should be used for encryption (Only first 16 characters are used)


===Returns===
===Returns===
Returns the [https://en.wikipedia.org/wiki/Base64 base64] representation of the encrypted data if the encryption process was successfully completed, ''false'' otherwise.
Returns the [https://en.wikipedia.org/wiki/Base64 base64] representation of the encrypted string if the encryption process was successfully completed, ''false'' otherwise.


==Example==
==Example==
Line 20: Line 21:
This example creates a /teaencrypt command, which performs the TEA on a given string with the specified key, if any.
This example creates a /teaencrypt command, which performs the TEA on a given string with the specified key, if any.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function encrypt(cmd, string, key)
function encryptString( cmd, theString, theKey )
     if string then
     if ( theString ) then
         if not key then
         if ( not theKey ) then
             key = string.rep(string.char(math.random(string.byte("a"), string.byte("Z"))), 10) -- Generate a random key of 10 characters
             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
         end
         local encodedString = teaEncode(string, key) -- Encode the string with the key
         local encodedString = teaEncode( theString, theKey ) -- Encode the string with the key
         outputChatBox("The encoded string is: " .. encodedString .. " (used key: " .. key .. ")")
         outputChatBox( "The encoded string is: " .. tostring( encodedString ) .. " (used key: " .. theKey .. ")" )
     else
     else
         outputChatBox("Syntax: /" .. cmd .. " (string) [key].")
         outputChatBox( "Syntax: /" .. cmd .. " [string] <[key]>" )
     end
     end
end
end
addCommandHandler("teaencrypt", encrypt)
addCommandHandler( "teaencrypt", encryptString )
</syntaxhighlight>
 
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
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Latest revision as of 10:48, 18 August 2018

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

[[{{{image}}}|link=|]] Important Note: Use encodeString instead, because teaEncode inflates the file size by as high as ~77% (or by ~33% if the input isn't binary converted to base64 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.

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:

function teaEncodeBinary( data, key )
    return teaEncode( base64Encode( data ), key )
end

function teaDecodeBinary( data, key )
    return base64Decode( teaDecode( data, key ) )
end

See Also