RC6 algorithm was developed by RSA laboratories from USA. It’s an improved version over its predecessors like RC2, RC4,RC5. It was proposed as an AES candidate for substituting DES algorithm. Given this, it was developed having in mind the AES specifications like operating on 128 bit blocks.

RC6 is a fully parameterized algorithm denoted like RC6 – w/r/b (w – word length, r – rounds, b – key lenght). For AES specs w = 32, r = 20 and can be refered as simply RC6.

The key length b can be anywhere between 0 and 255 bytes. Most used values are 128, 192 or 256 key length(in bits).

For all cases, RC6 work on 4 words of w bits utilizing the following basic operations.

- a+b integer addition modulo 2
^{w}

- a-b integer subtraction modulo 2
^{w}

- a^b xor operation at word level of
*w*bits

- a×b integer multiplication modulo 2
^{w}

- a‹‹‹b word rotation to left with the value given by the least lgw bits of b

- a›››b word rotation to right with value given by the least lgw bits of b

where lgw is base 2 logarithm of w.

RC6 is a very compact algorithm, with no lookup tables. Following Java implementation shows its simplicity in code, yet being a very powerfull encryption algorithm.

### Encryption and decryption

RC6 works on 4 registers A, B, C, D initially containing the input data. The first byte of plaintext or ciphertext is placed in the least-significant byte of register A, the last byte of plaintext or ciphertext is placed into the most significant byte of D.

Check out a more detailed description of the algorithm in this material.

Regarding java code, here are the functions for RC6 as described for AES standard(w = 32, r = 20)

- Function
**encryptBloc**contains the actual algorithm for encryption. We take the input and split it up in 4 pieces of 32 bits, storing them in the 4 registers. Next is the actual implementation of the algorithm as described in the official document(see pseudocode). Function rotl is a rotation to left of the first param with second param steps. The number 5 for rotation in the first two is the value given by lgw = lg 32 = 5.Array S holds the w bit round keys as described in the key scheduling process.

private static byte[] encryptBloc(byte[] input){ byte[] tmp = new byte[input.length]; int t,u; int aux; int[] data = new int[input.length/4]; for(int i =0;i<data.length;i++) data[i] = 0; int off = 0; for(int i=0;i<data.length;i++){ data[i] = ((input[off++]&0xff))| ((input[off++]&0xff) << 8)| ((input[off++]&0xff) << 16)| ((input[off++]&0xff) << 24); } int A = data[0],B = data[1],C = data[2],D = data[3]; B = B + S[0]; D = D + S[1]; for(int i = 1;i<=r;i++){ t = rotl(B*(2*B+1),5); u = rotl(D*(2*D+1),5); A = rotl(A^t,u)+S[2*i]; C = rotl(C^u,t)+S[2*i+1]; aux = A; A = B; B = C; C = D; D = aux; } A = A + S[2*r+2]; C = C + S[2*r+3]; data[0] = A;data[1] = B;data[2] = C;data[3] = D; for(int i = 0;i<tmp.length;i++){ tmp[i] = (byte)((data[i/4] >>> (i%4)*8) & 0xff); } return tmp; }

- Following are 2 helper functions for rotation operation.

private static int rotl(int val, int pas) { return (val << pas) | (val >>> (32 - pas)); } private static int rotr(int val, int pas) { return (val >>> pas) | (val << (32-pas)); }

- Decryption is similar. Function
**decryptBloc**handles that

private static byte[] decryptBloc(byte[] input){ byte[] tmp = new byte[input.length]; int t,u; int aux; int[] data = new int[input.length/4]; for(int i =0;i<data.length;i++) data[i] = 0; int off = 0; for(int i=0;i<data.length;i++){ data[i] = ((input[off++]&0xff))| ((input[off++]&0xff) << 8)| ((input[off++]&0xff) << 16)| ((input[off++]&0xff) << 24); } int A = data[0],B = data[1],C = data[2],D = data[3]; C = C - S[2*r+3]; A = A - S[2*r+2]; for(int i = r;i>=1;i--){ aux = D; D = C; C = B; B = A; A = aux; u = rotl(D*(2*D+1),5); t = rotl(B*(2*B + 1),5); C = rotr(C-S[2*i + 1],t) ^ u; A = rotr(A-S[2*i],u) ^ t; } D = D - S[1]; B = B - S[0]; data[0] = A;data[1] = B;data[2] = C;data[3] = D; for(int i = 0;i<tmp.length;i++){ tmp[i] = (byte)((data[i/4] >>> (i%4)*8) & 0xff); } return tmp; }

### Key schedule for RC6

Key schedule uses 2 constant called “magic constant”. Their value is given below in hexadecimal.

private static int Pw=0xb7e15163, Qw=0x9e3779b9;

As a first step of the key schedule is preloading the user key of b byte into in array L of c words. The following function does this.

private static int[] convBytesWords(byte[] key, int u, int c) { int[] tmp = new int[c]; for (int i = 0; i < tmp.length; i++) tmp[i] = 0; for (int i = 0, off = 0; i < c; i++) tmp[i] = ((key[off++] & 0xFF))| ((key[off++] & 0xFF) << 8)| ((key[off++] & 0xFF) << 16)| ((key[off++] & 0xFF) << 24); return tmp; }

Having this defined, here is the key scheduling function for RC6

private static int[] generateSubkeys(byte[] key) { int u = w / 8; int c = key.length / u; int t = 2 * r + 4; int[] L = convBytesWords(key, u, c); int[] S = new int[t]; S[0] = Pw; for (int i = 1; i < t; i++) S[i] = S[i - 1] + Qw; int A = 0; int B = 0; int k = 0, j = 0; int v = 3 * Math.max(c, t); for (int i = 0; i < v; i++) { A = S[k] = rotl((S[k] + A + B), 3); B = L[j] = rotl(L[j] + A + B, A + B); k = (k + 1) % t; j = (j + 1) % c; } return S; }

The source code contains also 2 function encrypt and decrypt as interface with the user. They add padding to the data and split it in blocks.

Below is a link for download entire java implementation of RC6 as a .java file

how about encryption short message service (SMS) in android platform ??

do you have example for me about source code encryption RC6 in android, please help me..

For android you could use the source file which is in java and port it to android.

Don’t think there’s much to change on it.

Or, if you are developing something serious you could search for libraries over the internet.

As I remember there is some encryption algorithms available in the javax.crypto package also.

this encryption is not work in android, i mean when i send the string to server which is in encrypted at android side it is different

sry I can’t help, not having time. The code is just a simple exemplification of how the algorithm works, not something tested thoroughly.

I’m sure it’s not too hard to extend its functionality to you needs, or debug this problem.

Hey n3vrax.. thanks for the java file.. it was of great help.. but i couldnt get it executed.. it seems that the rc6 main class file is missing..

this is the error that i came across..

Exception in thread “main” java.lang.NoClassDefFoundError: RC6

Caused by: java.lang.ClassNotFoundException: RC6

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:303)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)

Could not find the main class: RC6. Program will exit.

could you help me out? thanks.

Im a student of computer science from India..

Did you download the full eclipse project?

It depends on how you tried to compile it.

I suggest you download the full project from my AES java implementation post and import it into Eclipse. There are all encryption algorithms along with a main class file for testing purposes. That should work. Hope you’ll manage to get it working

Best regards!

thanks for such useful post !!!!

Hey n3vrax.. Appreciate you replying.. Im not familiar with Eclipse.. Please let me know how the algorithm can be executed using cmd.. we were given RC algorithms as an academic project.. it’d be of great help if you let us know how it can be implemented.. thanks

Hey there!

I didn’t try to compile eclipse project from cmd till now, but if you take the necessary .java files and use javac, it can be done if paths are correctly set up.

Maybe you could find more details with google, as I’m not having much time to do a research at the moment.

Sry I couldn’t help.

there is no main class in your algorithm.. plz send me code of main class for rc6.

there is no main class in your algorithm.. plz send me code of main class for rc6.

plz send asap

You can find a complete eclipse project in my AES Java impementation post.

It contains some test cases for each algorithm I’ve put up.

thanks

the paddingKey function,

there’s an error -> ” key[key.length+i] = 0; “

yeah..i got it too.. what is the solution for it ?

if you are referring to the above error, I haven’t much time to check this.

if you find the solution, comment it back so anyone can see it.

thanks

how essential is the key padding function? will it work without as well?

whether the algorithm can be implemented to image ???

if you can please help me, please

Any encryption algorithm can be used for images too, as they are binary data. Just read file and feed it to encryption function.

thanks for posting this source..

i don’t know what the function of method paddingKey

but i try to understand what that’s mean…

so i try to resolve that problem like this..

public static byte[] paddingKey(byte[] key) {

int l = key.length%4;

byte[] newkey = new byte[key.length+l];

System.arraycopy(key, 0, newkey, 0, key.length);

for (int i = 0; i < l; i++)

{newkey[key.length+i] = 0;

}

return newkey;

}

it's working very well encrypt and decrypt

i want to ask something

in the “paddingKey” function, there’s a byte[] key parameter, is this key a plaintext (“abcde”) or you already convert it to bytes?

thanks

the param comes from encrypt method – being a byte array, it can be anything. In this case could be a string converted to bytes. Take a look at the complete eclipse project from the AES post for a test case.