1

i am working on my semester project that is about data encryption and decryption.i have done the encryption part successfully and logically the decryption should also be easy and it is easy but i cant figure out how to do it in Java.Because i am not expert in java.i have a string which looks like this

cipher_string = "57 630 88 372 51 421 39 629 26 450 67 457 14 522 72 360 55 662 57 550 74 449 12 530 73 619 69 367 43 431 75 617 97 620 51 540 64 529";

the above string is actually the ecrypted form of the plain text

user_plain_text = "hello this is bravo";

now if you see carefully you will figure out that the first number in cipher_string is a double figure number then the 2nd number is a 3 figure number then again a two figure number and then 3 figure number and so on....

now the 2 figure numbers are actually the names of the .txt files...ie 57.txt and 88.txt and 51.txt and so on..while the 3 figure numbers are actually the indexes of char inside file..now i want to open these .txt files in a specific sequence i.e open 57.txt file then go to index 630 and print the char at 630 in file 57.txt to user then again open file 88.txt and go to index 372 and print the char at 372 in file 88.txt to user and so on...but i don't know how to do it in java...please if some body can help me even if in pseudo code..(sorry for my bad English)

user3343955
  • 49
  • 1
  • 1
  • 6

3 Answers3

1

you need to split the cipher_string with split() (see: http://www.tutorialspoint.com/java/java_string_split.htm), after that you can create a for loop over the splitted array. In the for loop you can do something like:

BufferedReader reader = new BufferedReader(new FileReader(filePath));
reader.skip(n); // chars to skip
// .. and here you can start reading

stolen from: https://stackoverflow.com/a/10102821/1880847

Community
  • 1
  • 1
alpine
  • 29
  • 4
0

You have to split the coded string and then read the required character from the file. Find an example below, though it does not pay attention to proper file handling, neither does it handle exceptions.

String[] cipher_split = cipher_string.split(" ");
FileReader in;

for (String s : cipher_split) {
    if (s.length == 2) {
        File f = new File(s + ".txt");
        in = new FileReader(f);
    } else if (s.length == 3) {
        int i = 0;
        int c;
        while (i < Integer.parseInt(s)) {
            c = in.read();
        }
        System.out.print((char) c);
        in.close();
    }
}
0

You're going to want to use java.io.FileReader:

 import java.io.*;

 //... get your first number, in a variable.  Here I call it 'num', but use a more descriptive name.

 File txt = new File(num+".txt");
 FileReader fr = new FileReader(txt);

Now that we have a FileReader , we can use the skip(long) function to go directly to where we want:

// Load the second number into 'm'

fr.skip(m); 
decodedString += fr.read();

And then loop until finished.

Steven Hewitt
  • 302
  • 3
  • 12