1

I have an mobile app and a desktop app.I have multiple list in desktop app. I want to encrypt all the values in the lists and send to a file and later from the mobile app i want retrieve the data from the file and decrypt the values and display them. I am using the encryption and decryption concept for the first time.I tried sending a string by encrypting and it worked.But i want to encrypt many list . How will i do that.Any code will be helpful.

For encrypting:

 KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
 SecretKey myDesKey = keygenerator.generateKey();
 Cipher desCipher;
 desCipher = Cipher.getInstance("DES");
 byte[] text = "Hello".getBytes("UTF8");


 desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
 byte[] textEncrypted = desCipher.doFinal(text);
 String s = new String(textEncrypted);
 System.out.println(s);

For Decrpyting

  desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
  byte[] textDecrypted = desCipher.doFinal(textEncrypted);
  s = new String(textDecrypted);
  System.out.println(s);

I used this code for a string but How to achieve the same with the list.Please help.

Thanks in advance.

sup
  • 101
  • 2
  • 11
  • How would you do it without encryption? Binary? Comma-separated? Tab-separated? XML? JSON? Whichever way you use to combine and later split the data, you do the same, except encrypt and decrypt the combined data. – Andreas Dec 21 '15 at 05:36
  • U mean i have to add everything in the stringbuilder and later encrypt the stringbuilder and send to file – sup Dec 21 '15 at 05:43
  • You don't encrypt a class. You encrypt **data**. You choose what data to encrypt. It just have to be in binary form, aka bytes. So, at first, forget encryption. How would you send the data if you didn't need encryption? – Andreas Dec 21 '15 at 05:48
  • FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); – sup Dec 21 '15 at 05:52
  • I was using the above two to write data – sup Dec 21 '15 at 05:52
  • Then use `ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(byteStream)); /*code here*/ out.close(); byte[] data = byteStream.toByteArray();`, then encrypt the bytes. – Andreas Dec 21 '15 at 05:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98531/discussion-between-sup-and-andreas). – sup Dec 21 '15 at 06:00
  • How will i encrypt multiple list and send to a single file – sup Dec 21 '15 at 06:36

2 Answers2

2

you can convert your list to byte array by

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(list);
byte[] text = bos.toByteArray();

then encrypt text as general. Then you can convert decrypted byte array to list as

ByteArrayInputStream bis = new ByteArrayInputStream(textDecrypted); ObjectInputStream ois = new ObjectInputStream(bis); List<String> result = (List<String>) ois.readObject();

Example:

        List<String> list = new ArrayList<String>();
        list.add("Hello");
        list.add(" World!!");

        System.out.println(list);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(list);
        byte[] text = bos.toByteArray();

        KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
        SecretKey myDesKey = keygenerator.generateKey();
        Cipher desCipher;
        desCipher = Cipher.getInstance("DES");

        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
        byte[] textEncrypted = desCipher.doFinal(text);

        desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
        byte[] textDecrypted = desCipher.doFinal(textEncrypted);

        ByteArrayInputStream bis = new ByteArrayInputStream(textDecrypted);
        ObjectInputStream ois = new ObjectInputStream(bis);
        List<String> result = (List<String>) ois.readObject();

        System.out.println(result);
Daniyar
  • 815
  • 1
  • 9
  • 22
  • How will i write to file after encrypting and read from a file and decrypt and add back to list – sup Dec 21 '15 at 05:50
  • @sup [link](http://stackoverflow.com/questions/4350084/byte-to-file-in-java) here example how to write byte[] to file, but to decrypt you need key, but I think you need to do something else... – Daniyar Dec 21 '15 at 05:57
  • which method should i use to decrypt the data – sup Dec 21 '15 at 06:04
  • How to encrypt multiple list and send to file. i understood with single list – sup Dec 21 '15 at 06:15
  • @sup do you need to encrypt multiple lists and save it to single file? – Daniyar Dec 21 '15 at 06:17
  • ya .I also want to read the same from the file and decrypt the lists and display in the application – sup Dec 21 '15 at 06:23
  • without encrypting and decrypting is there any other way of storing lists to file in human unreadable format – sup Dec 21 '15 at 07:29
1

You can use ArrayList and add every value in this list.

List<String> list = new ArrayList<>();
for () {
   // here first you encrypt the data then add to the list
}

Save it to file. And then when you retrieve you again put then in the list, and then:

for(String str: list) {
    // do decryption  
} 
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68