2

This is my encryption program. Primarily used to encrypt Files(text) This part of the program converts List<Integer> elements intobyte [] and writes it into a text file. Unfortunately i cannot provide the algorithm.

void printit(List<Integer> prnt, File outputFile) throws IOException
{
    StringBuilder building = new StringBuilder(prnt.size());

    for (Integer element : prnt)
 {
     int elmnt = element;
     //building.append(getascii(elmnt));
     building.append((char)elmnt);
 }

    String encryptdtxt=building.toString();
    //System.out.println(encryptdtxt);
    byte [] outputBytes = offo.getBytes();

    FileOutputStream outputStream =new FileOutputStream(outputFile);
    outputStream.write(outputBytes);


    outputStream.close();

}

This is the decryption program where the decryption program get input from a .enc file

void getfyle(File inputFile) throws IOException
{
    FileInputStream inputStream = new FileInputStream(inputFile);
    byte[] inputBytes = new byte[(int)inputFile.length()];
    inputStream.read(inputBytes);
    inputStream.close();
    String fylenters = new String(inputBytes);
    for (char a:fylenters.toCharArray())
  {
      usertext.add((int)a);
  }
    for (Integer bk : usertext)
  {
      System.out.println(bk);
  }

}

Since the methods used here, in my algorithm require List<Integer> byte[] gets converted to String first and then to List<Integer>and vice versa.

The elements while writing into a file during encryption do not match the elements read from the .enc file.

Is my method of converting List<Integer> to byte[] correct?? or is something else wrong? . I do know that java can't print extended ASCII characters so i used this .But, even this failed.It gives a lot of ?s Is there a solution?? please help me .. and also how to do it for other formats(.png.mp3....etc)

The format of the encrypted file can be anything (it needn't be .enc) thanxx

TUG
  • 41
  • 1
  • 7
  • Since you didn't disclose your encryption method (classical vs. modern) the problem is most likely `String fylenters = new String(inputBytes);`, because characters are not the same thing as bytes. – Artjom B. Dec 26 '15 at 15:36
  • i know but how to suucessfully convert bytes into characters without corrupting the string – TUG Dec 27 '15 at 03:07
  • Use some kind of encoding like Base64 or Hex. – Artjom B. Dec 27 '15 at 11:36

3 Answers3

3

There are thousands of different 'extended ASCII' codes and Java supports about a hundred of them, but you have to tell it which 'Charset' to use or the default often causes data corruption.

While representing arbitrary "binary" bytes in hex or base64 is common and often necessary, IF the bytes will be stored and/or transmitted in ways that preserve all 256 values, often called "8-bit clean", and File{Input,Output}Stream does, you can use "ISO-8859-1" which maps Java char codes 0-255 to and from bytes 0-255 without loss, because Unicode is based partly on 8859-1.

  • on input, read (into) a byte[] and then new String (bytes, charset) where charset is either the name "ISO-8859-1" or the java.nio.charset.Charset object for that name, available as java.nio.charset.StandardCharSets.ISO_8859_1; or create an InputStreamReader on a stream reading the bytes from a buffer or directly from the file, using that charset name or object, and read chars and/or a String from the Reader

  • on output, use String.getBytes(charset) where charset is that charset name or object and write the byte[]; or create an OutputStreamWriter on a stream writing the bytes to a buffer or the file, using that charset name or object, and write chars and/or String to the Writer

But you don't actually need char and String and Charset at all. You actually want to write a series of Integers as bytes, and read a series of bytes as Integers. So just do that:

void printit(List<Integer> prnt, File outputFile) throws IOException
{
    byte[] outputBytes = new byte[prnt.size()]; int i = 0;
    for (Integer element : prnt) outputBytes[i++] = (byte)element;

    FileOutputStream outputStream =new FileOutputStream(outputFile);
    outputStream.write(b);
    outputStream.close();
    // or replace the previous three lines by one
    java.nio.file.Files.write (outputFile.toPath(), outputBytes);
}

void getfyle(File inputFile) throws IOException
{
    FileInputStream inputStream = new FileInputStream(inputFile);
    byte[] inputBytes = new byte[(int)inputFile.length()];
    inputStream.read(inputBytes);
    inputStream.close();
    // or replace those four lines with
    byte[] inputBytes = java.nio.file.Files.readAllBytes (inputFile.toPath());

    for (byte b: inputBytes) System.out.println (b&0xFF);
    // or if you really wanted a list not just a printout
    ArrayList<Integer> list = new ArrayList<Integer>(inputBytes.length);
    for (byte b: inputBytes) list.add (b&0xFF);
    // return list or store it or whatever
}
dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70
0

Arbitrary data bytes are not all convertible to any character encoding and encryption creates data bytes including all values 0 - 255.

If you must convert the encrypted data to a string format the standard methods are to convert to Base64 or hexadecimal.

zaph
  • 111,848
  • 21
  • 189
  • 228
-1

In encryption part:

 `for (Integer element : prnt)
{
    int elmnt = element;
    //building.append(getascii(elmnt));
    char b = Integer.toString(elmnt).charAt(0);
    building.append(b);
}`

-->this will convert int to char like 1 to '1' and 5 to '5'