Jump to content




Rc4 Encryption / Decryption Functions

api lua utility

8 replies to this topic

#1 AgentE382

  • Members
  • 119 posts

Posted 10 July 2013 - 10:07 PM

This is a fully standard-compliant, pure Lua, CC-compatible implementation of the RC4 / ARC4 / ARCFOUR symmetric stream cipher.
  • Standard-compliant: This implementation is a real implementation. For example, you can encrypt a string in an external program and decrypt it with this function. You can also use the streams however long you want without the internal state breaking.

  • Pure Lua: Originally designed for Lua 5.2.2, it required only 2 tweaks to convert to CC-Lua 5.1. As such, it uses standard Lua functions except for the bitwise XOR function. (Tweaks: Use `bit` instead of `bit32` and use `unpack` instead of `table.unpack`.)

  • CC-compatible: CC-Lua doesn't implement the upper range of the ASCII character set. To work around this, the function can return a table of character codes instead of a string. It can also take a table of character codes as input instead of a string, to facilitate a round-trip.

  • RC4: This implementation is based off of the Wikipedia article and the gnulib implementation.
Pastebin link: http://pastebin.com/Rxe673BJ

Update:

I will add other RC4-family ciphers, which attempt to fix RC4's weaknesses. Most have failed, but I will be posting the one that no one has broken as of yet.

Code:
Spoiler

Design notes:
  • No modification is made to any input. A local copy is made of all tables.

  • Everything is as optimized as well as I know how at this point. Please feel free to suggest optimizations if you see any. I'll test them and incorporate them.
Please test for bugs and let me know how you like it!

Changelog:

12-06-2014 - v2.0.1 - Fixed last change.
12-06-2014 - v2.0.0 - Added parameter to force byte-table output; made string output the default.
09-22-2013 - v1.2 - Added RC4-drpp[n] for convenience.
09-22-2013 - v1.1 - Shortened / optimized RC4. Fix for CC-Lua converting CR to LF (RC4).
07-09-2013 - v1.0 - Initial Release, RC4.

Edited by AgentE382, 06 December 2014 - 06:06 AM.


#2 Draktharis

  • Members
  • 5 posts

Posted 11 August 2013 - 01:04 AM

Is it supposed to return a table most of the time, and a random letter sometimes? (For 1 digit strings encrypted with 1 digit keys.)
Or am I just doing it wrong?

http://pastebin.com/WnBCzA8G

#3 AgentE382

  • Members
  • 119 posts

Posted 16 August 2013 - 07:04 PM

 Draktharis, on 11 August 2013 - 01:04 AM, said:

Is it supposed to return a table most of the time, and a random letter sometimes? (For 1 digit strings encrypted with 1 digit keys.)
Or am I just doing it wrong?

http://pastebin.com/WnBCzA8G
Basically, since CC-Lua doesn't do well with non-printable characters, it'll return a table instead of a string containing those characters.

In general, it will return a table after encryption. However, for one character, it might return a string (because the one character may encrypt to a printable character). With longer data, RC4 will almost certainly produce at least one non-printable character.

#4 Mitchfizz05

  • Members
  • 125 posts
  • LocationAdelaide, Australia

Posted 07 September 2013 - 09:31 PM

Those are big indencies.

#5 AgentE382

  • Members
  • 119 posts

Posted 12 September 2013 - 11:00 AM

 mitchfizz05, on 07 September 2013 - 09:31 PM, said:

Those are big indencies.

I'm not sure what you mean, but If you're talking about the S-Box, I hard-coded it for performance reasons. My first priority for this implementation is accuracy. My second is speed.

#6 AgentE382

  • Members
  • 119 posts

Posted 22 September 2013 - 07:18 PM

RC4 updated! Working on RC4A, VMPC, and RC4+!

#7 bentallea

  • Members
  • 19 posts

Posted 08 October 2013 - 12:59 AM

can i replace return astable and chars or string.char(unpack(chars)) with return chars , amd remove

  • if chars[n] > 127 or chars[n] == 13 then


  • astable = true


  • end
to force output a table of bytes, as that would be much mpre useful to me?

#8 AgentE382

  • Members
  • 119 posts

Posted 08 October 2013 - 06:59 AM

Change
local chars, astable = type(plaintext) == "table" and {unpack(plaintext)} or {string.byte(plaintext,1,#plaintext)}, false
to
local chars = type(plaintext) == "table" and {unpack(plaintext)} or {string.byte(plaintext,1,#plaintext)}

Delete
if chars[n] > 127 or chars[n] == 13 then
    astable = true
end

Change
return astable and chars or string.char(unpack(chars))
to
return chars


That will completely remove the string output functionality and overhead. Note that it will still accept string input.


Also note that before editing, it would usually output a bytetable when encrypting and a string when decrypting.

#9 bentallea

  • Members
  • 19 posts

Posted 08 October 2013 - 11:03 PM

thanks. that will make the implementation more usable for my project.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users