gpt4 book ai didi

java - AES 文件加密/解密

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:35 25 4
gpt4 key购买 nike

目标:使用JFileChooser选择文件,将该文件传递给加密方法,加密文件得到加密文件,最后通过JFileChooser将文件上传到服务器。我拥有的上传方法,或更具体地说是特定的代码行 'uploadedFile = client.uploadFile( "/"+ nameOf ,DbxWriteMode.add(), inputFile.length(),his);'

问题:正常上传工作正常,无需涉及加密,当尝试加密文件时,“KEY”无法放入 inputFile,服务器将不接受该文件,请参阅代码。

问题:如何将加密集成到此中,以便代码能够正常工作并有效地结合我的 uploadFile 方法和我拥有的加密方法

下面的工作代码没有加密

public void uploadFile() throws DbxException, FileLoadException, 

IOException, Exception {

try{

//auth method

phoneHome();

}catch(IOException e){

System.out.println("not saving accessToken");

JOptionPane.showMessageDialog(null, "Your Access Information Does
Not Exist,\n Please Login"+
"Please Login By Clicking 'OK'");
drop(); // will run auth method for user to login
}
fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int dialog = fc.showSaveDialog(this);
if (dialog == JFileChooser.APPROVE_OPTION) {
inputFile = fc.getSelectedFile();
inputFile.getName();
inputFile.getAbsoluteFile();
String nameOf = inputFile.getName();
System.out.println(" File: " + inputFile);


fis = new FileInputStream(inputFile);
// where the file is being sent to upload to dropbox
uploadedFile = client.uploadFile( "/" + nameOf ,DbxWriteMode.add(), inputFile.length(),fis);
}

communication.append("");
System.out.println("Uploaded: " + uploadedFile.toString());
communication.append("Uploaded: " + uploadedFile.toString());


JOptionPane.showMessageDialog(null,"File Upload:" + uploadedFile.toString(),
"Success!", JOptionPane.WARNING_MESSAGE);



}

上面需要实现的加密方法

private byte[] getKeyBytes(final byte[] key) throws Exception {
byte[] keyBytes = new byte[16];
System.arraycopy(key, 0, keyBytes, 0, Math.min(key.length, keyBytes.length));
return keyBytes;
}

public Cipher getCipherEncrypt(final byte[] key) throws Exception {
byte[] keyBytes = getKeyBytes(key);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
return cipher;
}
public void encrypt() throws Exception {
Cipher cipher = getCipherEncrypt(key);
FileOutputStream fos = null;
CipherOutputStream cos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(inputFile);
fos = new FileOutputStream(outputFile);
cos = new CipherOutputStream(fos, cipher);
byte[] data = new byte[1024];
int read = fis.read(data);
while (read != -1) {
cos.write(data, 0, read);
read = fis.read(data);
System.out.println(new String(data, "UTF-8").trim());
}
cos.flush();
} finally {
cos.close();
fos.close();
fis.close();
}
}

将这些人聚集在一起的想法?

最佳答案

首先检查:将原始文件压缩并上传

如果它有效,那么将您的加密文件压缩为zip,然后通过dropbox发送

关于java - AES 文件加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35237880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com