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.

ok thanksss….

hey, can you tell me what convBytesWords do??

i’m trying to convert it to VB .NET , but the result is wrong

and I think it’s because convBytesWords part, thanks heheh

the code should not be a problem to convert as it is nothing special.

it takes 4 consecutive bytes from the key and put them into a 32 bit value, repeating this for all bytes within the key.

it uses basic binary manipulations, which shouldn’t be a problem to convert.

i want to ask you about RC-6 encryption, can i have your e-mail, please?

thank you

hie.. I am getting cipher text in some unicode char form.. symbolic.. why is it so???

what is this snippet doing???

for(int i = 0;i>> (i%4)*8) & 0xff);

}

how to convert in php ?? please..

because i try to convert but my source code error on the result..

if you know php well, it shouldn’t be that hard to translate

sry, but I can’t help you on this

but surely it is possible to translate is in any other language

maybe you can find libraries for php, if only the encrypting functionality is what you are for

HI , Thanks for the wonderful code. I just wanted to know how to get the standard output as the test vectors from the RSA pdf document you mentioned (snipper1). I have attached the snippet of the same.When I run the code , the ciphertext for some reason comes as in snipper 2.

Can you please help me in this.

snipper 1 :

plaintext 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

user key 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

ciphertext 8f c3 a5 36 56 b1 f7 78 c1 29 df 4e 98 48 a4 1e

snipper 2 :

Text clar: Algoritmul de criptare RC6

Text criptat RC6: ÐI.Ó÷sÃYÔ1c]Ä/×Cû¦Ö&ÁUÄ ‘z`Ï>

Text decriptat RC6: Algoritmul de criptare RC6

Hi

I’m not sure what is it that you are asking

the snipper 2 is from my test case of RC6, in order to see it is working

Text clar means the plaintext(sry, I used my native language)

text criptat is the ciphertext

and the third line is the decrypted cipher text, just to see it is the same as the original input

these are hardcoded in the test case file

Thank you for your prompt reply.

Actually the thing is I understood the snipper 2 , But I want the output in snipper 1 format , where user key is 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 instead of 123456789abcdefg (your hardcoded value ).

please help me , Thank you

Multumesc 🙂

Hey , any replies ?

heyy.

you can get the result by converting it to hex or some other encoding. in snippet 1 the cipher text has been converted to hex.

i’m sorry you can get flowcart for RC6 and manual calculation algorithm RC6, becouse i need for study,

if you need please send to my email

asepsuhardy14@gmail.com

Do you mean flowchart?

I’m pretty sure you’ll find one on the internet

thanks!!! please sir i want to no why param u is not use in converbyteswords function!!

please help me sir

Thanks for putting this algorithm!!!

I am facing problem during the decryption……

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException

at java.lang.System.arraycopy(Native Method)

at com.securityAlgorithm.RC6.decrypt(RC6.java:223)

**** this problems occurs if encrypted data byte is less than 16 as a input to decryption method

why in need do &0xff