简体   繁体   中英

Encrypting/Decrypting files in Java

O want to encrypt and decrypt files using my Java program. These files are compiled Java .class files, I don't know what I am doing wrong. I am just testing now, but after encrypt and decrypt with the same key. It shows that file is

Exception in thread "AWT-EventQueue-0" java.lang.ClassFormatError: Truncated class file

and that class cannot be loaded. There is my encrypter/decrypter class code:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class EncTool {

public static void encrypt(String key, String filename) throws Throwable {
    InputStream is = new FileInputStream("SomeFile.class");
    OutputStream os = new FileOutputStream("SomeFile.class");
    encrypt(key, Cipher.ENCRYPT_MODE, is, os);
}

public static void decrypt(String key, String filename) throws Throwable {
    InputStream is = new FileInputStream("SomeFile.class");
    OutputStream os = new FileOutputStream("SomeFile.class");
    decrypt(key, Cipher.DECRYPT_MODE, is, os);
}

public static void encrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {

    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = skf.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE

        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);

}

public static void decrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {

    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = skf.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE

        cipher.init(Cipher.DECRYPT_MODE, desKey);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);

}

public static void doCopy(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[64];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        os.write(bytes, 0, numBytes);
    }
    os.flush();
    os.close();
    is.close();
}

}

and for testing im executing it that way:

    try {
        EncTool.encrypt("somekey123", "SomeFile");
        EncTool.decrypt("somekey123", "SomeFile");
    } catch (Throwable e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

What am i doing wrong? Or maybe how should it be done?

EDIT

when my code looks that:

    public static void encrypt(String key, String filename) throws Throwable {
        InputStream is = new FileInputStream("Somefile.class");
        OutputStream os = new FileOutputStream("tempfile.class");

        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey desKey = skf.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE

            cipher.init(Cipher.ENCRYPT_MODE, desKey);
            CipherInputStream cis = new CipherInputStream(is, cipher);
            doCopy(cis, os);

            File file2 = new File("tempfile.class");    

            File f = new File("somefile.class");
            f.delete();
            file2.renameTo(f);
    }

Now it works, but these deleting and renaming thing doesn't look elegant, how can I do it more efficient?

You must write the output to a new file instead of overwriting the input while you are reading it. The way your code is written now the results will be undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM