Welcome to Dream.In.Code
Become a Java Expert!

Join 150,416 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,037 people online right now. Registration is fast and FREE... Join Now!




AES Symmitric key cryptography

 
Reply to this topicStart new topic

AES Symmitric key cryptography, AES Key Generation and Storing the key in a key file.

franklin_mca
18 Mar, 2008 - 08:20 PM
Post #1

New D.I.C Head
*

Joined: 18 Mar, 2008
Posts: 2

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


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));
}
}

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: AES Symmitric Key Cryptography
18 Mar, 2008 - 10:26 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,167



Thanked: 45 times
Dream Kudos: 125
My Contributions
please tell us about the problem.
User is offlineProfile CardPM
+Quote Post

franklin_mca
RE: AES Symmitric Key Cryptography
20 Mar, 2008 - 12:05 AM
Post #3

New D.I.C Head
*

Joined: 18 Mar, 2008
Posts: 2

I want to know the way by which the generated key is stored as a key file in the file system.

QUOTE(franklin_mca @ 18 Mar, 2008 - 09:20 PM) *

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


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));
}
}


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month