MARS is another AES candidate, operating on block of 128 bits and supporting key raging from 128 to 400 bits. Offers high security and high data rates.
Work on 32 bit words, based on a type-3 Feistel network in which one word of data is used to update the other 3 during the rounds. Uses simple operations like additions, subtraction and xor. It also uses a lookup table of 512 entries of 32 bit words called S-box. Sometimes this table is seen a two tables of 256 entries.
S-box table is given in the source code as a single table even if in code I use operations virtually dividing it. Other operations include fix rotations and data-dependent rotations, also multiplication modulo 2 raised to 32.
I’m not gonna post source code – you can download it below. In order to keep this short I’ll briefly explain functions purpose in the code. For more details about the working of MARS algorithm, including some pseudo-code, check out this document.
Functions ecrypt/decrypt are called from outside by user, supplying an array of bytes of any length representing data, and a key of aforementioned length sizes. It will automatically add and subtract padding from the plain/cipher text.
The above methods call encryptBloc/decryptBloc for each data block. This is the actual encryption/decryption process. It follows the pseudo-code from the given document above so you shouldn’t have problems picking it up.
To see how it comes together, I invite you to download the entire source code below.
Thanks for code! MARS seems to be rather slow compared to RC6, AES, Twofish and even Serpent (Bouncycastle implementation)…
Thanks for the comment! Indeed, although I didn’t see a comparison regarding speed between them, those enumerated by you seem to be faster. AES won against Serpent in AES contest, one of the reason being speed. Twofish, in software might be faster even than AES, but for key lengths of 256.
hey thanks for the code, i’m on working my project, implementating MARS algorithm for encryption in java. Do you have the sample program/application ?
For a complete working example of all the algorithms, check the eclipse project containing all code plus test cases. You can download it from the AES post.
Best regards!
wonderful!!!
thanks a lot for the code
can we keep this code as a final project for mca?
Don’t know what mca stands for. But you can use this code in whatever project you like.
Thanks for appreciation!
pls say weather it is applicable for the java code files for projects
thanks a lot for the code , excuse me i will use that code in my project .. Thanks in advance .
Glad it helped!
great codes. I want to know. How you develop the permutation array in code of polygram.
Hi, I have a problem with the compilation, there isn’t a main class, how can u run it? I use Eclipse
Hi, check AES post for a complete eclipse project.
Thank u 😉
the example runs good but i’m not able to integrate it into my project.
either the decrypted result is total screwd or causes ArrayIndexOutOfBoundsException at System.arraycopy(bloc, 0, tmp, i – 16, bloc.length);
HEY THANKS FOR THE CODE IT REALLY HELPED ME ALOT IN COLLEGE I LOVE YOU!
the code doesn’t exist , where I can find it now?
Can’t you re-upload the file
Dear n3vrax,
first of all thank you very much for your post. You are sharing great code !!!
I need an easy to use implementation of encryption/decryption, fast, reasonably secure and easily portable on any JVM. Some JVM I use on some dedicated hardware are very limited and without Java Encryption libraries. That’s why I tried your MARS and your AES implementations, both being great and self-contained. I tested them agains a ~1MB file to decide which one to use.
The result is strange… MARS takes about 100ms and AES about 1-2 secs for that ~1MB file!? In my context speed is way more important than better encryption and I would therefore use MARS instead of AES.
Am I using MARS correctly ?
——————
package standalone;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
public class Test {
private final static String PWD = “1234567890123456”; // 16 bytes => 128 bits
// private final static String PWD = “123456789012345678901234”; // 24 bytes => 192 bits
// private final static String PWD = “12345678901234567890123456789012”; // 32 bytes => 256 bits
// private final static String PWD = “123456789012345678901234567890123456789012345678”; // 48 bytes => 384 bits
public static void main( String[] args ) {
try {
byte[] key = PWD.getBytes( StandardCharsets.ISO_8859_1 );
byte[] clearData = Files.readAllBytes( Paths.get( “test-data.data” ) );
long startTime = System.currentTimeMillis();
byte[] encryptedData = MARS.encrypt( clearData, key );
byte[] decryptedData = MARS.decrypt( encryptedData, key );
boolean sameContent = Arrays.equals( clearData, decryptedData );
long endTime = System.currentTimeMillis();
if ( sameContent ) {
System.out.println( “Processed in [” + ( endTime – startTime ) + “] ms” );
} else {
System.out.println( “FAILED !” );
}
}
catch ( Exception e ) {
System.out.println( e.getMessage() );
}
}
}