Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few things here. </p> <pre><code>private static String bytes2String(byte[] bytes) </code></pre> <p>Is dodgy, you are casting a byte to a char in this method so there is no character encoding specified here. To convert bytes to characters you should just use the String constructor that takes an array of bytes and an encoding. e.g.</p> <pre><code> byte[] tmp = new byte[10]; String a = new String(tmp, "UTF-8"); </code></pre> <p>Be careful using BufferedReaders + .readLine() - this will strip out any newline characters from your file as you read it unless you add them back into your buffer. Although I don't think this is your problem.</p> <p>But I think the best way to simplify your code is to write the encoded bytes via an OutputStream directly to the file. Unless you need to send the contents of the file over a transport that doesn't like binary data, there is no need to base64 encode. Just use Input/OutputStreams to write the encrypted bytes direct to disk.</p> <p>RESPONSE TO LATER EDIT:</p> <p>You are still mixing up your use of binary data (bytes) and character data (String/chars). You can't do things like:</p> <pre><code> int ch; while ((ch = fis.read()) != -1) { sb.append((char)ch); </code></pre> <p>The input stream is retuning bytes, a byte is not a character and just casting it to one is going to cause problems. When using encryption the output from the encryption operation is binary data, and the input to the decryption operation is also binary data. The fact that your are encrypting text is something you deal with before the encryption occurs, and after the decryption occurs. You basic operation should go along the following lines.</p> <ul> <li>Take the text you want to encrypt and convert it to bytes, specifying an encoding using the .getBytes(String charsetName) on your String.</li> <li>Pass these bytes into your encryption routine</li> <li>Write the resulting bytes directly to disk</li> </ul> <p>To decrypt:</p> <ul> <li>Read the bytes from the file</li> <li>Pass the bytes to your decryption routine (as bytes! no Strings/ text involved)</li> <li>Take the out put bytes and re-construct you String using new String(byte[] bytes, String charsetName) specifying the same encoding as before. </li> </ul> <p>You might find the following (untested, but should work) methods useful:</p> <pre><code>public byte[] readBinaryFile(File f) throws IOException { byte[] contents = new byte[(int)f.length()]; BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(f)); DataInputStream dis = new DataInputStream(bis); dis.readFully(contents); } finally { if(bis != null) { bis.close(); } } return contents; } public void writeBinaryFile(byte[] contents, File f) throws IOException { BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(f)); bos.write(contents); } finally { if(bos != null) { bos.close(); } } } </code></pre> <p>So you will also need to change the interface, and internals of your encrypt and decrypt methods so they take and return byte arrays, and ditch the base64 encoding.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload