Java文件加密技术详解

在当今信息化社会,数据安全问题备受重视。尤其对于一些敏感信息或重要文件,我们往往需要加密保护来确保其安全性。在Java编程中,加密技术也得到了广泛的应用。本文将介绍Java文件加密的基本原理、常用技术和代码示例,帮助读者更好地了解和应用文件加密技术。

基本原理

文件加密是指通过一定的算法和密钥,将文件内容转换成不可读的形式,以达到保护文件内容的目的。在Java中,常用的文件加密技术包括对称加密和非对称加密。

  • 对称加密:使用相同的密钥进行加密和解密。常用的对称加密算法包括DES、AES等。
  • 非对称加密:使用公钥加密、私钥解密的方式进行加密和解密。常用的非对称加密算法包括RSA等。

加密技术应用

对称加密示例

下面是一个使用AES对称加密算法对文件进行加密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;

public class AESEncryption {

    public static void encryptFile(String inputFile, String outputFile, String key) throws Exception {
        Key secretKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        FileInputStream inputStream = new FileInputStream(inputFile);
        byte[] inputBytes = new byte[(int) inputFile.length()];
        inputStream.read(inputBytes);

        byte[] outputBytes = cipher.doFinal(inputBytes);

        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(outputBytes);

        inputStream.close();
        outputStream.close();
    }

    public static void main(String[] args) throws Exception {
        encryptFile("plaintext.txt", "encrypted.txt", "secretKey123");
    }
}

非对称加密示例

下面是一个使用RSA非对称加密算法对文件进行加密的示例代码:

import javax.crypto.Cipher;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAEncryption {

    public static void encryptFile(String inputFile, String outputFile, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        FileInputStream inputStream = new FileInputStream(inputFile);
        byte[] inputBytes = new byte[(int) inputFile.length()];
        inputStream.read(inputBytes);

        byte[] outputBytes = cipher.doFinal(inputBytes);

        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(outputBytes);

        inputStream.close();
        outputStream.close();
    }

    public static void main(String[] args) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        FileInputStream publicKeyFile = new FileInputStream("publicKey.pub");
        byte[] publicKeyBytes = new byte[(int) publicKeyFile.length()];
        publicKeyFile.read(publicKeyBytes);
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

        encryptFile("plaintext.txt", "encrypted.txt", publicKey);
    }
}

文件加密应用实例

假设我们有一个名为plaintext.txt的文件,我们可以使用上述示例代码对其进行加密。首先需要生成密钥,然后使用生成的密钥对文件进行加密。

生成密钥

可以通过以下代码生成AES密钥:

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;

public class KeyGeneratorExample {

    public static void generateKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] key = secretKey.getEncoded();
        System.out.println(new String(key));
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        generateKey();
    }
}

加密文件

使用生成的密钥对文件进行加密:

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class AESEncryption {

    public static void encryptFile(String inputFile, String outputFile, byte[] key) throws Exception {
        Key secretKey = new Secret