RC4decrypt and RC4encrypt (String functions): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (Apply Dave's comments; still some missing / not found links)
m (remove old references)
 
(19 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Template:String:RC4encrypt subtitle}}
{{Template:String:RC4decrypt and RC4encrypt subtitle}}


The <var>RC4encrypt</var> <var>[[Intrinsic classes|intrinsic]]</var> function returns a binary string that is the method object string encrypted or decrypted with the specified RC4 encryption key.  The length of the returned string is the same as that of the object string.
<var>RC4decrypt</var> and <var>RC4encrypt</var> are synonyms for the [[Intrinsic classes|intrinsic]] function to return a binary string that is the method object string encrypted or decrypted with the specified RC4 encryption key.  The length of the returned string is the same as that of the object string.


RC4 is a two-way, or symmetric, cipher (algorithm), so encrypting a string with a key and then encrypting the result of that encryption with the same key produces the original string. That is, the following assertion should <b><i>always</i></b> hold:
RC4 is a two-way, or symmetric, [https://en.wikipedia.org/wiki/Stream_cipher stream] cipher (algorithm), so encrypting a string with a key and then decrypting the result of that encryption with the same key produces the original string. That is, the following assertion should <b><i>always</i></b> hold:
<p class="code">assert %string:rc4encrypt(%key):rc4decrypt(%key) eq %string
<p class="code">[[Assert statement|assert]] %string:rc4encrypt(%key):rc4decrypt(%key) eq %string
</p>
</p>
<var>[[RC4decrypt_(String_function)|RC4decrypt]]</var> is a synonym for <var>RC4encrypt</var>. You can use the RC4decrypt synonym to "document" that you are decrypting rather than encrypting in a call.
You can use <var>RC4decrypt</var> to "document" that you are decrypting, and
use <var>RC4encrypt</var> to "document" that you are encrypting. In the remainder of this article, you can use <var>RC4decrypt</var> and <var>RC4encrypt</var> interchangeably.


==Syntax==
==Syntax==
{{Template:String:RC4decrypt syntax}}
{{Template:String:RC4encrypt syntax}}
{{Template:String:RC4encrypt syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table>
<tr><th>%outString</th>
<tr><th>%outString</th>
<td>A string variable to receive the encrypted or decrypted method object <var class="term">string</var>.  Its length is the same as <var class="term">string</var>.</td></tr>
<td>A string variable to receive the encrypted or decrypted method object <var class="term">string</var>.  Its length is the same as <var class="term">string</var>.</td></tr>
<tr><th>string</th>
<tr><th>string</th>
<td>The string to which the method is applied.</td></tr>
<td>The string to which the method is applied.</td></tr>
<tr><th>key</th>
<tr><th>key</th>
<td> A string variable whose value is used to encrypt or decrypt the method object <var class="term">string</var>. The key is transformed and then combined with the object string. This key value must not be null nor longer than 255 bytes. (RC4 keys are rarely longer than 64 bytes.)</td></tr>
<td> A string variable whose value is used to encrypt or decrypt the method object <var class="term">string</var>. The key is transformed and then combined with the object string. This key value must not be null nor longer than 255 bytes. (RC4 keys are rarely longer than 64 bytes.)</td></tr>
</table>
</table>


==Usage notes==
==Usage notes==
<ul>
<ul>
<li>A complete explanation of RC4 encryption can easily be found on the Internet.
<li>A complete explanation of RC4 encryption can easily be found on the Internet. </li>
<li>You are <b><i>not</i></b> prevented from creating confusion by encrypting with <var>[[RC4decrypt_(String_function)|RC4decrypt]]</var> and decrypting with <var>RC4encrypt</var>.
 
<li>The <var class="term">key</var> value you provide is (internally) concatenated with itself until it reaches 256 bytes and becomes the "true" key that participates in the encryption algorithm. A consequence of this is that a key that consists of text that repeats will produce the same encryption result as the text alone (without its repetitions). For example, the eight-byte key <code>'AAAAAAAA'</code> is no stronger than the one-byte key <code>'A'</code>, so it is not very secure. Similarly, the ten-byte key <code>'ababababab'</code> is no stronger than the two-byte key <code>'ab'</code>.
<li>You are <b><i>not</i></b> prevented from creating confusion by encrypting with <var>RC4decrypt</var> and decrypting with <var>RC4encrypt</var>. </li>
<li>RC4 is the default stream cipher used in <var class="product">[[Janus Network Security]]</var>, and it may be used by customers who license <var class="product">[[Janus Network Security]]</var> or <var class="product">[[Janus_SOAP|Janus SOAP]]</var>.
 
<li>The <var>RC4encrypt</var> function is available as of <var class="product">Sirius Mods</var> version 7.3.</ul>
<li>The <var class="term">key</var> value you provide is (internally) concatenated with itself until it reaches 256 bytes and becomes the "true" key that participates in the encryption algorithm. A consequence of this is that a key that consists of text that repeats will produce the same encryption result as the text alone (without its repetitions). For example, the eight-byte key <code>'AAAAAAAA'</code> is no stronger than the one-byte key <code>'A'</code>, so it is not very secure. Similarly, the ten-byte key <code>'ababababab'</code> is no stronger than the two-byte key <code>'ab'</code>. </li>
 
<li>RC4 is the former default stream cipher used in <var class="product">[[Janus Network Security]]</var>. </li>
</ul>


==Examples==
==Examples==
<ol>
In the following example, the output string from the <var>RC4encrypt</var> method is assigned to a variable, converted to hex using the <var>[[StringToHex (String function)|StringToHex]]</var> intrinsic function to reveal its non-displayable characters, then decrypted to return the original input string:
<li>In the following example, the output string from the <var>RC4encrypt</var> method is assigned to a variable, converted to hex using the <var>[[StringToHex (String function)|StringToHex]]</var> intrinsic function to reveal its non-displayable characters, then decrypted to return the original input string:
 
<p class="output">%string = 'this is a test':rc4encrypt('key')
<p class="code">%string = 'this is a test':rc4encrypt('key')
printText {~} is {%string:stringTohex}
[[PrintText statement|printText]] {~} is {%string:stringTohex}
printText {~} is: {%string:rc4decrypt('key')}
printText {~} is: {%string:rc4decrypt('key')}
</p>
</p>
Line 39: Line 47:
%string:rc4decrypt('key') is: this is a test
%string:rc4decrypt('key') is: this is a test
</p>
</p>
</ol>


==See also==
==See also==
<ul>
<ul>
<li><var>[[MD5digest (String function)|MD5digest]]</var> and <var>[[SHAdigest (String function)|SHAdigest]]</var> are cryptographic hash functions that also operate on the method object <var class="term">string</var>.
<li>Additional two-way ciphers:
<li>For details of the <var>printtext</var> statement, please see <var>[[Intrinsic classes#printtext|printText]]</var>.
<ul>
{{Template:AES crypto methods}}
{{Template:DEA crypto methods}}
</ul></li>
 
<li><var>String</var> hashes:
<ul>
{{Template:Digest methods}}
</ul></li>
</ul>
</ul>
{{Template:String:RC4encrypt footer}}
{{Template:String:RC4encrypt footer}}

Latest revision as of 21:28, 31 May 2016

RC4 encryption or decryption (String class)


RC4decrypt and RC4encrypt are synonyms for the intrinsic function to return a binary string that is the method object string encrypted or decrypted with the specified RC4 encryption key. The length of the returned string is the same as that of the object string.

RC4 is a two-way, or symmetric, stream cipher (algorithm), so encrypting a string with a key and then decrypting the result of that encryption with the same key produces the original string. That is, the following assertion should always hold:

assert %string:rc4encrypt(%key):rc4decrypt(%key) eq %string

You can use RC4decrypt to "document" that you are decrypting, and use RC4encrypt to "document" that you are encrypting. In the remainder of this article, you can use RC4decrypt and RC4encrypt interchangeably.

Syntax

%outString = string:RC4decrypt( key)

%outString = string:RC4encrypt( key)

Syntax terms

%outString A string variable to receive the encrypted or decrypted method object string. Its length is the same as string.
string The string to which the method is applied.
key A string variable whose value is used to encrypt or decrypt the method object string. The key is transformed and then combined with the object string. This key value must not be null nor longer than 255 bytes. (RC4 keys are rarely longer than 64 bytes.)

Usage notes

  • A complete explanation of RC4 encryption can easily be found on the Internet.
  • You are not prevented from creating confusion by encrypting with RC4decrypt and decrypting with RC4encrypt.
  • The key value you provide is (internally) concatenated with itself until it reaches 256 bytes and becomes the "true" key that participates in the encryption algorithm. A consequence of this is that a key that consists of text that repeats will produce the same encryption result as the text alone (without its repetitions). For example, the eight-byte key 'AAAAAAAA' is no stronger than the one-byte key 'A', so it is not very secure. Similarly, the ten-byte key 'ababababab' is no stronger than the two-byte key 'ab'.
  • RC4 is the former default stream cipher used in Janus Network Security.

Examples

In the following example, the output string from the RC4encrypt method is assigned to a variable, converted to hex using the StringToHex intrinsic function to reveal its non-displayable characters, then decrypted to return the original input string:

%string = 'this is a test':rc4encrypt('key') printText {~} is {%string:stringTohex} printText {~} is: {%string:rc4decrypt('key')}

The result is:

%string:stringTohex is E15655DAC416D10ACB3730FA22D2 %string:rc4decrypt('key') is: this is a test

See also