我想用 java 加密和解密一个文件,我读过这个 url http://www-users.york.ac.uk/~mal503/lore/pkencryption.htm 我有两个文件,即公共安全证书和私人安全证书文件和 private.pem 文件,我复制这些文件并粘贴到当前目录并编写java代码如下,当我运行它时没有执行加密或解密,请看看这个并告诉我哪里出错了
I want to encrypt and decrypt a file in java, i had read this url http://www-users.york.ac.uk/~mal503/lore/pkencryption.htm and i got two files namely public Security certificate and private security certificate file and private.pem file, i copied these files and pasted in the current directory and worte java code as follows, when i run this no encryption or decryption is performed, pls see this and tell me where i went wrong
加密代码
File ecryptfile=new File("encrypt data");
File publickeydata=new File("/publickey");
File encryptmyfile=new File("/sys_data.db");
File copycontent =new File("Copy Data");
secure.makeKey();
secure.saveKey(ecryptfile, publickeydata);
secure.encrypt(encryptmyfile, copycontent);
解密代码
File ecryptfile=new File("encrypt data");
File privateKeyFile=new File("/privatekey");
File encryptmyfile=new File("/sys_data.db");
File unencryptedFile =new File("unencryptedFile");
try {
secure.loadKey(encryptmyfile, privateKeyFile);
secure.decrypt(encryptmyfile, unencryptedFile);
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
你只是弄乱了你的文件.此代码使用您链接的文章中描述的从 openssl 生成的 DER 文件工作:
You simply have muddled your files. This code works using the DER files generated from openssl as described in the article you linked:
FileEncryption secure = new FileEncryption();
// Encrypt code
{
File encryptFile = new File("encrypt.data");
File publicKeyData = new File("public.der");
File originalFile = new File("sys_data.db");
File secureFile = new File("secure.data");
// create AES key
secure.makeKey();
// save AES key using public key
secure.saveKey(encryptFile, publicKeyData);
// save original file securely
secure.encrypt(originalFile, secureFile);
}
// Decrypt code
{
File encryptFile = new File("encrypt.data");
File privateKeyFile = new File("private.der");
File secureFile = new File("secure.data");
File unencryptedFile = new File("unencryptedFile");
// load AES key
secure.loadKey(encryptFile, privateKeyFile);
// decrypt file
secure.decrypt(secureFile, unencryptedFile);
}
这篇关于如何在 Java 中加密或解密文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!