Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Devaka Cooray
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Beginning Java
Encryption-decryption loosing bytes in operation
mandlar suurla
Ranch Hand
Posts: 67
I like...
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is my encryption/decrypt code. But after decrypting file, it is some bytes bigger or smaller, i dont know how to get the exact bytes.
import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.AlgorithmParameterSpec; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Crypt { private final static int BUFFER_SIZE = 1024; private Cipher ecipher; private Cipher dcipher; private byte[] buf = new byte[BUFFER_SIZE]; // 8-byte Salt byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 }; // Iteration count int iterationCount = 19; Crypt(String passPhrase) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException { // Create the key KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES") .generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); // Create the ciphers ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } public void encrypt(InputStream in, OutputStream out) throws Exception { out = new CipherOutputStream(out, ecipher); buf = new byte[BUFFER_SIZE]; int countRead = 0; while ((countRead = in.read(buf, 0, BUFFER_SIZE)) != -1) { out.write(buf, 0, countRead); //System.out.println(countRead); } } public void decrypt(InputStream in, OutputStream out) throws Exception { in = new CipherInputStream(in, dcipher); buf = new byte[BUFFER_SIZE]; int countRead = 0; while ((countRead = in.read(buf, 0, BUFFER_SIZE)) != -1) out.write(buf, 0, countRead); } }
Simple use of it
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import javax.crypto.NoSuchPaddingException; import junit.framework.TestCase; public class TestCryption extends TestCase { File file = new File("C:\\test.txt"); public void testEncryption() { try { Crypt c = new Crypt("kass"); c.encrypt(new FileInputStream("C:\\winhelp.exe"), new FileOutputStream("C:\\winhelp2.exe")); c.decrypt(new FileInputStream("C:\\winhelp2.exe"), new FileOutputStream("c:\\decrypted.exe")); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeySpecException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Paul Clapham
Sheriff
Posts: 28382
99
I like...
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You aren't closing your output stream after you finish writing to it.
mandlar suurla
Ranch Hand
Posts: 67
I like...
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yeha it worked like charm
.
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
encrypt data in database
Encryption: Runtime Exception when run as JavaBean
InvalidKeyException
Problem Urgent: Crypto using jsp - javabean
Encryption-decryption file problem
More...