Article View: borland.public.jbuilder.java.language
Article #183the encrypted file is cutted shorter
From: zhebincong
Date: Mon, 10 Apr 2006 11:40
Date: Mon, 10 Apr 2006 11:40
132 lines
3951 bytes
3951 bytes
hello
my code as following:
public class EncryptHandler {
public EncryptHandler() {
}
public SecretKey genKey() {
try {
KeyGenerator kg = KeyGenerator.getInstance("DESede");
kg.init(168);
SecretKey key = kg.generateKey();
return key;
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
return null;
}
}
public SecretKey readKey(String path) {
try {
FileInputStream fin = new FileInputStream(path);
int num = fin.available();
byte[] keykb = new byte[num];
fin.read(keykb);
SecretKeySpec key = new SecretKeySpec(keykb, "DESede");
return key;
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
return null;
}
}
public void saveKeyAsObject(SecretKey key, String destPath) {
try {
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
destPath));
o.writeObject(key);
} catch (Exception e) {
System.exit(-1);
e.printStackTrace();
}
}
public void saveKeyAsByte(SecretKey key, String destPath) {
try {
byte[] kb = key.getEncoded();
FileOutputStream fo = new FileOutputStream(destPath);
fo.write(kb);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
public void encryptFile(String srcPath, String destPath, SecretKey key) {
try {
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, key);
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(srcPath)));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new CipherOutputStream(new FileOutputStream(destPath), cp)));
String tmpStr = "";
while ((tmpStr = reader.readLine()) != null) {
writer.write(tmpStr + "\n");
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
public void decryptFile(String srcPath, String destPath, SecretKey key) {
try {
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, key);
BufferedReader reader = new BufferedReader(new InputStreamReader(
new CipherInputStream(new FileInputStream(srcPath), cp)));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destPath)));
String tmpStr = "";
while ((tmpStr = reader.readLine()) != null) {
writer.write(tmpStr + "\n");
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] argv) {
String s = "Hello world!!!!";
EncryptHandler c = new EncryptHandler();
SecretKey key = c.genKey();
c.saveKeyAsByte(key, "c:\\temp\\testkey1");
SecretKey k = c.readKey("c:\\temp\\testkey1");
c.encryptFile("c:\\temp\\newpasswords", "c:\\temp\\oooo", key);
c.decryptFile("c:\\temp\\oooo", "c:\\temp\\ttttt", key);
}
}
i use symetrical key encrypt text firle the flow as follow:
1 generate key
2 save the key to the disk
3 read the key from disk
4 encrypt a text file
5 decrypt the file generated in step 4
but i find the final text file is cutted shortter then the original one, and i think the such problem result from "encryptFile" method, i guess it maybe result from the "padding" problem, but i don't know how to solve it, i am a beginner, who can help me?
thank you very much!!
Technical Details
Message-ID:
<443a441f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: f74b3,339aa11c263b4a95,start X-Google-Attributes: gidf74b3,public X-Google-Language: ENGLISH,ASCII-7-bit From: "zhebincong" <zhebincong@tom.com> Sender: "zhebincong" <zhebincong@tom.com> Reply-To: "zhebincong" <zhebincong@tom.com> Subject: the encrypted file is cutted shorter Newsgroups: borland.public.jbuilder.java.language X-User-Info: 202.108.130.138 NNTP-Posting-Host: 207.105.83.66 Message-ID: <443a441f$1@newsgroups.borland.com> Date: 10 Apr 2006 04:40:15 -0700 X-Trace: newsgroups.borland.com 1144669215 207.105.83.66 (10 Apr 2006 04:40:15 -0700) Lines: 132 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail