If you use a block-chaining mode like CBC, you need to provide an IvParameterSpec to the Cipher as well.
If you use a block-chaining mode like CBC, you need to provide an IvParameterSpec to the Cipher as well. So you can initialize an IvParameterSpec like this: // build the initialization vector. This example is all zeros, but it // could be any value or generated using a random number generator.
Byte iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; IvParameterSpec ivspec = new IvParameterSpec(iv); Then for encryption, change your code where you init the cipher to this: ecipher. Init(Cipher. ENCRYPT_MODE, keySpec, ivspec); And where you decrypt: c.
Init(Cipher. DECRYPT_MODE, keySpec, ivspec); So your complete code should look like this (It works for me): public class KeyHandler { Scanner scan = new Scanner(System. In); public KeyHandler() { try { startMenu(); } catch (Exception e) { System.out.
Println("fel någonstanns :)"); } } public void startMenu() throws Exception { System.out. Println("Hej. Med detta program kan du generera en hemlig nyckel" + "\n" + "Vill du:" + "\n" + "1.
Generera en nyckel" + "\n" + "2. Avsluta"); int val = Integer. ParseInt(scan.nextLine()); do { switch (val) { case 1: generateKey(); break; case 2: System.
Exit(1); default: System.out. Println("Du måste välja val 1 eller 2"); } } while (val! = 3); } public void generateKey() throws Exception { try { KeyGenerator gen = KeyGenerator.
GetInstance("AES"); gen. Init(128); SecretKey key = gen.generateKey(); byte keyBytes = key.getEncoded(); System.out. Print("Ge nyckeln ett filnamn: "); String filename = scan.next(); System.out.
Println("Genererar nyckeln..."); FileOutputStream fileOut = new FileOutputStream(filename); fileOut. Write(keyBytes); fileOut.close(); System.out. Println("Nyckeln är genererad med filnamnet: " + filename + "..."); System.
Exit(1); } catch (NoSuchAlgorithmException e) { } } public static void main(String args) { new KeyHandler(); } } public class EncryptHandler { private String encryptedDataString; private Cipher ecipher; AlgorithmParameterSpec paramSpec; byte iv; public EncryptHandler(String dataString, String secretKey, String encryptedDataString) { this. EncryptedDataString = encryptedDataString; try { encryptFile(dataString, secretKey); } catch (Exception e) { } } public void encryptFile(String dataString, String secretKey) throws Exception { FileInputStream fis = new FileInputStream(secretKey); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int theByte = 0; while ((theByte = fis.read())! = -1) { baos.
Write(theByte); } fis.close(); byte keyBytes = baos.toByteArray(); baos.close(); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); // build the initialization vector. This example is all zeros, but it // could be any value or generated using a random number generator. Byte iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; IvParameterSpec ivspec = new IvParameterSpec(iv); try { ecipher = Cipher.
GetInstance("AES/CBC/PKCS5Padding"); ecipher. Init(Cipher. ENCRYPT_MODE, keySpec, ivspec); } catch (Exception e) { e.printStackTrace(); } System.out.
Println("Encrypting file..."); try { encryptStream(new FileInputStream(dataString), new FileOutputStream(encryptedDataString)); } catch (Exception e) { e.printStackTrace(); } } public void encryptStream(InputStream in, OutputStream out) { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); byte buf = new byte1024; try { out = new CipherOutputStream(out, ecipher); // read the cleartext and write it to out int numRead = 0; while ((numRead = in. Read(buf)) >= 0) { out. Write(buf, 0, numRead); } bOut.
WriteTo(out); out.close(); bOut.reset(); } catch (java.io. IOException e) { } } public static void main(String args) { String data = "test. Txt"; String keyFileName = "a"; String encryptedFile = "krypterad.
Txt"; //String encryptedFile =args2; new EncryptHandler(data, keyFileName, encryptedFile); } } public class DecryptHandler { public DecryptHandler() { try { decryptFile(); } catch (Exception e) { e.printStackTrace(); System.out. Println("något gick fel :) "); } } public void decryptFile() throws Exception { byte buf = new byte1024; String keyFilename = "hemlig"; FileInputStream fis = new FileInputStream(keyFilename); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int theByte = 0; while ((theByte = fis.read())! = -1) { baos.
Write(theByte); } fis.close(); byte keyBytes = baos.toByteArray(); baos.close(); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); // build the initialization vector. This example is all zeros, but it // could be any value or generated using a random number generator. Byte iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; IvParameterSpec ivspec = new IvParameterSpec(iv); Cipher c = Cipher.
GetInstance("AES/CBC/PKCS5Padding"); System.out. Println("här"); c. Init(Cipher.
DECRYPT_MODE, keySpec, ivspec); System.out. Println("Decrypting file..."); try { decryptStream(new FileInputStream("krypterad. Txt"), new FileOutputStream("Dekryperad.
Txt"), c, buf); } catch (java.io. IOException e) { } System.out. Println("File decrypted!"); } public void decryptStream(InputStream in, OutputStream out, Cipher dcipher, byte buf) { try { in = new CipherInputStream(in, dcipher); // Read in the decrypted bytes and write the cleartext to out int numRead = 0; while ((numRead = in.
Read(buf)) >= 0) { out. Write(buf, 0, numRead); } out.close(); } catch (java.io. IOException e) { } } public static void main(String args) { new DecryptHandler(); } }.
Realized that CBC requiers and IV so I changed to ECB mode. Anyhow ECB mode will do fine for what im trying to acomplish. Thanks for your reply – Jimmy Gustafsson Jul 12 at 19:48 @Jimmy: You should not use ECB mode, it is insecure.
Instead, generate a random IV and store it as the first block of your file. (This is only 16 byte overhead. ) – PaÅlo Ebermann Aug 24 at 0:22.
Bear in mind that ECB mode is not secure, it leaks information. There is an excellent illustration (literally) of the information leakage on the Wikipedia page en.wikipedia.org/wiki/Block_cipher_modes.... For better security use CBC or CTR modes.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.