Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protect/encrypt R package code for distribution [closed]

Tags:

r

obfuscation

I am writing a package in R and would like to protect/crypt the code. Basically, when looking into my package code, it should be crypted and not readable. I have read that someone has crypted his code(1), however I haven't found any more information about that. I know I could just write the code in C/C++ and compile it, however I would like to let it in R and just "protect" it there.

My question is: Is this possible, how is this possible?

I appreciate your answer!

Reference:

(1) link

like image 652
Carol.Kar Avatar asked Aug 13 '14 09:08

Carol.Kar


2 Answers

I recently had to do something similar to this and it wasn't easy. But i managed to get it done. Obfuscating and/or encrypting scripts is possible. The question is, do you have the time to devote to it? You'll need to make sure whichever "obfuscation/encryption" method you use is very difficult and time consuming to crack, and that it doesn't slow down the execution time of the script.

If you wish to encrypt a Rscript code fast, you can do so using this site.

I tested the following rcode using the aforementioned site and it produced a very intimidating output, which somehow worked:

#!/usr/bin/env Rscript
for (i in 1:100){
    if (i%%3==0) if (i%%5==0) print("fizzbuzz") else print("fizz") else
    if (i%%5==0) print("buzz") else
    print(i)
}

If you do have some time on your hands and you wish to encrypt your script on your own, using your own improvised method, you'll want to use the openssl command. Why? Because it appears to be the one encryption tool that is available across most, if not all Unix systems. I've verified it exists on Linux (ubuntu, centos, redhat, mac), and AIX.

The simplest way to use Openssl to encrypt a file or script is:

1. cat <your-script> | openssl aes-128-cbc -a -salt -k "specify-a-password" > yourscript.enc

OR

2. openssl aes-128-cbc -a -salt -in <path-to-your-script> -k "yourpassword"

To decrypt a script using Openssl (notice the '-d'):

1. cat yourscript.enc | openssl aes-128-cbc -a -d -salt -k "specify-a-password" > yourscript.dec

OR

2. openssl aes-128-cbc -a -d -salt -in <path-to-your-script> -k "yourpassword" > yourscript.dec

The trick here would be to automate the supply of password so your users need not specify a password each time they wanted to run the script. Or maybe that's what you want?

like image 131
RoyMWell Avatar answered Oct 11 '22 07:10

RoyMWell


Did you try following that thread?

https://stat.ethz.ch/pipermail/r-help/2011-July/282717.html

At some point the R code has to be processed by the R interpreter. If you give someone encrypted code, you have to give them the decryption key so R can run it. Maybe you can hide the key somewhere and hope they don't find it. But they must have access to it in order to generate the plain text R code somehow.

This is true for all programs or files that you run or view on your computer. Encrypted PDF files? No, they are just obfuscated, and once you find the decryption keys you can decrypt them. Even code written in C or C++ distributed as a binary can be reverse engineered with enough time, tools, and clever enough hackers.

You want it protected, you keep it on your servers, and only allow access via a network API.

like image 24
Spacedman Avatar answered Oct 11 '22 06:10

Spacedman