Results 1 to 1 of 1
Thread: Trouble with Encryption
- 04-24-2010, 11:52 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
Trouble with Encryption
Hello. I am trying to encrypt and decrypt a file using symmetric encryption. However, when I try to decrypt, I get an exception: Input length must be multiple of 8 when decrypting with padded cipher. How do I resolve this?
Here is my code:
Thank you.Java Code:public static void symmetricEncrypt(String filename) { try { File file = new File(filename); int len = (int)file.length(); byte[] input = new byte[len]; FileInputStream fis = new FileInputStream(file); int b = fis.read(); int i = 0; while(b != -1) { input[i] = (byte)b; i++; b = fis.read(); } fis.close(); SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede"); Cipher cipher = Cipher.getInstance("DESede"); byte[] cipherText = new byte[input.length]; cipher.init(Cipher.ENCRYPT_MODE, key); cipher.init(Cipher.ENCRYPT_MODE, key); cipherText = cipher.doFinal(input); FileOutputStream fos = new FileOutputStream(file); fos.write(cipherText); fos.close(); } catch(Exception e) { System.err.println("Failed to encrypt"); System.err.println(e.getMessage()); System.exit(1); } } public static void symmetricDecrypt(String filename) { try { File file = new File(filename); int ctLength = (int)file.length(); byte[] cipherText = new byte[ctLength]; FileInputStream fis = new FileInputStream(file); int b = fis.read(); int i = 0; while(b != -1) { cipherText[i] = (byte)b; i++; b = fis.read(); } fis.close(); System.out.println("Read file"); SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede"); Cipher cipher = Cipher.getInstance("DESede"); byte[] plainText = new byte[ctLength]; cipher.init(Cipher.DECRYPT_MODE, key); plainText = cipher.doFinal(cipherText); filename += ".decrypted"; file = new File(filename); FileOutputStream fos = new FileOutputStream(file); fos.write(plainText); fos.close(); } catch(Exception e) { System.err.println("Failed to decrypt"); System.err.println(e.getMessage()); System.exit(1); } }
Similar Threads
-
Aes Encryption
By rohitpatidar3 in forum Advanced JavaReplies: 7Last Post: 04-12-2010, 04:16 AM -
PGP Encryption&Decryption
By Deepa in forum New To JavaReplies: 2Last Post: 07-07-2009, 06:22 AM -
Encryption Project
By JennaTailier in forum New To JavaReplies: 6Last Post: 05-03-2009, 12:38 AM -
Image Encryption
By bugmenot in forum New To JavaReplies: 3Last Post: 04-20-2009, 03:20 PM -
encryption
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 02-06-2008, 10:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks