在Java中,可以使用Java Cryptography Extension (JCE)库来实现文件的加密和解密。以下是一个简单的示例,使用AES加密算法对文件进行加密和解密:
首先,确保已经将JCE无限制强度策略文件添加到Java运行时环境中。
然后,创建一个名为FileEncryptor的类,并添加以下代码:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;
public class FileEncryptor {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
public static void encrypt(String key, String inputFile, String outputFile) throws Exception {
doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
}
public static void decrypt(String key, String inputFile, String outputFile) throws Exception {
doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}
private static void doCrypto(int cipherMode, String key, String inputFile, String outputFile) throws Exception {
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
byte[] inputBytes = readFileToByteArray(inputFile);
byte[] outputBytes = new byte[inputBytes.length];
if (cipherMode == Cipher.ENCRYPT_MODE) {
SecureRandom random = new SecureRandom();
byte[] iv = new byte[cipher.getBlockSize()];
random.nextBytes(iv);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(cipherMode, secretKey, ivParameterSpec);
outputBytes = cipher.doFinal(inputBytes);
System.arraycopy(iv, 0, outputBytes, 0, iv.length);
} else if (cipherMode == Cipher.DECRYPT_MODE) {
byte[] iv = new byte[cipher.getBlockSize()];
System.arraycopy(inputBytes, 0, iv, 0, iv.length);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(cipherMode, secretKey, ivParameterSpec);
outputBytes = cipher.doFinal(inputBytes);
}
writeFileFromByteArray(outputFile, outputBytes);
}
private static byte[] readFileToByteArray(String fileName) throws IOException {
FileInputStream fileInputStream = new FileInputStream(fileName);
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
fileInputStream.close();
return bytes;
}
private static void writeFileFromByteArray(String fileName, byte[] bytes) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
fileOutputStream.write(bytes);
fileOutputStream.close();
}
public static void main(String[] args) {
try {
String key = "ThisIsASecretKey"; // 16 bytes key for AES
String inputFile = "input.txt";
String encryptedFile = "encrypted.txt";
String decryptedFile = "decrypted.txt";
encrypt(key, inputFile, encryptedFile);
decrypt(key, encryptedFile, decryptedFile);
System.out.println("File encrypted and decrypted successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
在这个示例中,我们使用了AES加密算法和CBC模式。encrypt方法用于加密文件,decrypt方法用于解密文件。doCrypto方法是一个通用的方法,用于执行加密和解密操作。
要使用这个类,只需将main方法中的key、inputFile、encryptedFile和decryptedFile变量设置为适当的值,然后运行程序。这将加密指定的输入文件,并将加密后的文件保存到指定的输出文件中。接下来,它将解密加密的文件,并将解密后的文件保存到另一个指定的输出文件中。
请注意,这个示例仅用于演示目的,实际应用中可能需要更多的错误处理和安全性检查。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。