我有一个java程序,我想从中访问一个需要管理员权限的文件。我使用 IntelliJ IDE 进行开发,我想从 IDE 启动程序。
我有一个带有两种方法
void encrypt(String password, String databaseName)
和 void decrypt(String password, String databaseName)
的 EncryptionModule。这些方法采用用户提供的密码和文件路径。应该可以加密/解密 csv 文件或 mysql 数据库。
它适用于 csv 文件,但要加密数据库,我需要访问数据库表。数据库位于 docker 容器内,因此我需要管理员权限才能访问该表。
如果没有管理员权限,程序会显示“文件不存在”。加密/解密方法中 if 子句中 else 情况的输出是什么:
Path path = Paths.get(databaseName);
if (Files.exists(path)){
...
} else {
System.out.println("File does not exist");
}
我已经尝试以 root 身份执行 IDE,但是当我打开项目时,IDE 崩溃了。我更喜欢一个无需以 root 身份执行整个 IDE 的解决方案。
如何授予程序所需的权限?或者对于我想做的事情还有其他解决方案吗?
操作系统:Ubuntu,但它也应该可以在 Windows 上运行。
代码:
package com.example.passwordsafe.data;
import com.example.passwordsafe.core.usecases.EncryptionModuleInterface;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
public class EncryptionModule implements EncryptionModuleInterface {
private static final int ITERATION_COUNT = 1000000;
private static final int KEY_LENGTH = 256;
private static final String PBKDF_ALGORITHM = "PBKDF2WithHmacSHA1";
private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
private static final String ALGORITHM = "AES";
@Override
public void encrypt(String password, String databaseName) {
Path path = Paths.get(databaseName);
if (Files.exists(path)){
File plaintextFile = new File(databaseName);
File encryptedFile = new File(databaseName + ".encrypted");
doEncryption(password, plaintextFile, encryptedFile);
plaintextFile.delete();
path = Paths.get(databaseName + ".encrypted");
try {
Files.move(path, path.resolveSibling(databaseName));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
System.out.println("File does not exist");
}
}
@Override
public void decrypt(String password, String databaseName) {
Path path = Paths.get(databaseName);
if (Files.exists(path)){
File encryptedFile = new File(databaseName);
File plaintextFile = new File(databaseName + ".decrypted");
doDecryption(password, encryptedFile, plaintextFile);
encryptedFile.delete();
path = Paths.get(databaseName + ".decrypted");
try {
Files.move(path, path.resolveSibling(databaseName));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
System.out.println("File does not exist");
}
}
private void doEncryption (String password, File inputFile, File outputFile) {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_ALGORITHM);
byte[] key = factory.generateSecret(spec).getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
byte[] ivBytes = new byte[16];
random.nextBytes(ivBytes);
IvParameterSpec iv = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] encValue = cipher.doFinal(inputBytes);
byte[] finalCiphertext = new byte[encValue.length+2*16];
System.arraycopy(ivBytes, 0, finalCiphertext, 0, 16);
System.arraycopy(salt, 0, finalCiphertext, 16, 16);
System.arraycopy(encValue, 0, finalCiphertext, 32, encValue.length);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(finalCiphertext);
inputStream.close();
outputStream.close();
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException |
IOException | BadPaddingException | InvalidKeySpecException | InvalidAlgorithmParameterException e) {
throw new RuntimeException(e);
}
}
private void doDecryption (String password, File inputFile, File outputFile) {
FileInputStream inputStream = null;
byte[] ivBytes = new byte[16];
byte[] salt = new byte[16];
byte[] readEncryptedBytesWithIvAndSaltPrefix;
try {
inputStream = new FileInputStream(inputFile);
readEncryptedBytesWithIvAndSaltPrefix = new byte[(int) inputFile.length()];
inputStream.read(readEncryptedBytesWithIvAndSaltPrefix);
} catch (IOException e) {
throw new RuntimeException(e);
}
byte[] inputBytes = new byte[readEncryptedBytesWithIvAndSaltPrefix.length - 32];
System.arraycopy(readEncryptedBytesWithIvAndSaltPrefix, 0, ivBytes, 0, 16);
System.arraycopy(readEncryptedBytesWithIvAndSaltPrefix, 16, salt, 0, 16);
System.arraycopy(readEncryptedBytesWithIvAndSaltPrefix, 32, inputBytes, 0, readEncryptedBytesWithIvAndSaltPrefix.length - 32);
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_ALGORITHM);
byte[] key = factory.generateSecret(spec).getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
byte[] encValue = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(encValue);
inputStream.close();
outputStream.close();
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException |
IOException | BadPaddingException | InvalidKeySpecException | InvalidAlgorithmParameterException e) {
throw new RuntimeException(e);
}
}
}
main中的方法调用:
String dbpath = "/var/lib/docker/volumes/d74a425c8728e5333a3472860c2b62a3e47a0b5655dd44cce1b4c47ac2c3b6b8/_data/password_safe/password_entries.ibd";
String password = "password";
encryptionModuleInterface.encrypt(password, dbpath);
使用 sudo 命令作为前缀。从命令行,例如 sudo mvn exec:java -Dexec.mainClass="com.example.Main"
你必须先安装EXEC插件
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>