4 Replies - 26333 Views - Last Post: 12 February 2010 - 02:36 AM Rate Topic: -----

#1 ahmed_sd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-March 08

need code for AES algorithm in java

Post icon  Posted 17 March 2008 - 11:35 PM

Hi all :)

This is Ahmed Farooq here.I have joined this community today only.

Can anybody provide code for AES Encryption Decryption algorithm in Java.

Thank you
Is This A Good Question/Topic? 0
  • +

Replies To: need code for AES algorithm in java

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: need code for AES algorithm in java

Posted 17 March 2008 - 11:39 PM

View Postahmed_sd, on 18 Mar, 2008 - 12:05 PM, said:

Hi all :)

This is Ahmed Farooq here.I have joined this community today only.

Can anybody provide code for AES Encryption Decryption algorithm in Java.

Thank you


and the answer is a big NO... Try something by yourself first and then if you face problems, put them here.
Was This Post Helpful? 0
  • +
  • -

#3 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 412
  • Joined: 19-December 07

Re: need code for AES algorithm in java

Posted 17 March 2008 - 11:42 PM

Simply type to Google "Java AES"

http://java.sun.com/...AES/AES_v1.html

I not quite good in security field... You just refer it ...

Erm, Are you Malaysian people? Take Cryto cos? :P
Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: need code for AES algorithm in java

Posted 18 March 2008 - 02:55 AM

Simply look for a JCE provider implementation in google, and it will contain an AES implementation (BouncyCastle is one of the popular ones, but SUN bundles its implementation in recent JREs / JDKs, so if you download the source of them you'll find it as well probably neatly commented)
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Wellwisher*


Reputation:

Re: need code for AES algorithm in java

Posted 12 February 2010 - 02:36 AM

import java.security.*;
   import javax.crypto.*;
   import javax.crypto.spec.*;
   import java.io.*;

   /**
   * This program generates a AES key, retrieves its raw bytes, and
   * then reinstantiates a AES key from the key bytes.
   * The reinstantiated key is used to initialize a AES cipher for
   * encryption and decryption.
   */

   public class AES {

     /**
     * Turns array of bytes into string
     *
     * @param buf	Array of bytes to convert to hex string
     * @return	Generated hex string
     */
     public 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="This is just an example";

       // 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 ?
          "This is just an example" : 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: " +
         originalString + " " + asHex(original));
     }
   }

Was This Post Helpful? 0

Page 1 of 1