AES encryption/Decryption
hi,
I want to create a key and store it in a text file. while during encryption/decryption process, i read the key from the text file and proceed the process.
I have the following queries:
1. I create a key using AES algorithm convert into bytes and store it in a text file. My requirement is the content in text file should be in a readable format.
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
publicclass cryptoKey{
public SecretKeySpec skeySpec;
publicvoid keygeneration(){
try{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey skey = kgen.generateKey();
byte[] key = skey.getEncoded();
skeySpec =new SecretKeySpec(key,"AES");
}catch(Exception e){
System.out.println("error in keygen = "+e);
}
}
publicvoid keyFile(){
try{
File f=new File("C:\\keyFile.txt");
FileOutputStream fos =new FileOutputStream(f);
byte[] k=skeySpec.getEncoded();
String ss =new String(k,"ASCII");
fos.write(ss.getBytes());
}catch(Exception e1){
System.out.println("error file write "+e1);
}
}
publicstaticvoid main(String args[]){
cryptoKey cKey =new cryptoKey();
cKey.keygeneration();
cKey.keyFile();
}
}
2. I access the key from the text file, it must be in string or byte. How can i convert it into key object.
I dont like to download external jar files to achieve this requirement. Please help me.
Thanks in advance.
[2983 byte] By [
sakthiia] at [2007-10-3 2:41:22]

hi, For No 1 i got the answer, it was done by encoder and decoder of base64.Please help me, how to convert bytes to key object.Thanks in advance
Extrapolate on reply 3 of - http://forum.java.sun.com/thread.jspa?threadID=615118&tstart=0
hi,
I am facing the problem while encrypt a value with a newly created key.
The following class is used to generate key and store it in a file.
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class cryptoKey {
private SecretKeySpec keygeneration() {
SecretKeySpec skeySpec=null;
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256);
SecretKey skey = kgen.generateKey();
byte[] key = skey.getEncoded();
skeySpec = new SecretKeySpec(key,"AES");
}catch(Exception e) {
System.out.println("error in keygen = "+e);
}
return skeySpec;
}
public void keyFile() {
try{
FileWriter fos = new FileWriter("c:\\keyFile.txt");
BufferedWriter bw = new BufferedWriter(fos);
SecretKeySpec skeySpec=keygeneration();
byte[] key=skeySpec.getEncoded();
String encodedString=new String(key);
bw.write(encodedString);
bw.close();
}catch(Exception e1){
System.out.println("error file write "+e1);
}
}
public static void main(String args[]){
cryptoKey cKey = new cryptoKey();
cKey.keyFile();
}
}
The following class is used to read the key from the text file and encrypt the input value
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class cryptoEncrypt {
public void encrypt() {
try{
FileReader fis = new FileReader("C:\\keyFile.txt");
BufferedReader disFile = new BufferedReader(fis);
String s=disFile.readLine();
byte[] k = s.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(k, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
String msg = null;
System.out.print("Input String = ");
DataInputStream dis = new DataInputStream(System.in);
msg = dis.readLine();
byte[] encryptText = cipher.doFinal(msg.getBytes());
System.out.println("encrypted text "+new String(encryptText));
}catch(Exception e) {
System.out.println("error "+e);
}
}
public static void main(String args[]){
System.out.println("within crypto example");
cryptoEncrypt cryptoEnc = new cryptoEncrypt();
cryptoEnc.encrypt();
System.out.println("completed");
}
}
The tried the code with Base64 encoding and decoding even though i am facing the problem.
While running this program i am receiving the following error
java.lang.SecurityException: Unsupported keysize or algorithm parameters
Please help me. Thanks in advance
Keys contain bytes not characters so you need to use FileOutputSteam and FileInputStream not FileWriter and FileReader .
Thanks for your reply.
The code is working for
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
if i use kgen.init(192);
kgen.init(256);
then only i am facing the error.
i tried as FileInput and FileOutput Stream, but now also i am facing the same error.
hi
Key generation:
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class cryptoKey {
private SecretKeySpec keygeneration() {
SecretKeySpec skeySpec=null;
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey skey = kgen.generateKey();
byte[] key = skey.getEncoded();
skeySpec = new SecretKeySpec(key,"AES");
}catch(Exception e) {
System.out.println("error in keygen = "+e);
}
return skeySpec;
}
public void keyFile() {
try{
FileOutputStream fos=new FileOutputStream("c:\\keyFile.txt");
DataOutputStream dos=new DataOutputStream(fos);
SecretKeySpec skeySpec=keygeneration();
byte[] key=skeySpec.getEncoded();
BASE64Encoder base64 = new BASE64Encoder();
String encodedString = base64.encodeBuffer(key);
dos.write(encodedString.getBytes());
}catch(Exception e1){
System.out.println("error file write "+e1);
}
}
public static void main(String args[]){
cryptoKey cKey = new cryptoKey();
cKey.keyFile();
}
}
Code for Encryption
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.sql.*;
public class cryptoEncrypt {
public String encrypt(String msg) {
String encText=null;
try{
FileInputStream fis=new FileInputStream("c:\\keyFile.txt");
DataInputStream disFile=new DataInputStream(fis);
String s=disFile.readLine();
byte[] k = s.getBytes();
BASE64Decoder base64 = new BASE64Decoder();
byte[] kk=base64.decodeBuffer(s);
SecretKeySpec skeySpec = new SecretKeySpec(kk, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encryptText = cipher.doFinal(msg.getBytes());
System.out.println("encrypted text "+new String(encryptText));
}catch(Exception e) {
System.out.println("error "+e);
}
return encText;
}
public static void main(String args[]){
cryptoEncrypt cryptoEnc = new cryptoEncrypt();
String msg = null;
try{
System.out.print("Input String = ");
DataInputStream dis = new DataInputStream(System.in);
msg = dis.readLine();
}catch(Exception e){
System.out.println("error "+e);
}
//encryption done
String encryptValue=cryptoEnc.encrypt(msg);
System.out.println("completed");
}
}
