Show Some Love
JAVA

Encrypt and Decrypt an ArrayList

This forum was posted on Sunday, May 19, 2013 9:32:28 PM


I want to store an array list of strings in a file by encrypting it. And then I decrypt the file content and restore them to a array list. But when i decrypt the content blocks of 'Null' are there inside the content. With no 'Null' blocks rest of the text are the similar as i encoded.

public static void encryptFile(List moduleList  File fileOut) {
try {
OutputStream out = new FileOutputStream(fileOut);
out = new CipherOutputStream(out encryptCipher);
StringBuilder moduleSet = new StringBuilder();
for (String module : moduleList) {
moduleSet.append(module + "#");
}
out.write(moduleSet.toString().getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
} catch (java.io.IOException ex) {
System.out.println("Exception: " + ex.getMessage());
}
}
public static List decryptFile(File fileIn) {
List moduleList = new ArrayList();
byte buf = new byte16;
try {
InputStream in = new FileInputStream(fileIn);
in = new CipherInputStream(in decryptCipher);
int numRead = 0;
int counter = 0;
StringBuilder moduleSet = new StringBuilder();
while ((numRead = in.read(buf)) >= 0) {
counter++;
moduleSet.append(new String(buf));
}
String blocks = moduleSet.split("#");
System.out.println("Items: " + blocks.length);
} catch (java.io.IOException ex) {
System.out.println("Exception: " + ex.getMessage());
}
return moduleList;
}

I attempted with UTF-16 since strings are encoded in java in UTF-16 But it only makes the o/p worst.
Your ideas will be apprised...
Thanks


101 Viewed this Question



 Recent Replies on "Encrypt and Decrypt an ArrayList"

  • Nobody has replied on this post, be first to post answer.

Post Your Solution for this Question


Note: Please Put Code into <code></code> block.

Threads related to "Encrypt and Decrypt an ArrayList":

Saving works using Tomcat, jetty is giving me a java.lang.NoSuchMethodError for Persistence.getPersistenceUtil()

I'm running a spring mvc application that uses hibernate.When I save a form it works best in Tomcat 7.When I run the similar using an embedded instance of jetty I get this error:problem accessing /users/update. Reason: javax.persistence.Persistence.getPersistenceUtil()Ljavax/persistence/PersistenceUtil;Caused by:java.language.NoSuchMethodError: javax.persistence.Persistence.getPersistenceUtil()Ljavax/persistence/PersistenceUtil; at org.hibernate.validator.engine.resolver.JPATraversableResolver.is

Creating an independent jar file

I hv make a jar file using netbeans and it's working best in my system. But i require to make jar files which is capable of running in all systems which has JRE and it should work perfectly even the class path is not set in that systempackage circle;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.BorderFactory;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Swi

Looking for no-cron based Java Scheduler

I'm searching for a Java scheduling framework which is not timer-driven or any sort of cron-style scheduler like Quartz and others. there're awfully various issues and solutions here that address timer-driven schedulers but such a scheduler wont fix my problem. The problem I hv is that I hv a some threads where each takes msgs from a blocking queue assigned to it. When a blocking queue is blank the thread that reads from it is blocked and can not be assigned to little other thread. This app

How to create event on which every client would be subscribed and will be “fired” every time Timer task is done?

i require to kill randomly choosen member (thread) every x sec. So I made timer which task is to every x sec select randomly clientID. Clients in their infinite loop (in run() method) generally checks for choosen clientID and that abuses processor. One of solutions would be that I generate event on which every member would be subscribed (clients would be listeners). So my problem is How do I generate event that will be "fired" after timer task is done and after that clients can checkout for

Java EE Security

I'm an iOS developer and i require to develop a web service becoz the iOS app needs to recover little info from a database stored in a server. i require to develop this web service using Java EE but i wish to have little info about the security of Java EE becoz i require to ensure the security of data stored into the database on the server.Do you have little book or great tutorial to advise about Java EE security?

Error while running a java code for MATLAB

I am doing coding for a voice password in Java.While running the ja I get the following error msg: Error: could not find or load main class Files\MATLAB\MATLABWhat might be the problem?

Is validating fields in both constructor and setter considered bad redundant code?

I hv the following class : public class Project { private int id; private String name; public Project(int id String name) { if(name == null ){ throw new NullPointerException("Name can not be null"); } if(id == 0 ){ throw new IllegalArgumentException("id can not be zero"); } this.name = name; this.id = id; } private Project(){} public int getId() { return id; } public void setId(int id) { if(id == 0 ){ throw new IllegalArgumentException("id can not be zero"); } this.

Extending nested class from abstract class in a subclass and making the methods use the new definition of the nested class

I hv a base abstract class with a nested class inside of it. In one of the subclasses i require to extend the nested class but the abstract methods inherited from the base class expect the base class definition of the nested class (for return types and arguments). How can i make them use the new definition of the nested class?Also the nonabstract methods(which I'm not overriding) don't work properly either(and they worked best before I moved them to the abstract class).abstract class Base { prot

Initializing a boolean array to false

I have it piece of code snippet snippet below. How do I initialize each element = false?boolean seats = new boolean10I saw a similar issue. But the second line didnt make sense to me (Can you explain the 2nd line?). Boolean array = new Booleansize; Arrays.fill(array Boolean.FALSE);

Java not calling component's overloaded paintComponent method

first this is segment of a homework assignment to generate a mosaic image generator.i require The code snippet to repaint in realtime as it finds a image and place it ontop of other (source image).This is code snippet snippet to generate the panel in my main function.The last piece mypanel.generate() is the mosaic logic. myPanel = new mosiacPanel(sourceFile sizePercent pixesize threads imageList);//test.setText(Integer.toString(myPanel.getWidth()));JFrame frame2 = new JFrame("COS 226 MOSIAC OF AWESOMENESS