Write a JAVA program to implement the Rijndael algorithm | Rijndael algorithm in JAVA | Cryptography
AIM: Write a C/JAVA program to implement the Rijndael algorithm logic.
HARDWARE AND SOFTWARE REQUIREMENT:
1. Intel based Desktop PC: - RAM of 512 MB
2. Notepad/Notepad ++ editor and Command Prompt or
3. IntelliJ IDEA Community Edition
THEORY:
Ø Rijndael is advanced version of AES(which is defined for fixed block size i.e.128 bit) which defined for variable length block sizes i.e.128,192 and 256 bits
Ø If a different block size between encryption and decryption is used, then it is not possible to recover the original plaintext.
Ø The Rijndael algorithm is a new generation symmetric encryption algorithm.
Ø It is block cipher algorithm data handling happened in 128-bits blocks,variable key length & variable round number.
Ø Encryption under Rijndael is achieved through a series of matrix transformations . Each transformation is known as a round
Ø Rijndael does not use a fixed number of rounds, the number of rounds are dependant on key/block sizes, as follows:
· For key/block size 128 bits , 9 rounds are required.
· For key/block size 192 bits, 11 rounds are required.
· For key/block size 256 bits, 13 rounds are required.
SOURCE CODE:
import javax.crypto.*;
import javax.crypto.spec.*;
class Rijndael {
private static String asHex(byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length*2);
int i;
for(i=0;i<buf.length;i++) {
if(((int)buf[i]&0xff)<0x10)
strbuf.append("0");
strbuf.append(Long.toString((int)buf[i]&0xff,16));
}
return strbuf.toString();
}
public static void main(String[]args) throws Exception {
String message = "ONLINE SMART TRAINER";
//Get the KeyGenerator
KeyGenerator kgen= KeyGenerator.getInstance("AES");
kgen.init(128); //192 and 256 bits may not be available
//Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec= new SecretKeySpec(raw,"AES");
// Instantiate the cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
byte[] encrypted= cipher.doFinal((args.length==0? message : args[0]).getBytes());
System.out.println("Encrypted String: " + asHex(encrypted));
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
byte[]original=cipher.doFinal(encrypted);
String originalString=new String(original);
System.out.println("Original String in Hexadecimal: " + asHex(original));
System.out.println("Original String: " + originalString);
}
}
OUTPUT:
VIVA QUESTIONS:
- What is JAVA?
- What is rijndael Algorithm?
- What is Encryption?
- What is Decryption?
Comments
Post a Comment