• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Encrypting & Decrypting an XML File using Base64Encoder & Base64Decoder

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one give me a code sample to encrypt & decrypt an xml file

I tried this code. It works fine. But not supposed to use sun packages.
The URL http://java.sun.com/products/jdk/faq/faq-sun-packages.html says that its risky to use sun packages.


So can anyone help in doing encryption in other way.
Awaiting reply
Thanks in advance.


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.security.Key;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptDecrypt
{
private String characterEncoding;
private Cipher encryptCipher;
private Cipher decryptCipher;
private BASE64Encoder base64Encoder = new BASE64Encoder();
private BASE64Decoder base64Decoder = new BASE64Decoder();

private static Key getKey() throws Exception {
Key key = null;
ObjectInputStream in=null;
try {
in = new ObjectInputStream(new FileInputStream("d:\\key1"));
key = (Key)in.readObject();
} catch (FileNotFoundException e) {
throw new Exception("File not found"+ e.getMessage());
}finally {
if(in != null) {
try { in.close(); }catch(Exception ex){ }
}
}
return key;
}

public EncryptDecrypt(String characterEncoding) throws Exception{
Key key = getKey();
this.characterEncoding = characterEncoding;
this.encryptCipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
this.encryptCipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
this.decryptCipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
this.decryptCipher.init(javax.crypto.Cipher.DECRYPT_MODE, key);
}

synchronized public String encrypt(String text) throws Exception{
byte[] textBytes = text.getBytes(characterEncoding);
byte[] encryptedTextBytes = this.encryptCipher.doFinal(textBytes);
String encodedEncryptedText = this.base64Encoder.encode(encryptedTextBytes);
return encodedEncryptedText;
}

synchronized public String decrypt(String encodedEncryptedText) throws Exception{
byte[] encryptedTextBytes = this.base64Decoder.decodeBuffer(encodedEncryptedText);
byte[] textBytes = this.decryptCipher.doFinal(encryptedTextBytes);
String recoveredText = new String(textBytes, characterEncoding);
return recoveredText;
}

public static String getString(FileReader fr) throws Exception{
StringBuffer text = new StringBuffer();
String lineSep = System.getProperty("line.separator");
String nextLine="";
BufferedReader in = new BufferedReader(fr);
try{
while ((nextLine = in.readLine()) != null){
text.append(nextLine);
text.append(lineSep);
}
return text.toString();
} catch (Exception e) {
System.out.println("Error Occured in getString method:");
e.printStackTrace();
}finally{
in.close();
}
return null;
}

public static void main(String[] args){
try{

EncryptDecrypt encryptAgent = new EncryptDecrypt("ASCII");
String stxt = EncryptDecrypt.getString(new FileReader("d:\\enc\\one.xml"));
String encodedEncryptedText = encryptAgent.encrypt(stxt);
FileWriter fw = new FileWriter("d:\\enc\\one.xml");
fw.write(encodedEncryptedText);
fw.close();
System.out.println("done encryption");

String encryptedText = EncryptDecrypt.getString(new FileReader("d:\\enc\\two.xml"));
String recoveredText = encryptAgent.decrypt(encryptedText);
System.out.println("\n\nRecovered Text ..........[" + recoveredText + "]");
FileWriter fw1 = new FileWriter("d:\\enc\\three.xml");
fw1.write(recoveredText);
fw1.close();
System.out.println("done decryption");
}
catch (Exception e){
System.out.println("Error Occured:");
e.printStackTrace(System.out);
}
}
}
[ August 23, 2006: Message edited by: shiva kumar ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the Apache Jakarta Commons Codec library, it has a Base64 encoder and decoder that you can use.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this question substantially different from the one you posted yesterday? If not, then let's continue the discussion over there, where you got helpful replies.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic