Java File文件内容的加密与解密

发布时间:2025-02-07 12:41:40 作者:小樊
来源:亿速云 阅读:84

在Java中,对文件内容进行加密和解密可以通过多种方式实现。这里,我将向您展示如何使用Java内置的Cipher类来实现文件的加密和解密。我们将使用AES算法作为示例。

首先,确保您已将Java的加密扩展(Java Cryptography Extension, JCE)添加到项目中。对于Java 8及更高版本,默认已包含JCE,但对于早期版本,您可能需要下载并安装它。

以下是一个简单的示例,展示了如何使用AES算法对文件内容进行加密和解密:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class FileEncryptionDecryption {

    private static final String AES = "AES";
    private static final String CHARSET_NAME = "UTF-8";

    public static void main(String[] args) throws Exception {
        String inputFile = "input.txt";
        String encryptedFile = "encrypted.txt";
        String decryptedFile = "decrypted.txt";

        // 生成密钥
        SecretKey secretKey = generateSecretKey();

        // 加密文件
        encryptFile(inputFile, encryptedFile, secretKey);

        // 解密文件
        decryptFile(encryptedFile, decryptedFile, secretKey);
    }

    private static SecretKey generateSecretKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
        keyGenerator.init(128);
        return keyGenerator.generateKey();
    }

    private static void encryptFile(String inputFile, String outputFile, SecretKey secretKey) throws Exception {
        try (FileInputStream inputStream = new FileInputStream(inputFile);
             FileOutputStream outputStream = new FileOutputStream(outputFile);
             Cipher cipher = Cipher.getInstance(AES)) {

            cipher.init(Cipher.ENCRYPT_MODE, secretKey);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byte[] encryptedBytes = cipher.update(buffer, 0, bytesRead);
                if (encryptedBytes != null) {
                    outputStream.write(encryptedBytes);
                }
            }

            byte[] finalBytes = cipher.doFinal();
            if (finalBytes != null) {
                outputStream.write(finalBytes);
            }
        }
    }

    private static void decryptFile(String inputFile, String outputFile, SecretKey secretKey) throws Exception {
        try (FileInputStream inputStream = new FileInputStream(inputFile);
             FileOutputStream outputStream = new FileOutputStream(outputFile);
             Cipher cipher = Cipher.getInstance(AES)) {

            cipher.init(Cipher.DECRYPT_MODE, secretKey);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byte[] decryptedBytes = cipher.update(buffer, 0, bytesRead);
                if (decryptedBytes != null) {
                    outputStream.write(decryptedBytes);
                }
            }

            byte[] finalBytes = cipher.doFinal();
            if (finalBytes != null) {
                outputStream.write(finalBytes);
            }
        }
    }
}

在这个示例中,我们首先生成一个AES密钥,然后使用该密钥对文件内容进行加密和解密。请注意,这个示例仅用于演示目的,实际应用中可能需要更多的错误处理和功能。

此外,如果您希望将密钥存储在安全的地方,可以考虑使用Java KeyStore系统。这将允许您以更安全的方式存储和管理密钥。

推荐阅读:
  1. Java公钥私钥是什么意思
  2. Java上传下载文件并实现加密解密的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:如何遍历Java File文件目录

下一篇:如何使用Java File文件进行数据备份

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》