Tuesday, May 3, 2011

AES algorithm

Hello,
Good information related to AES is given on wikipedia.
Follow http://en.wikipedia.org/wiki/Advanced_Encryption_Standard

AES implementation

Code language: Java
Crypto packages are used

-------------------------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


/*
 *Impliment AES algorithm
 * @author chakreshprasad
 */

public class aes
{
byte key[]={'T','h','i','s','i','s','S','e','c','r'
            ,'e','t','K','e','y','!'};
/* We have take 'key' in byte as we require key
   in byte format in this algorithm
 */
/* Size of key[] is 16 as 16*8=128 bits.
   It means that this version of AES is 128 bit version
 */
String encrypt(String plain) throws Exception
{
    Key keysp=new SecretKeySpec(key, "AES");
    Cipher c=Cipher.getInstance("AES");
    c.init(Cipher.ENCRYPT_MODE, keysp);
    byte[] envalue=c.doFinal(plain.getBytes());
    System.out.println("Encrypted values in bytes____");
    show(envalue);
    String enstring=new BASE64Encoder().encode(envalue);
    return enstring;
}
String decrypt(String ciphertext) throws Exception
{
    Key keysp=new SecretKeySpec(key, "AES");
    Cipher c=Cipher.getInstance("AES");
    c.init(Cipher.DECRYPT_MODE, keysp);
    byte[] deval=new BASE64Decoder().decodeBuffer(ciphertext);
    byte[] decode=c.doFinal(deval);
    System.out.println("Decrypted values in bytes____");
    show(decode);
    String plain=new String(decode);
    return plain;
}
void show(byte s[])
{
  
    System.out.println("No of bytes : "+s.length);
    for(int i=0;i<s.length;i++)
        System.out.print(" "+s[i]);
    System.out.println();
  
}
    public static void main(String[] args) throws Exception
    {
        String plain="",ciphertext="",dec="";
        aes as=new aes();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter data : ");
        plain=br.readLine();
        System.out.println("Plain Text : "+plain);
        ciphertext=as.encrypt(plain);
        System.out.println("Encrypted Text : "+ciphertext);
        dec=as.decrypt(ciphertext);
        System.out.println("Decrypted Text : "+dec);

    }

}
-----------------------------------------------------------------------------
Output:

Enter data : 
chakresh

Plain Text : chakresh

Encrypted values in bytes____
No of bytes : 16
 86 -127 79 97 -87 -34 87 82 -17 -56 50 -113 121 -85 36 125
Encrypted Text : VoFPYaneV1LvyDKPeaskfQ==

Decrypted values in bytes____
No of bytes : 8
 99 104 97 107 114 101 115 104
Decrypted Text : chakresh
-----------------------------------------------------------------------------------------------------------
From the output encrypted string, you may not be able to understand if the text is really a 128 bit output or not.
So I have also displayed the output in 'byte format' so that from the total no of bytes, you may tell that it is correct output.

Total no of bytes must be 16, since 16*8=128 bits.
-----------------------------------------------------------------------------------------------------------
There are many other methods available on net for AES implementation using packages.
Follow___




i haven't checked if they are working code or not. If you are going to use them, please tell us your experience.

1 comment:

  1. Great ! Finally I have found the source code of this algorithm. This is the most complex one for me and I was looking this complete program. You have posted so many other algorithms tool on this blog. Thank you.
    digital id

    ReplyDelete