一篇文章教你Java文件加密

一、什么是FileEncryption

  • Java FileEncryption 是指使用Java编程语言对文件进行加密和解密的过程。加密是一种将数据转换为难以理解的形式的过程,以防止未经授权的访问。解密是将加密的数据恢复为原始形式的过程。
  • 在Java中,可以使用内置的库javax.crypto和java.security来实现文件加密和解密。这些库提供了各种加密算法,如AES(高级加密标准)、DES(数据加密标准)和RSA(一种非对称加密算法)等。

二、使用步骤

第一步:导入所需的类和接口。

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.security.NoSuchAlgorithmException;

第二步:定义要使用的加密算法(AES)和转换模式(AES)。

private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES";

第三步:写一个生成128位的AES密钥的方法。KeyGenerator类用于生成密钥,getInstance方法指定要使用的算法。

  • 在实际应用中,可以选择不同的加密算法,如DES(Data Encryption Standard,数据加密标准)、3DES(Triple DES,三重数据加密算法)和Blowfish等。但请注意,不同的算法具有不同的安全性和性能特点。在选择加密算法时,请根据您的需求和安全性要求进行权衡。
private static SecretKey generateSecretKey() throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
    keyGenerator.init(128);
    return keyGenerator.generateKey();
}

第四步:分别写一个使用AES算法对文件进行加密和解密的方法。

加密方法:

private static void encryptFile(String inputFilePath, String encryptedFilePath, SecretKey secretKey) throws Exception {
    Cipher cipher = Cipher.getInstance(TRANSFORMATION);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    //创建一个Cipher实例并使用密钥初始化为加密模式。
    byte[] inputBytes = Files.readAllBytes(Paths.get(inputFilePath));
    //读取输入文件的所有字节
    byte[] encryptedBytes = cipher.doFinal(inputBytes);
    //使用doFinal方法对这些字节进行加密
    Files.write(Paths.get(encryptedFilePath), encryptedBytes);
    //将加密后的字节写入加密文件。
}

解密方法:

private static void decryptFile(String encryptedFilePath, String decryptedFilePath, SecretKey secretKey) throws Exception {
    Cipher cipher = Cipher.getInstance(TRANSFORMATION);
    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    byte[] encryptedBytes = Files.readAllBytes(Paths.get(encryptedFilePath));
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

    Files.write(Paths.get(decryptedFilePath), decryptedBytes);
//这个方法使用AES算法对文件进行解密。
//它的操作与encryptFile方法类似,但是使用了解密模式(Cipher.DECRYPT_MODE)。
}

第五步:编写程序的主方法。它定义了输入文件、加密文件和解密文件的路径。接着,它生成一个AES密钥,然后使用该密钥对输入文件进行加密和解密。

public static void main(String[] args) {
        String inputFilePath = "D:\\Test\\File\\file.txt";//输入文件路径
        String encryptedFilePath = "D:\\Test\\EncryptedFile\\encryptedFile.txt";//加密文件路径
        String decryptedFilePath = "D:\\Test\\DecryptFile\\decryptedFile.txt";//解密文件路径
//文件路径可以选择自己想要的包括解密、加密后的文件名都可以根据需要改变
        try {
            SecretKey secretKey = generateSecretKey();//生成密钥
            encryptFile(输入文件路径, 加密文件路径, 密钥);
            //encryptFile(inputFilePath, encryptedFilePath, secretKey);
            decryptFile(encryptedFilePath, decryptedFilePath, secretKey);
            //decryptFile(加密文件路径, 解密文件路径, 密钥);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

完整代码:

package FileEncryptionTest;

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.security.NoSuchAlgorithmException;

public class FileEncryption {

    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES";

    public static void main(String[] args) {
        String inputFilePath = "D:\\Test\\File\\file.txt";
        String encryptedFilePath = "D:\\Test\\EncryptedFile\\encryptedFile.txt";
        String decryptedFilePath = "D:\\Test\\DecryptFile\\decryptedFile.txt";

        try {
            SecretKey secretKey = generateSecretKey();
            encryptFile(inputFilePath, encryptedFilePath, secretKey);
            decryptFile(encryptedFilePath, decryptedFilePath, secretKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    private static void encryptFile(String inputFilePath, String encryptedFilePath, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        byte[] inputBytes = Files.readAllBytes(Paths.get(inputFilePath));
        byte[] encryptedBytes = cipher.doFinal(inputBytes);

        Files.write(Paths.get(encryptedFilePath), encryptedBytes);
    }

    private static void decryptFile(String encryptedFilePath, String decryptedFilePath, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        byte[] encryptedBytes = Files.readAllBytes(Paths.get(encryptedFilePath));
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

        Files.write(Paths.get(decryptedFilePath), decryptedBytes);
    }
}

运行结果:

原文件路径:“D:\Test\File\file.txt”
原文件内容:
在这里插入图片描述
加密后文件路径:“D:\Test\EncryptedFile\encryptedFile.txt”;
加密后文件内容:
在这里插入图片描述

解密后文件路径:“D:\Test\EncryptedFile\encryptedFile.txt”;
解密后文件内容:
在这里插入图片描述

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值