Note that there are some explanatory texts on larger screens.

plurals
  1. PODES encrypt/decrypt from a file
    primarykey
    data
    text
    <p>I am writting a program where I take a string, encrypt it and then write it in a file. Then later, I read from the file the string, decrypt it and then modify it. Here's my code for DES encryption/decryption:</p> <pre><code>/* class for crypting and decrypting a file */ class DESEncrypter { private Cipher encryptionCipher; private Cipher decryptionCipher; public DESEncrypter (SecretKey key) throws Exception { encryptionCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); encryptionCipher.init(Cipher.ENCRYPT_MODE, key); decryptionCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); decryptionCipher.init(Cipher.DECRYPT_MODE, key); } /* write to 'out' the encryption of the information read from 'in' */ public String encrypt(String unencryptedString) { String encryptedString = ""; try { byte[] unencryptedByteArray = unencryptedString.getBytes("UTF8"); byte[] encryptedBytes = this.encryptionCipher.doFinal(unencryptedByteArray); encryptedString = new sun.misc.BASE64Encoder().encode(encryptedBytes); } catch (Exception ex) { Logger.getLogger(DESEncrypter.class.getName()).log(Level.SEVERE, null, ex); } return encryptedString; } private static String bytes2String(byte[] bytes) { StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i &lt; bytes.length; i++) { stringBuffer.append((char) bytes[i]); } return stringBuffer.toString(); } /* write to 'out' the information obtained by decrypting the information read from 'in' */ public String decrypt (String encryptedString) throws UnsupportedEncodingException { byte[] unencryptedByteArray = new byte[4096]; try { // Encode bytes to base64 to get a string byte[] decodedBytes = new sun.misc.BASE64Decoder().decodeBuffer(encryptedString); // Decrypt unencryptedByteArray = this.decryptionCipher.doFinal(decodedBytes); } catch (Exception ex) { Logger.getLogger(DESEncrypter.class.getName()).log(Level.SEVERE, null, ex); } return bytes2String(unencryptedByteArray); } } </code></pre> <p>And this is the function where I write a encrypted String in a file:</p> <pre><code>public void writeToFileEncrypted(String filename, String owner, String departament) { try { BufferedReader br = new BufferedReader(new FileReader(new File("files_encrypted"))); String crypt = ""; String aux; while ((aux = br.readLine()) != null) { crypt += aux; } br.close(); String info = this.server.crypt.decrypt(crypt); info += filename + " " + owner + " " + departament + "\n"; /* delete the old encryption */ File temp = new File("files_encrypted"); temp.delete(); String infoCrypt = this.server.crypt.encrypt(info); File newFiles = new File("files_encrypted"); if (newFiles.createNewFile() == false) { log.severe("Failed to re-create the 'files_encrypted' file when trying to add a new file"); return; } BufferedWriter bw = new BufferedWriter(new FileWriter(newFiles)); bw.write(infoCrypt); bw.close(); } catch (Exception e) { log.warning("An exception was caught while trying to remove '" + clientName + "' from the banned list"); e.printStackTrace(); return; } } </code></pre> <p>While the server runs, I can make modification to that String from file(run that function many time). The problem is when I close the server and then I open it again because I get the error: javax.crypto.BadPaddingException: Given final block not properly padded</p> <p>This is how I read from file when the server opens:</p> <pre><code>BufferedReader br = new BufferedReader(new FileReader(new File("files_encrypted"))); String crypto = new String(); String aux; while ((aux = br.readLine()) != null) { crypto += aux; readBytes++; } br.close(); System.out.println(readBytes); info = this.crypt.decrypt(crypto); </code></pre> <p>Why do I get that error? What I'm doing wrong? I must write the encrypted String in file some other way?</p> <p>LATER EDIT:</p> <p>I've changed the function that read a String from a file, decrypt it, modify it , encrypt it and then write it in file.</p> <pre><code>public void writeToFileEncrypted(String filename, String owner, String departament) { try { File f = new File("files_encrypted"); int nrRead = 0; String info = null; FileInputStream fis = new FileInputStream(f); StringBuffer sb = new StringBuffer(); int ch; while ((ch = fis.read()) != -1) { sb.append((char)ch); nrRead++; } fis.close(); StringBuilder sba = null; if (nrRead != 0) { info = this.server.crypt.decrypt(new String(sb.toString().getBytes("UTF-8"), "UTF-8")); sba = new StringBuilder(info); sba.append(filename + " " + owner + " " + departament + " "); } else { sba = new StringBuilder(filename + " " + owner + " " + departament + " "); } /* delete the old encryption */ File temp = new File("files_encrypted"); temp.delete(); //System.out.println("before: " + sba.toString()); String infoCrypt = this.server.crypt.encrypt(sba.toString()); //System.out.println("after: " + infoCrypt); File newFiles = new File("files_encrypted"); if (newFiles.createNewFile() == false) { log.severe("Failed to re-create the 'files_encrypted' file when trying to add a new file"); return; } FileOutputStream fos = new FileOutputStream(newFiles); fos.write(infoCrypt.getBytes("UTF-8")); fos.flush(); fos.close(); } catch (Exception e) { log.warning("An exception was caught while trying to remove '" + clientName + "' from the banned list"); e.printStackTrace(); return; } } </code></pre> <p>I've also modified where I read the info from file when server opens for the first time:</p> <pre><code>FileInputStream fis = new FileInputStream(f); StringBuffer sb = new StringBuffer(); int ch; while ((ch = fis.read()) != -1) { sb.append((char)ch); readBytes++; } fis.close(); if (readBytes != 0) { System.out.println("on: " + sb.toString()); info = this.crypt.decrypt(new String(sb.toString().getBytes("UTF-8"), "UTF-8")); System.out.println("load: " + info); } } </code></pre> <p>At the System.out.println with "on: " what I read from file is exactly what I've written encrypted, without any spaces or new lines. If I read with read(buffer), where buffer is byte[], it seems that adds a lot of spaces.</p> <p>Although I've made all this modifications I still get the error javax.crypto.BadPaddingException: Given final block not properly padded</p> <p>Has somebody any idea what's going on here?</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.
 

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